Skip to content

Commit ab7cf71

Browse files
committed
Auto merge of #61035 - nnethercote:avoid-more-symbol-interning, r=petrochenkov
Avoid more symbol interning r? @petrochenkov
2 parents be10e62 + 33a3206 commit ab7cf71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+299
-228
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3009,6 +3009,7 @@ dependencies = [
30093009
"rustc_cratesio_shim 0.0.0",
30103010
"rustc_data_structures 0.0.0",
30113011
"serialize 0.0.0",
3012+
"syntax_pos 0.0.0",
30123013
]
30133014

30143015
[[package]]

src/librustc/hir/lowering.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1145,9 +1145,7 @@ impl<'a> LoweringContext<'a> {
11451145
let unstable_span = self.sess.source_map().mark_span_with_reason(
11461146
CompilerDesugaringKind::Async,
11471147
span,
1148-
Some(vec![
1149-
Symbol::intern("gen_future"),
1150-
].into()),
1148+
Some(vec![sym::gen_future].into()),
11511149
);
11521150
let gen_future = self.expr_std_path(
11531151
unstable_span, &[sym::future, sym::from_generator], None, ThinVec::new());
@@ -2958,7 +2956,7 @@ impl<'a> LoweringContext<'a> {
29582956
ident: match f.ident {
29592957
Some(ident) => ident,
29602958
// FIXME(jseyfried): positional field hygiene
2961-
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
2959+
None => Ident::new(sym::integer(index), f.span),
29622960
},
29632961
vis: self.lower_visibility(&f.vis, None),
29642962
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),
@@ -4177,9 +4175,7 @@ impl<'a> LoweringContext<'a> {
41774175
let unstable_span = this.sess.source_map().mark_span_with_reason(
41784176
CompilerDesugaringKind::TryBlock,
41794177
body.span,
4180-
Some(vec![
4181-
Symbol::intern("try_trait"),
4182-
].into()),
4178+
Some(vec![sym::try_trait].into()),
41834179
);
41844180
let mut block = this.lower_block(body, true).into_inner();
41854181
let tail = block.expr.take().map_or_else(

src/librustc/hir/map/def_collector.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::session::CrateDisambiguator;
55
use syntax::ast::*;
66
use syntax::ext::hygiene::Mark;
77
use syntax::visit;
8-
use syntax::symbol::kw;
9-
use syntax::symbol::Symbol;
8+
use syntax::symbol::{kw, sym};
109
use syntax::parse::token::{self, Token};
1110
use syntax_pos::Span;
1211

@@ -221,7 +220,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
221220
_: &'a Generics, _: NodeId, _: Span) {
222221
for (index, field) in data.fields().iter().enumerate() {
223222
let name = field.ident.map(|ident| ident.name)
224-
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
223+
.unwrap_or_else(|| sym::integer(index));
225224
let def = self.create_def(field.id,
226225
DefPathData::ValueNs(name.as_interned_str()),
227226
field.span);

src/librustc/middle/lang_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
210210
pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
211211
attrs.iter().find_map(|attr| Some(match attr {
212212
_ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span),
213-
_ if attr.check_name(sym::panic_handler) => (Symbol::intern("panic_impl"), attr.span),
214-
_ if attr.check_name(sym::alloc_error_handler) => (Symbol::intern("oom"), attr.span),
213+
_ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span),
214+
_ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span),
215215
_ => return None,
216216
}))
217217
}

src/librustc/middle/mem_categorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13161316

13171317
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
13181318
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
1319-
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
1319+
let interior = InteriorField(FieldIndex(i, sym::integer(i)));
13201320
let subcmt = Rc::new(
13211321
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13221322
self.cat_pattern_(subcmt, &subpat, op)?;
@@ -1363,7 +1363,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13631363
};
13641364
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
13651365
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
1366-
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
1366+
let interior = InteriorField(FieldIndex(i, sym::integer(i)));
13671367
let subcmt = Rc::new(
13681368
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13691369
self.cat_pattern_(subcmt, &subpat, op)?;

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'a, 'tcx> Index<'tcx> {
437437
reason: Some(Symbol::intern(reason)),
438438
issue: 27812,
439439
},
440-
feature: Symbol::intern("rustc_private"),
440+
feature: sym::rustc_private,
441441
rustc_depr: None,
442442
const_stability: None,
443443
promotable: false,
@@ -880,7 +880,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
880880
// FIXME: only remove `libc` when `stdbuild` is active.
881881
// FIXME: remove special casing for `test`.
882882
remaining_lib_features.remove(&Symbol::intern("libc"));
883-
remaining_lib_features.remove(&Symbol::intern("test"));
883+
remaining_lib_features.remove(&sym::test);
884884

885885
let check_features =
886886
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &[_]| {

src/librustc/session/config.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::source_map::{FileName, FilePathMapping};
1919
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
2020
use syntax::parse::token;
2121
use syntax::parse;
22-
use syntax::symbol::Symbol;
22+
use syntax::symbol::{sym, Symbol};
2323
use syntax::feature_gate::UnstableFeatures;
2424
use errors::emitter::HumanReadableErrorType;
2525

@@ -1503,31 +1503,31 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
15031503
Some(Symbol::intern(vendor)),
15041504
));
15051505
if sess.target.target.options.has_elf_tls {
1506-
ret.insert((Symbol::intern("target_thread_local"), None));
1506+
ret.insert((sym::target_thread_local, None));
15071507
}
15081508
for &i in &[8, 16, 32, 64, 128] {
15091509
if i >= min_atomic_width && i <= max_atomic_width {
15101510
let s = i.to_string();
15111511
ret.insert((
1512-
Symbol::intern("target_has_atomic"),
1512+
sym::target_has_atomic,
15131513
Some(Symbol::intern(&s)),
15141514
));
15151515
if &s == wordsz {
15161516
ret.insert((
1517-
Symbol::intern("target_has_atomic"),
1517+
sym::target_has_atomic,
15181518
Some(Symbol::intern("ptr")),
15191519
));
15201520
}
15211521
}
15221522
}
15231523
if atomic_cas {
1524-
ret.insert((Symbol::intern("target_has_atomic"), Some(Symbol::intern("cas"))));
1524+
ret.insert((sym::target_has_atomic, Some(Symbol::intern("cas"))));
15251525
}
15261526
if sess.opts.debug_assertions {
15271527
ret.insert((Symbol::intern("debug_assertions"), None));
15281528
}
15291529
if sess.opts.crate_types.contains(&CrateType::ProcMacro) {
1530-
ret.insert((Symbol::intern("proc_macro"), None));
1530+
ret.insert((sym::proc_macro, None));
15311531
}
15321532
ret
15331533
}
@@ -1547,7 +1547,7 @@ pub fn build_configuration(sess: &Session, mut user_cfg: ast::CrateConfig) -> as
15471547
let default_cfg = default_configuration(sess);
15481548
// If the user wants a test runner, then add the test cfg
15491549
if sess.opts.test {
1550-
user_cfg.insert((Symbol::intern("test"), None));
1550+
user_cfg.insert((sym::test, None));
15511551
}
15521552
user_cfg.extend(default_cfg.iter().cloned());
15531553
user_cfg
@@ -2702,7 +2702,7 @@ mod tests {
27022702
use std::path::PathBuf;
27032703
use super::{Externs, OutputType, OutputTypes};
27042704
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
2705-
use syntax::symbol::Symbol;
2705+
use syntax::symbol::sym;
27062706
use syntax::edition::{Edition, DEFAULT_EDITION};
27072707
use syntax;
27082708
use super::Options;
@@ -2744,15 +2744,14 @@ mod tests {
27442744
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
27452745
let sess = build_session(sessopts, None, registry);
27462746
let cfg = build_configuration(&sess, to_crate_config(cfg));
2747-
assert!(cfg.contains(&(Symbol::intern("test"), None)));
2747+
assert!(cfg.contains(&(sym::test, None)));
27482748
});
27492749
}
27502750

27512751
// When the user supplies --test and --cfg test, don't implicitly add
27522752
// another --cfg test
27532753
#[test]
27542754
fn test_switch_implies_cfg_test_unless_cfg_test() {
2755-
use syntax::symbol::sym;
27562755
syntax::with_default_globals(|| {
27572756
let matches = &match optgroups().parse(&["--test".to_string(),
27582757
"--cfg=test".to_string()]) {

src/librustc_allocator/expand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
9191
call_site: item.span, // use the call site of the static
9292
def_site: None,
9393
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: Some(vec![
95-
Symbol::intern("rustc_attrs"),
96-
].into()),
94+
allow_internal_unstable: Some(vec![sym::rustc_attrs].into()),
9795
allow_internal_unsafe: false,
9896
local_inner_macros: false,
9997
edition: self.sess.edition,
@@ -223,7 +221,7 @@ impl AllocFnFactory<'_> {
223221
}
224222

225223
fn attrs(&self) -> Vec<Attribute> {
226-
let special = Symbol::intern("rustc_std_internal_symbol");
224+
let special = sym::rustc_std_internal_symbol;
227225
let special = self.cx.meta_word(self.span, special);
228226
vec![self.cx.attribute(self.span, special)]
229227
}

src/librustc_interface/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn add_configuration(
6868
sess: &Session,
6969
codegen_backend: &dyn CodegenBackend,
7070
) {
71-
let tf = Symbol::intern("target_feature");
71+
let tf = sym::target_feature;
7272

7373
cfg.extend(
7474
codegen_backend

src/librustc_lint/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,8 @@ impl KeywordIdents {
14391439
{
14401440
let next_edition = match cx.sess.edition() {
14411441
Edition::Edition2015 => {
1442-
match &ident.as_str()[..] {
1443-
"async" | "await" | "try" => Edition::Edition2018,
1442+
match ident.name {
1443+
kw::Async | kw::Await | kw::Try => Edition::Edition2018,
14441444

14451445
// rust-lang/rust#56327: Conservatively do not
14461446
// attempt to report occurrences of `dyn` within
@@ -1454,7 +1454,7 @@ impl KeywordIdents {
14541454
// its precise role in the parsed AST and thus are
14551455
// assured this is truly an attempt to use it as
14561456
// an identifier.
1457-
"dyn" if !under_macro => Edition::Edition2018,
1457+
kw::Dyn if !under_macro => Edition::Edition2018,
14581458

14591459
_ => return,
14601460
}

src/librustc_macros/src/symbols.rs

+20
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
9696

9797
let mut keyword_stream = quote! {};
9898
let mut symbols_stream = quote! {};
99+
let mut digits_stream = quote! {};
99100
let mut prefill_stream = quote! {};
100101
let mut counter = 0u32;
101102
let mut keys = HashSet::<String>::new();
@@ -106,6 +107,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
106107
}
107108
};
108109

110+
// Generate the listed keywords.
109111
for keyword in &input.keywords.0 {
110112
let name = &keyword.name;
111113
let value = &keyword.value;
@@ -119,6 +121,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
119121
counter += 1;
120122
}
121123

124+
// Generate the listed symbols.
122125
for symbol in &input.symbols.0 {
123126
let name = &symbol.name;
124127
let value = match &symbol.value {
@@ -135,6 +138,19 @@ pub fn symbols(input: TokenStream) -> TokenStream {
135138
counter += 1;
136139
}
137140

141+
// Generate symbols for the strings "0", "1", ..., "9".
142+
for n in 0..10 {
143+
let n = n.to_string();
144+
check_dup(&n);
145+
prefill_stream.extend(quote! {
146+
#n,
147+
});
148+
digits_stream.extend(quote! {
149+
Symbol::new(#counter),
150+
});
151+
counter += 1;
152+
}
153+
138154
let tt = TokenStream::from(quote! {
139155
macro_rules! keywords {
140156
() => {
@@ -145,6 +161,10 @@ pub fn symbols(input: TokenStream) -> TokenStream {
145161
macro_rules! symbols {
146162
() => {
147163
#symbols_stream
164+
165+
pub const digits_array: &[Symbol; 10] = &[
166+
#digits_stream
167+
];
148168
}
149169
}
150170

src/librustc_metadata/cstore_impl.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,7 @@ impl cstore::CStore {
431431
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
432432
let ext = SyntaxExtension::ProcMacro {
433433
expander: Box::new(BangProcMacro { client }),
434-
allow_internal_unstable: Some(vec![
435-
Symbol::intern("proc_macro_def_site"),
436-
].into()),
434+
allow_internal_unstable: Some(vec![sym::proc_macro_def_site].into()),
437435
edition: data.root.edition,
438436
};
439437
return LoadedMacro::ProcMacro(Lrc::new(ext));

src/librustc_mir/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Write;
22
use std::hash::Hash;
33
use std::ops::RangeInclusive;
44

5-
use syntax_pos::symbol::Symbol;
5+
use syntax_pos::symbol::{sym, Symbol};
66
use rustc::hir;
77
use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf, VariantIdx};
88
use rustc::ty;
@@ -188,7 +188,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, '
188188

189189
PathElem::ClosureVar(name.unwrap_or_else(|| {
190190
// Fall back to showing the field index.
191-
Symbol::intern(&field.to_string())
191+
sym::integer(field)
192192
}))
193193
}
194194

src/librustc_target/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ log = "0.4"
1515
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
1616
rustc_data_structures = { path = "../librustc_data_structures" }
1717
serialize = { path = "../libserialize" }
18+
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_target/abi/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt;
77
use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
88

99
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
10+
use syntax_pos::symbol::{sym, Symbol};
1011

1112
pub mod call;
1213

@@ -552,6 +553,13 @@ impl FloatTy {
552553
}
553554
}
554555

556+
pub fn to_symbol(self) -> Symbol {
557+
match self {
558+
FloatTy::F32 => sym::f32,
559+
FloatTy::F64 => sym::f64,
560+
}
561+
}
562+
555563
pub fn bit_width(self) -> usize {
556564
match self {
557565
FloatTy::F32 => 32,

src/librustc_typeck/check/mod.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -2697,16 +2697,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26972697

26982698
fn resolve_place_op(&self, op: PlaceOp, is_mut: bool) -> (Option<DefId>, ast::Ident) {
26992699
let (tr, name) = match (op, is_mut) {
2700-
(PlaceOp::Deref, false) =>
2701-
(self.tcx.lang_items().deref_trait(), "deref"),
2702-
(PlaceOp::Deref, true) =>
2703-
(self.tcx.lang_items().deref_mut_trait(), "deref_mut"),
2704-
(PlaceOp::Index, false) =>
2705-
(self.tcx.lang_items().index_trait(), "index"),
2706-
(PlaceOp::Index, true) =>
2707-
(self.tcx.lang_items().index_mut_trait(), "index_mut"),
2700+
(PlaceOp::Deref, false) => (self.tcx.lang_items().deref_trait(), sym::deref),
2701+
(PlaceOp::Deref, true) => (self.tcx.lang_items().deref_mut_trait(), sym::deref_mut),
2702+
(PlaceOp::Index, false) => (self.tcx.lang_items().index_trait(), sym::index),
2703+
(PlaceOp::Index, true) => (self.tcx.lang_items().index_mut_trait(), sym::index_mut),
27082704
};
2709-
(tr, ast::Ident::from_str(name))
2705+
(tr, ast::Ident::with_empty_ctxt(name))
27102706
}
27112707

27122708
fn try_overloaded_place_op(&self,
@@ -4948,7 +4944,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49484944
// This is less than ideal, it will not suggest a return type span on any
49494945
// method called `main`, regardless of whether it is actually the entry point,
49504946
// but it will still present it as the reason for the expected type.
4951-
Some((decl, ident, ident.name != Symbol::intern("main")))
4947+
Some((decl, ident, ident.name != sym::main))
49524948
}),
49534949
Node::TraitItem(&hir::TraitItem {
49544950
ident, node: hir::TraitItemKind::Method(hir::MethodSig {

0 commit comments

Comments
 (0)