Skip to content

Commit fbfc5ad

Browse files
authored
Rollup merge of #105423 - oli-obk:symbols, r=jackh726
Use `Symbol` for the crate name instead of `String`/`str` It always got converted to a symbol anyway
2 parents 2fbde2b + d30848b commit fbfc5ad

File tree

18 files changed

+103
-92
lines changed

18 files changed

+103
-92
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn link_binary<'a>(
102102
sess,
103103
crate_type,
104104
outputs,
105-
codegen_results.crate_info.local_crate_name.as_str(),
105+
codegen_results.crate_info.local_crate_name,
106106
);
107107
match crate_type {
108108
CrateType::Rlib => {

compiler/rustc_driver/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_data_structures::sync::SeqCst;
2525
use rustc_errors::registry::{InvalidErrorCode, Registry};
2626
use rustc_errors::{ErrorGuaranteed, PResult};
2727
use rustc_feature::find_gated_cfg;
28+
use rustc_hir::def_id::LOCAL_CRATE;
2829
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
2930
use rustc_interface::{interface, Queries};
3031
use rustc_lint::LintStore;
@@ -374,14 +375,14 @@ fn run_compiler(
374375
queries.global_ctxt()?.peek_mut().enter(|tcx| {
375376
let result = tcx.analysis(());
376377
if sess.opts.unstable_opts.save_analysis {
377-
let crate_name = queries.crate_name()?.peek().clone();
378+
let crate_name = tcx.crate_name(LOCAL_CRATE);
378379
sess.time("save_analysis", || {
379380
save::process_crate(
380381
tcx,
381-
&crate_name,
382+
crate_name,
382383
compiler.input(),
383384
None,
384-
DumpHandler::new(compiler.output_dir().as_deref(), &crate_name),
385+
DumpHandler::new(compiler.output_dir().as_deref(), crate_name),
385386
)
386387
});
387388
}
@@ -678,7 +679,7 @@ fn print_crate_info(
678679
let crate_types = collect_crate_types(sess, attrs);
679680
for &style in &crate_types {
680681
let fname =
681-
rustc_session::output::filename_for_input(sess, style, &id, &t_outputs);
682+
rustc_session::output::filename_for_input(sess, style, id, &t_outputs);
682683
println!("{}", fname.file_name().unwrap().to_string_lossy());
683684
}
684685
}

compiler/rustc_expand/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ pub trait LintStoreExpand {
960960
node_id: NodeId,
961961
attrs: &[Attribute],
962962
items: &[P<Item>],
963-
name: &str,
963+
name: Symbol,
964964
);
965965
}
966966

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ impl InvocationCollectorNode for P<ast::Item> {
11221122
ecx.current_expansion.lint_node_id,
11231123
&attrs,
11241124
&items,
1125-
ident.name.as_str(),
1125+
ident.name,
11261126
);
11271127
}
11281128

compiler/rustc_hir/src/tests.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
22
use rustc_span::def_id::{DefPathHash, StableCrateId};
3+
use rustc_span::edition::Edition;
4+
use rustc_span::{create_session_if_not_set_then, Symbol};
35

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

14-
let id0 = StableCrateId::new("foo", false, vec!["1".to_string()]);
15-
let id1 = StableCrateId::new("foo", false, vec!["2".to_string()]);
16+
create_session_if_not_set_then(Edition::Edition2024, |_| {
17+
let id0 = StableCrateId::new(Symbol::intern("foo"), false, vec!["1".to_string()]);
18+
let id1 = StableCrateId::new(Symbol::intern("foo"), false, vec!["2".to_string()]);
1619

17-
let h0 = mk_test_hash(id0);
18-
let h1 = mk_test_hash(id1);
20+
let h0 = mk_test_hash(id0);
21+
let h1 = mk_test_hash(id1);
1922

20-
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
21-
assert_ne!(h0.local_hash(), h1.local_hash());
23+
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
24+
assert_ne!(h0.local_hash(), h1.local_hash());
2225

23-
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
24-
let parent_hash = DefPathHash::new(stable_crate_id, 0);
26+
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
27+
let parent_hash = DefPathHash::new(stable_crate_id, 0);
2528

26-
let key = DefKey {
27-
parent: None,
28-
disambiguated_data: DisambiguatedDefPathData {
29-
data: DefPathData::CrateRoot,
30-
disambiguator: 0,
31-
},
32-
};
29+
let key = DefKey {
30+
parent: None,
31+
disambiguated_data: DisambiguatedDefPathData {
32+
data: DefPathData::CrateRoot,
33+
disambiguator: 0,
34+
},
35+
};
3336

34-
key.compute_stable_hash(parent_hash)
35-
}
37+
key.compute_stable_hash(parent_hash)
38+
}
39+
})
3640
}

compiler/rustc_incremental/src/persist/fs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ use rustc_data_structures::{base_n, flock};
109109
use rustc_errors::ErrorGuaranteed;
110110
use rustc_fs_util::{link_or_copy, LinkOrCopy};
111111
use rustc_session::{Session, StableCrateId};
112+
use rustc_span::Symbol;
112113

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

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

663664
let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);

compiler/rustc_interface/src/passes.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn create_resolver(
158158
sess: Lrc<Session>,
159159
metadata_loader: Box<MetadataLoaderDyn>,
160160
krate: &ast::Crate,
161-
crate_name: &str,
161+
crate_name: Symbol,
162162
) -> BoxedResolver {
163163
trace!("create_resolver");
164164
BoxedResolver::new(sess, move |sess, resolver_arenas| {
@@ -171,7 +171,7 @@ pub fn register_plugins<'a>(
171171
metadata_loader: &'a dyn MetadataLoader,
172172
register_lints: impl Fn(&Session, &mut LintStore),
173173
mut krate: ast::Crate,
174-
crate_name: &str,
174+
crate_name: Symbol,
175175
) -> Result<(ast::Crate, LintStore)> {
176176
krate = sess.time("attributes_injection", || {
177177
rustc_builtin_macros::cmdline_attrs::inject(
@@ -228,19 +228,21 @@ fn pre_expansion_lint<'a>(
228228
lint_store: &LintStore,
229229
registered_tools: &RegisteredTools,
230230
check_node: impl EarlyCheckNode<'a>,
231-
node_name: &str,
231+
node_name: Symbol,
232232
) {
233-
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name).run(|| {
234-
rustc_lint::check_ast_node(
235-
sess,
236-
true,
237-
lint_store,
238-
registered_tools,
239-
None,
240-
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
241-
check_node,
242-
);
243-
});
233+
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
234+
|| {
235+
rustc_lint::check_ast_node(
236+
sess,
237+
true,
238+
lint_store,
239+
registered_tools,
240+
None,
241+
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
242+
check_node,
243+
);
244+
},
245+
);
244246
}
245247

246248
// Cannot implement directly for `LintStore` due to trait coherence.
@@ -254,7 +256,7 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
254256
node_id: ast::NodeId,
255257
attrs: &[ast::Attribute],
256258
items: &[rustc_ast::ptr::P<ast::Item>],
257-
name: &str,
259+
name: Symbol,
258260
) {
259261
pre_expansion_lint(sess, self.0, registered_tools, (node_id, attrs, items), name);
260262
}
@@ -268,7 +270,7 @@ pub fn configure_and_expand(
268270
sess: &Session,
269271
lint_store: &LintStore,
270272
mut krate: ast::Crate,
271-
crate_name: &str,
273+
crate_name: Symbol,
272274
resolver: &mut Resolver<'_>,
273275
) -> Result<ast::Crate> {
274276
trace!("configure_and_expand");
@@ -462,7 +464,7 @@ fn generated_output_paths(
462464
sess: &Session,
463465
outputs: &OutputFilenames,
464466
exact_name: bool,
465-
crate_name: &str,
467+
crate_name: Symbol,
466468
) -> Vec<PathBuf> {
467469
let mut out_filenames = Vec::new();
468470
for output_type in sess.opts.output_types.keys() {
@@ -661,7 +663,7 @@ pub fn prepare_outputs(
661663
compiler: &Compiler,
662664
krate: &ast::Crate,
663665
boxed_resolver: &RefCell<BoxedResolver>,
664-
crate_name: &str,
666+
crate_name: Symbol,
665667
) -> Result<OutputFilenames> {
666668
let _timer = sess.timer("prepare_outputs");
667669

@@ -771,7 +773,7 @@ pub fn create_global_ctxt<'tcx>(
771773
dep_graph: DepGraph,
772774
resolver: Rc<RefCell<BoxedResolver>>,
773775
outputs: OutputFilenames,
774-
crate_name: &str,
776+
crate_name: Symbol,
775777
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
776778
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
777779
arena: &'tcx WorkerLocal<Arena<'tcx>>,

compiler/rustc_interface/src/queries.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_query_impl::Queries as TcxQueries;
1717
use rustc_session::config::{self, OutputFilenames, OutputType};
1818
use rustc_session::{output::find_crate_name, Session};
1919
use rustc_span::symbol::sym;
20+
use rustc_span::Symbol;
2021
use std::any::Any;
2122
use std::cell::{Ref, RefCell, RefMut};
2223
use std::rc::Rc;
@@ -74,7 +75,7 @@ pub struct Queries<'tcx> {
7475

7576
dep_graph_future: Query<Option<DepGraphFuture>>,
7677
parse: Query<ast::Crate>,
77-
crate_name: Query<String>,
78+
crate_name: Query<Symbol>,
7879
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
7980
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
8081
dep_graph: Query<DepGraph>,
@@ -135,7 +136,7 @@ impl<'tcx> Queries<'tcx> {
135136
&*self.codegen_backend().metadata_loader(),
136137
self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
137138
krate,
138-
&crate_name,
139+
crate_name,
139140
)?;
140141

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

152-
pub fn crate_name(&self) -> Result<&Query<String>> {
153+
pub fn crate_name(&self) -> Result<&Query<Symbol>> {
153154
self.crate_name.compute(|| {
154155
Ok({
155156
let parse_result = self.parse()?;
@@ -165,18 +166,18 @@ impl<'tcx> Queries<'tcx> {
165166
) -> Result<&Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>> {
166167
trace!("expansion");
167168
self.expansion.compute(|| {
168-
let crate_name = self.crate_name()?.peek().clone();
169+
let crate_name = *self.crate_name()?.peek();
169170
let (krate, lint_store) = self.register_plugins()?.take();
170171
let _timer = self.session().timer("configure_and_expand");
171172
let sess = self.session();
172173
let mut resolver = passes::create_resolver(
173174
sess.clone(),
174175
self.codegen_backend().metadata_loader(),
175176
&krate,
176-
&crate_name,
177+
crate_name,
177178
);
178179
let krate = resolver.access(|resolver| {
179-
passes::configure_and_expand(sess, &lint_store, krate, &crate_name, resolver)
180+
passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver)
180181
})?;
181182
Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
182183
})
@@ -201,20 +202,20 @@ impl<'tcx> Queries<'tcx> {
201202
pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
202203
self.prepare_outputs.compute(|| {
203204
let (krate, boxed_resolver, _) = &*self.expansion()?.peek();
204-
let crate_name = self.crate_name()?.peek();
205+
let crate_name = *self.crate_name()?.peek();
205206
passes::prepare_outputs(
206207
self.session(),
207208
self.compiler,
208209
krate,
209210
&*boxed_resolver,
210-
&crate_name,
211+
crate_name,
211212
)
212213
})
213214
}
214215

215216
pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
216217
self.global_ctxt.compute(|| {
217-
let crate_name = self.crate_name()?.peek().clone();
218+
let crate_name = *self.crate_name()?.peek();
218219
let outputs = self.prepare_outputs()?.take();
219220
let dep_graph = self.dep_graph()?.peek().clone();
220221
let (krate, resolver, lint_store) = self.expansion()?.take();
@@ -225,7 +226,7 @@ impl<'tcx> Queries<'tcx> {
225226
dep_graph,
226227
resolver,
227228
outputs,
228-
&crate_name,
229+
crate_name,
229230
&self.queries,
230231
&self.gcx,
231232
&self.arena,

compiler/rustc_metadata/src/creader.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ impl<'a> CrateLoader<'a> {
245245
pub fn new(
246246
sess: &'a Session,
247247
metadata_loader: Box<MetadataLoaderDyn>,
248-
local_crate_name: &str,
248+
local_crate_name: Symbol,
249249
) -> Self {
250250
let mut stable_crate_ids = FxHashMap::default();
251251
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
252252

253253
CrateLoader {
254254
sess,
255255
metadata_loader,
256-
local_crate_name: Symbol::intern(local_crate_name),
256+
local_crate_name,
257257
cstore: CStore {
258258
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
259259
// order to make array indices in `metas` match with the
@@ -1000,7 +1000,7 @@ impl<'a> CrateLoader<'a> {
10001000
);
10011001
let name = match orig_name {
10021002
Some(orig_name) => {
1003-
validate_crate_name(self.sess, orig_name.as_str(), Some(item.span));
1003+
validate_crate_name(self.sess, orig_name, Some(item.span));
10041004
orig_name
10051005
}
10061006
None => item.ident.name,

compiler/rustc_metadata/src/fs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) {
6161
.unwrap_or(MetadataKind::None);
6262

6363
let crate_name = tcx.crate_name(LOCAL_CRATE);
64-
let out_filename =
65-
filename_for_metadata(tcx.sess, crate_name.as_str(), tcx.output_filenames(()));
64+
let out_filename = filename_for_metadata(tcx.sess, crate_name, tcx.output_filenames(()));
6665
// To avoid races with another rustc process scanning the output directory,
6766
// we need to write the file somewhere else and atomically move it to its
6867
// final destination, with an `fs::rename` call. In order for the rename to

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ impl<'tcx> TyCtxt<'tcx> {
12851285
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
12861286
queries: &'tcx dyn query::QueryEngine<'tcx>,
12871287
query_kinds: &'tcx [DepKindStruct<'tcx>],
1288-
crate_name: &str,
1288+
crate_name: Symbol,
12891289
output_filenames: OutputFilenames,
12901290
) -> GlobalCtxt<'tcx> {
12911291
let data_layout = s.target.parse_data_layout().unwrap_or_else(|err| {
@@ -1325,7 +1325,7 @@ impl<'tcx> TyCtxt<'tcx> {
13251325
pred_rcache: Default::default(),
13261326
selection_cache: Default::default(),
13271327
evaluation_cache: Default::default(),
1328-
crate_name: Symbol::intern(crate_name),
1328+
crate_name,
13291329
data_layout,
13301330
alloc_map: Lock::new(interpret::AllocMap::new()),
13311331
output_filenames: Arc::new(output_filenames),

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ impl<'a> Resolver<'a> {
11961196
pub fn new(
11971197
session: &'a Session,
11981198
krate: &Crate,
1199-
crate_name: &str,
1199+
crate_name: Symbol,
12001200
metadata_loader: Box<MetadataLoaderDyn>,
12011201
arenas: &'a ResolverArenas<'a>,
12021202
) -> Resolver<'a> {

0 commit comments

Comments
 (0)