Skip to content

Commit 09623a8

Browse files
committed
Auto merge of #152948 - GrigorenkoPV:sym-ascii, r=<try>
pre-intern single-letter `sym::[a-zA-Z]`
2 parents 99246f4 + abcdef0 commit 09623a8

10 files changed

Lines changed: 62 additions & 31 deletions

File tree

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn expand_deriving_debug(
3030
name: sym::fmt,
3131
generics: Bounds::empty(),
3232
explicit_self: true,
33-
nonself_args: vec![(fmtr, sym::f)],
33+
nonself_args: vec![(fmtr, sym::character('f'))],
3434
ret_ty: Path(path_std!(fmt::Result)),
3535
attributes: thin_vec![cx.attr_word(sym::inline, span)],
3636
fieldless_variants_strategy:

compiler/rustc_macros/src/symbols.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,14 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
189189
let mut keyword_stream = quote! {};
190190
let mut symbols_stream = quote! {};
191191
let mut prefill_stream = quote! {};
192-
let mut entries = Entries::with_capacity(input.keywords.len() + input.symbols.len() + 10);
192+
let prefill_ints = 0..=9;
193+
let prefill_letters = ('A'..='Z').chain('a'..='z');
194+
let mut entries = Entries::with_capacity(
195+
input.keywords.len()
196+
+ input.symbols.len()
197+
+ prefill_ints.clone().count()
198+
+ prefill_letters.clone().count(),
199+
);
193200

194201
// Generate the listed keywords.
195202
for keyword in input.keywords.iter() {
@@ -234,12 +241,11 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
234241
});
235242
}
236243

237-
// Generate symbols for the strings "0", "1", ..., "9".
238-
for n in 0..10 {
239-
let n = n.to_string();
240-
entries.insert(Span::call_site(), &n, &mut errors);
244+
// Generate symbols for ascii letters and digits
245+
for s in prefill_ints.map(|n| n.to_string()).chain(prefill_letters.map(|c| c.to_string())) {
246+
entries.insert(Span::call_site(), &s, &mut errors);
241247
prefill_stream.extend(quote! {
242-
#n,
248+
#s,
243249
});
244250
}
245251

@@ -285,9 +291,13 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
285291
}
286292

287293
let symbol_digits_base = entries.map["0"].idx;
294+
let symbol_uppercase_letters_base = entries.map["A"].idx;
295+
let symbol_lowercase_letters_base = entries.map["a"].idx;
288296
let predefined_symbols_count = entries.len();
289297
let output = quote! {
290298
const SYMBOL_DIGITS_BASE: u32 = #symbol_digits_base;
299+
const SYMBOL_UPPERCASE_LETTERS_BASE: u32 = #symbol_uppercase_letters_base;
300+
const SYMBOL_LOWERCASE_LETTERS_BASE: u32 = #symbol_lowercase_letters_base;
291301

292302
/// The number of predefined symbols; this is the first index for
293303
/// extra pre-interned symbols in an Interner created via

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<'a> Parser<'a> {
647647
// positive for a `cr#` that wasn't intended to start a c-string literal, but identifying
648648
// that in the parser requires unbounded lookahead, so we only add a hint to the existing
649649
// error rather than replacing it entirely.
650-
if ((self.prev_token == TokenKind::Ident(sym::c, IdentIsRaw::No)
650+
if ((self.prev_token == TokenKind::Ident(sym::character('c'), IdentIsRaw::No)
651651
&& matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
652652
|| (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)
653653
&& matches!(

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,7 @@ fn string_to_tts_1() {
23242324
let expected = TokenStream::new(vec![
23252325
TokenTree::token_alone(token::Ident(kw::Fn, IdentIsRaw::No), sp(0, 2)),
23262326
TokenTree::token_joint_hidden(
2327-
token::Ident(Symbol::intern("a"), IdentIsRaw::No),
2327+
token::Ident(sym::character('a'), IdentIsRaw::No),
23282328
sp(3, 4),
23292329
),
23302330
TokenTree::Delimited(
@@ -2335,7 +2335,7 @@ fn string_to_tts_1() {
23352335
Delimiter::Parenthesis,
23362336
TokenStream::new(vec![
23372337
TokenTree::token_joint(
2338-
token::Ident(Symbol::intern("b"), IdentIsRaw::No),
2338+
token::Ident(sym::character('b'), IdentIsRaw::No),
23392339
sp(5, 6),
23402340
),
23412341
TokenTree::token_alone(token::Colon, sp(6, 7)),
@@ -2355,7 +2355,7 @@ fn string_to_tts_1() {
23552355
Delimiter::Brace,
23562356
TokenStream::new(vec![
23572357
TokenTree::token_joint(
2358-
token::Ident(Symbol::intern("b"), IdentIsRaw::No),
2358+
token::Ident(sym::character('b'), IdentIsRaw::No),
23592359
sp(15, 16),
23602360
),
23612361
// `Alone` because the `;` is followed by whitespace.
@@ -2543,10 +2543,10 @@ fn look(p: &Parser<'_>, dist: usize, kind: rustc_ast::token::TokenKind) {
25432543
#[test]
25442544
fn look_ahead() {
25452545
create_default_session_globals_then(|| {
2546-
let sym_f = Symbol::intern("f");
2547-
let sym_x = Symbol::intern("x");
2546+
let sym_f = sym::character('f');
2547+
let sym_x = sym::character('x');
25482548
#[allow(non_snake_case)]
2549-
let sym_S = Symbol::intern("S");
2549+
let sym_S = sym::character('S');
25502550
let raw_no = IdentIsRaw::No;
25512551

25522552
let psess = ParseSess::new();
@@ -2618,10 +2618,10 @@ fn look_ahead() {
26182618
#[test]
26192619
fn look_ahead_non_outermost_stream() {
26202620
create_default_session_globals_then(|| {
2621-
let sym_f = Symbol::intern("f");
2622-
let sym_x = Symbol::intern("x");
2621+
let sym_f = sym::character('f');
2622+
let sym_x = sym::character('x');
26232623
#[allow(non_snake_case)]
2624-
let sym_S = Symbol::intern("S");
2624+
let sym_S = sym::character('S');
26252625
let raw_no = IdentIsRaw::No;
26262626

26272627
let psess = ParseSess::new();

compiler/rustc_parse/src/parser/tokenstream/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn test_is_empty() {
9292
create_default_session_globals_then(|| {
9393
let test0 = TokenStream::default();
9494
let test1 =
95-
TokenStream::token_alone(token::Ident(Symbol::intern("a"), IdentIsRaw::No), sp(0, 1));
95+
TokenStream::token_alone(token::Ident(sym::character('a'), IdentIsRaw::No), sp(0, 1));
9696
let test2 = string_to_ts("foo(bar::baz)");
9797

9898
assert_eq!(test0.is_empty(), true);

compiler/rustc_span/src/symbol.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ symbols! {
189189
BorrowMut,
190190
Break,
191191
BuildHasher,
192-
C,
193192
CStr,
194193
CallOnceFuture,
195194
CallRefFuture,
@@ -547,7 +546,6 @@ symbols! {
547546
built,
548547
builtin_syntax,
549548
bundle,
550-
c,
551549
c_dash_variadic,
552550
c_str_literals,
553551
c_unwind,
@@ -751,7 +749,6 @@ symbols! {
751749
custom_inner_attributes,
752750
custom_mir,
753751
custom_test_frameworks,
754-
d,
755752
d32,
756753
dead_code,
757754
dealloc,
@@ -845,7 +842,6 @@ symbols! {
845842
dyn_star,
846843
dyn_trait,
847844
dynamic_no_pic: "dynamic-no-pic",
848-
e,
849845
edition_panic,
850846
effective_target_features,
851847
effects,
@@ -912,7 +908,6 @@ symbols! {
912908
extern_weak,
913909
external,
914910
external_doc,
915-
f,
916911
f16,
917912
f16_nan,
918913
f16c_target_feature,
@@ -2770,6 +2765,15 @@ pub mod sym {
27702765
#[doc(inline)]
27712766
pub use super::sym_generated::*;
27722767

2768+
// use quite a lot in relation to C ABI
2769+
pub const C: Symbol = ascii_letter_digit('C').unwrap();
2770+
2771+
// RISC-V stuff
2772+
#[expect(non_upper_case_globals)]
2773+
pub const f: Symbol = ascii_letter_digit('f').unwrap();
2774+
#[expect(non_upper_case_globals)]
2775+
pub const d: Symbol = ascii_letter_digit('d').unwrap();
2776+
27732777
/// Get the symbol for an integer.
27742778
///
27752779
/// The first few non-negative integers each have a static symbol and therefore
@@ -2784,6 +2788,23 @@ pub mod sym {
27842788
let printed = buffer.format(n);
27852789
Symbol::intern(printed)
27862790
}
2791+
2792+
pub const fn ascii_letter_digit(c: char) -> Option<Symbol> {
2793+
let i = c as u32;
2794+
Option::Some(Symbol::new(match c {
2795+
'0'..='9' => super::SYMBOL_DIGITS_BASE + (i - '0' as u32),
2796+
'A'..='Z' => super::SYMBOL_UPPERCASE_LETTERS_BASE + (i - 'A' as u32),
2797+
'a'..='z' => super::SYMBOL_LOWERCASE_LETTERS_BASE + (i - 'a' as u32),
2798+
_ => return Option::None,
2799+
}))
2800+
}
2801+
2802+
pub fn character(c: char) -> Symbol {
2803+
ascii_letter_digit(c).unwrap_or_else(|| {
2804+
let mut buf: [u8; char::MAX_LEN_UTF8] = Default::default();
2805+
Symbol::intern(c.encode_utf8(&mut buf))
2806+
})
2807+
}
27872808
}
27882809

27892810
impl Symbol {

compiler/rustc_target/src/asm/riscv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl RiscVInlineAsmRegClass {
5555
}
5656

5757
pub(crate) fn is_e(target_features: &FxIndexSet<Symbol>) -> bool {
58-
target_features.contains(&sym::e)
58+
target_features.contains(&sym::character('e'))
5959
}
6060

6161
fn not_e(

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::error::TypeError;
1414
use rustc_middle::ty::{
1515
self, IsSuggestable, Region, Ty, TyCtxt, TypeVisitableExt as _, Upcast as _,
1616
};
17-
use rustc_span::{BytePos, ErrorGuaranteed, Span, Symbol, kw};
17+
use rustc_span::{BytePos, ErrorGuaranteed, Span, Symbol, kw, sym};
1818
use tracing::{debug, instrument};
1919

2020
use super::ObligationCauseAsDiagArg;
@@ -1431,9 +1431,9 @@ fn suggest_precise_capturing<'tcx>(
14311431
});
14321432
} else {
14331433
let mut next_fresh_param = || {
1434-
["T", "U", "V", "W", "X", "Y", "A", "B", "C"]
1434+
['T', 'U', 'V', 'W', 'X', 'Y', 'A', 'B', 'C']
14351435
.into_iter()
1436-
.map(Symbol::intern)
1436+
.map(sym::character)
14371437
.chain((0..).map(|i| Symbol::intern(&format!("T{i}"))))
14381438
.find(|s| captured_non_lifetimes.insert(*s))
14391439
.unwrap()

compiler/rustc_trait_selection/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{self as hir, AmbigArg, FnRetTy, GenericParamKind, Node};
1212
use rustc_macros::{Diagnostic, Subdiagnostic};
1313
use rustc_middle::ty::print::{PrintTraitRefExt as _, TraitRefPrintOnlyTraitPath};
1414
use rustc_middle::ty::{self, Binder, ClosureKind, FnSig, GenericArg, Region, Ty, TyCtxt};
15-
use rustc_span::{BytePos, Ident, Span, Symbol, kw};
15+
use rustc_span::{BytePos, Ident, Span, Symbol, kw, sym};
1616

1717
use crate::error_reporting::infer::ObligationCauseAsDiagArg;
1818
use crate::error_reporting::infer::need_type_info::UnderspecifiedArgKind;
@@ -2062,9 +2062,9 @@ pub fn impl_trait_overcapture_suggestion<'tcx>(
20622062
}
20632063

20642064
let mut next_fresh_param = || {
2065-
["T", "U", "V", "W", "X", "Y", "A", "B", "C"]
2065+
['T', 'U', 'V', 'W', 'X', 'Y', 'A', 'B', 'C']
20662066
.into_iter()
2067-
.map(Symbol::intern)
2067+
.map(sym::character)
20682068
.chain((0..).map(|i| Symbol::intern(&format!("T{i}"))))
20692069
.find(|s| captured_non_lifetimes.insert(*s))
20702070
.unwrap()

src/librustdoc/clean/types/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn is_same_generic() {
7575
use crate::formats::cache::Cache;
7676
create_default_session_globals_then(|| {
7777
let cache = Cache::new(false, false);
78-
let generic = Type::Generic(Symbol::intern("T"));
78+
let generic = Type::Generic(sym::character('T'));
7979
let unit = Type::Primitive(PrimitiveType::Unit);
8080
assert!(!generic.is_doc_subtype_of(&unit, &cache));
8181
assert!(unit.is_doc_subtype_of(&generic, &cache));

0 commit comments

Comments
 (0)