Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Symbol for the crate name instead of String/str #105423

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn link_binary<'a>(
sess,
crate_type,
outputs,
codegen_results.crate_info.local_crate_name.as_str(),
codegen_results.crate_info.local_crate_name,
);
match crate_type {
CrateType::Rlib => {
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc_data_structures::sync::SeqCst;
use rustc_errors::registry::{InvalidErrorCode, Registry};
use rustc_errors::{ErrorGuaranteed, PResult};
use rustc_feature::find_gated_cfg;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
use rustc_interface::{interface, Queries};
use rustc_lint::LintStore;
Expand Down Expand Up @@ -374,14 +375,14 @@ fn run_compiler(
queries.global_ctxt()?.peek_mut().enter(|tcx| {
let result = tcx.analysis(());
if sess.opts.unstable_opts.save_analysis {
let crate_name = queries.crate_name()?.peek().clone();
let crate_name = tcx.crate_name(LOCAL_CRATE);
sess.time("save_analysis", || {
save::process_crate(
tcx,
&crate_name,
crate_name,
compiler.input(),
None,
DumpHandler::new(compiler.output_dir().as_deref(), &crate_name),
DumpHandler::new(compiler.output_dir().as_deref(), crate_name),
)
});
}
Expand Down Expand Up @@ -678,7 +679,7 @@ fn print_crate_info(
let crate_types = collect_crate_types(sess, attrs);
for &style in &crate_types {
let fname =
rustc_session::output::filename_for_input(sess, style, &id, &t_outputs);
rustc_session::output::filename_for_input(sess, style, id, &t_outputs);
println!("{}", fname.file_name().unwrap().to_string_lossy());
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ pub trait LintStoreExpand {
node_id: NodeId,
attrs: &[Attribute],
items: &[P<Item>],
name: &str,
name: Symbol,
);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ impl InvocationCollectorNode for P<ast::Item> {
ecx.current_expansion.lint_node_id,
&attrs,
&items,
ident.name.as_str(),
ident.name,
);
}

Expand Down
38 changes: 21 additions & 17 deletions compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
use rustc_span::def_id::{DefPathHash, StableCrateId};
use rustc_span::edition::Edition;
use rustc_span::{create_session_if_not_set_then, Symbol};

#[test]
fn def_path_hash_depends_on_crate_id() {
Expand All @@ -11,26 +13,28 @@ fn def_path_hash_depends_on_crate_id() {
// the crate by changing the crate disambiguator (e.g. via bumping the
// crate's version number).

let id0 = StableCrateId::new("foo", false, vec!["1".to_string()]);
let id1 = StableCrateId::new("foo", false, vec!["2".to_string()]);
create_session_if_not_set_then(Edition::Edition2024, |_| {
let id0 = StableCrateId::new(Symbol::intern("foo"), false, vec!["1".to_string()]);
let id1 = StableCrateId::new(Symbol::intern("foo"), false, vec!["2".to_string()]);

let h0 = mk_test_hash(id0);
let h1 = mk_test_hash(id1);
let h0 = mk_test_hash(id0);
let h1 = mk_test_hash(id1);

assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
assert_ne!(h0.local_hash(), h1.local_hash());
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
assert_ne!(h0.local_hash(), h1.local_hash());

fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
let parent_hash = DefPathHash::new(stable_crate_id, 0);
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
let parent_hash = DefPathHash::new(stable_crate_id, 0);

let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
data: DefPathData::CrateRoot,
disambiguator: 0,
},
};
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
data: DefPathData::CrateRoot,
disambiguator: 0,
},
};

key.compute_stable_hash(parent_hash)
}
key.compute_stable_hash(parent_hash)
}
})
}
5 changes: 3 additions & 2 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ use rustc_data_structures::{base_n, flock};
use rustc_errors::ErrorGuaranteed;
use rustc_fs_util::{link_or_copy, LinkOrCopy};
use rustc_session::{Session, StableCrateId};
use rustc_span::Symbol;

use std::fs as std_fs;
use std::io::{self, ErrorKind};
Expand Down Expand Up @@ -202,7 +203,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
pub fn prepare_session_directory(
sess: &Session,
crate_name: &str,
crate_name: Symbol,
stable_crate_id: StableCrateId,
) -> Result<(), ErrorGuaranteed> {
if sess.opts.incremental.is_none() {
Expand Down Expand Up @@ -657,7 +658,7 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
Ok(UNIX_EPOCH + duration)
}

fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
fn crate_path(sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId) -> PathBuf {
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();

let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);
Expand Down
40 changes: 21 additions & 19 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn create_resolver(
sess: Lrc<Session>,
metadata_loader: Box<MetadataLoaderDyn>,
krate: &ast::Crate,
crate_name: &str,
crate_name: Symbol,
) -> BoxedResolver {
trace!("create_resolver");
BoxedResolver::new(sess, move |sess, resolver_arenas| {
Expand All @@ -171,7 +171,7 @@ pub fn register_plugins<'a>(
metadata_loader: &'a dyn MetadataLoader,
register_lints: impl Fn(&Session, &mut LintStore),
mut krate: ast::Crate,
crate_name: &str,
crate_name: Symbol,
) -> Result<(ast::Crate, LintStore)> {
krate = sess.time("attributes_injection", || {
rustc_builtin_macros::cmdline_attrs::inject(
Expand Down Expand Up @@ -228,19 +228,21 @@ fn pre_expansion_lint<'a>(
lint_store: &LintStore,
registered_tools: &RegisteredTools,
check_node: impl EarlyCheckNode<'a>,
node_name: &str,
node_name: Symbol,
) {
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name).run(|| {
rustc_lint::check_ast_node(
sess,
true,
lint_store,
registered_tools,
None,
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
check_node,
);
});
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
|| {
rustc_lint::check_ast_node(
sess,
true,
lint_store,
registered_tools,
None,
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
check_node,
);
},
);
}

// Cannot implement directly for `LintStore` due to trait coherence.
Expand All @@ -254,7 +256,7 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
node_id: ast::NodeId,
attrs: &[ast::Attribute],
items: &[rustc_ast::ptr::P<ast::Item>],
name: &str,
name: Symbol,
) {
pre_expansion_lint(sess, self.0, registered_tools, (node_id, attrs, items), name);
}
Expand All @@ -268,7 +270,7 @@ pub fn configure_and_expand(
sess: &Session,
lint_store: &LintStore,
mut krate: ast::Crate,
crate_name: &str,
crate_name: Symbol,
resolver: &mut Resolver<'_>,
) -> Result<ast::Crate> {
trace!("configure_and_expand");
Expand Down Expand Up @@ -462,7 +464,7 @@ fn generated_output_paths(
sess: &Session,
outputs: &OutputFilenames,
exact_name: bool,
crate_name: &str,
crate_name: Symbol,
) -> Vec<PathBuf> {
let mut out_filenames = Vec::new();
for output_type in sess.opts.output_types.keys() {
Expand Down Expand Up @@ -661,7 +663,7 @@ pub fn prepare_outputs(
compiler: &Compiler,
krate: &ast::Crate,
boxed_resolver: &RefCell<BoxedResolver>,
crate_name: &str,
crate_name: Symbol,
) -> Result<OutputFilenames> {
let _timer = sess.timer("prepare_outputs");

Expand Down Expand Up @@ -771,7 +773,7 @@ pub fn create_global_ctxt<'tcx>(
dep_graph: DepGraph,
resolver: Rc<RefCell<BoxedResolver>>,
outputs: OutputFilenames,
crate_name: &str,
crate_name: Symbol,
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_query_impl::Queries as TcxQueries;
use rustc_session::config::{self, OutputFilenames, OutputType};
use rustc_session::{output::find_crate_name, Session};
use rustc_span::symbol::sym;
use rustc_span::Symbol;
use std::any::Any;
use std::cell::{Ref, RefCell, RefMut};
use std::rc::Rc;
Expand Down Expand Up @@ -74,7 +75,7 @@ pub struct Queries<'tcx> {

dep_graph_future: Query<Option<DepGraphFuture>>,
parse: Query<ast::Crate>,
crate_name: Query<String>,
crate_name: Query<Symbol>,
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
dep_graph: Query<DepGraph>,
Expand Down Expand Up @@ -135,7 +136,7 @@ impl<'tcx> Queries<'tcx> {
&*self.codegen_backend().metadata_loader(),
self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
krate,
&crate_name,
crate_name,
)?;

// Compute the dependency graph (in the background). We want to do
Expand All @@ -149,7 +150,7 @@ impl<'tcx> Queries<'tcx> {
})
}

pub fn crate_name(&self) -> Result<&Query<String>> {
pub fn crate_name(&self) -> Result<&Query<Symbol>> {
self.crate_name.compute(|| {
Ok({
let parse_result = self.parse()?;
Expand All @@ -165,18 +166,18 @@ impl<'tcx> Queries<'tcx> {
) -> Result<&Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>> {
trace!("expansion");
self.expansion.compute(|| {
let crate_name = self.crate_name()?.peek().clone();
let crate_name = *self.crate_name()?.peek();
let (krate, lint_store) = self.register_plugins()?.take();
let _timer = self.session().timer("configure_and_expand");
let sess = self.session();
let mut resolver = passes::create_resolver(
sess.clone(),
self.codegen_backend().metadata_loader(),
&krate,
&crate_name,
crate_name,
);
let krate = resolver.access(|resolver| {
passes::configure_and_expand(sess, &lint_store, krate, &crate_name, resolver)
passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver)
})?;
Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
})
Expand All @@ -201,20 +202,20 @@ impl<'tcx> Queries<'tcx> {
pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
self.prepare_outputs.compute(|| {
let (krate, boxed_resolver, _) = &*self.expansion()?.peek();
let crate_name = self.crate_name()?.peek();
let crate_name = *self.crate_name()?.peek();
passes::prepare_outputs(
self.session(),
self.compiler,
krate,
&*boxed_resolver,
&crate_name,
crate_name,
)
})
}

pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
self.global_ctxt.compute(|| {
let crate_name = self.crate_name()?.peek().clone();
let crate_name = *self.crate_name()?.peek();
let outputs = self.prepare_outputs()?.take();
let dep_graph = self.dep_graph()?.peek().clone();
let (krate, resolver, lint_store) = self.expansion()?.take();
Expand All @@ -225,7 +226,7 @@ impl<'tcx> Queries<'tcx> {
dep_graph,
resolver,
outputs,
&crate_name,
crate_name,
&self.queries,
&self.gcx,
&self.arena,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ impl<'a> CrateLoader<'a> {
pub fn new(
sess: &'a Session,
metadata_loader: Box<MetadataLoaderDyn>,
local_crate_name: &str,
local_crate_name: Symbol,
) -> Self {
let mut stable_crate_ids = FxHashMap::default();
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);

CrateLoader {
sess,
metadata_loader,
local_crate_name: Symbol::intern(local_crate_name),
local_crate_name,
cstore: CStore {
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
// order to make array indices in `metas` match with the
Expand Down Expand Up @@ -1000,7 +1000,7 @@ impl<'a> CrateLoader<'a> {
);
let name = match orig_name {
Some(orig_name) => {
validate_crate_name(self.sess, orig_name.as_str(), Some(item.span));
validate_crate_name(self.sess, orig_name, Some(item.span));
orig_name
}
None => item.ident.name,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_metadata/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) {
.unwrap_or(MetadataKind::None);

let crate_name = tcx.crate_name(LOCAL_CRATE);
let out_filename =
filename_for_metadata(tcx.sess, crate_name.as_str(), tcx.output_filenames(()));
let out_filename = filename_for_metadata(tcx.sess, crate_name, tcx.output_filenames(()));
// To avoid races with another rustc process scanning the output directory,
// we need to write the file somewhere else and atomically move it to its
// final destination, with an `fs::rename` call. In order for the rename to
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ impl<'tcx> TyCtxt<'tcx> {
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
queries: &'tcx dyn query::QueryEngine<'tcx>,
query_kinds: &'tcx [DepKindStruct<'tcx>],
crate_name: &str,
crate_name: Symbol,
output_filenames: OutputFilenames,
) -> GlobalCtxt<'tcx> {
let data_layout = s.target.parse_data_layout().unwrap_or_else(|err| {
Expand Down Expand Up @@ -1321,7 +1321,7 @@ impl<'tcx> TyCtxt<'tcx> {
pred_rcache: Default::default(),
selection_cache: Default::default(),
evaluation_cache: Default::default(),
crate_name: Symbol::intern(crate_name),
crate_name,
data_layout,
alloc_map: Lock::new(interpret::AllocMap::new()),
output_filenames: Arc::new(output_filenames),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ impl<'a> Resolver<'a> {
pub fn new(
session: &'a Session,
krate: &Crate,
crate_name: &str,
crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>,
arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a> {
Expand Down
Loading