Skip to content

Commit 8d006c0

Browse files
committed
Auto merge of #80036 - sivadeilra:syms_no_macro, r=petrochenkov
Stop using intermediate macros in definition of symbols Currently, the rustc_macros::symbols macro generates two `macro_rules!` macros as its output. These two macros are used in rustc_span/src/symbol.rs. This means that each Symbol that we define is represented in the AST of rustc_symbols twice: once in the definition of the `define_symbols!` macro (similarly for the `keywords! macro), and once in the rustc_span::symbols definition. That would be OK if there were only a handful of symbols, but currently we define over 1100 symbols. The definition of the `define_symbols!` macro contains the expanded definition of each symbol, so that's a lot of AST storage wasted on a macro that is used exactly once. This commit removes the `define_symbols` macro, and simply allows the proc macro to directly generate the `rustc_symbols::symbol::sym` module. The benefit is mainly in reducing memory wasted during compilation of rustc itself. It should also reduce memory used by Rust Analyzer. This commit also reduces the size of the AST for symbol definitions, by moving two `#[allow(...)]` attributes from the symbol constants to the `sym` module. This eliminates 2200+ attribute nodes. This commit also eliminates the need for the `digits_array` constant. There's no need to store an array of Symbol values for digits. We can simply define a constant of the base value, and add to that base value. I left the `sym::integer` function in rustc_span/src/symbol.rs instead of moving it into rustc_macros/src/symbols.rs for two reasons. First, because it's human-written code; it doesn't need to be generated by the proc-macro. Second, because I didn't want the `#[allow(...)]` attributes that I moved to the `sym` module scope to apply to this function. The `sym` module re-exports the `integer` function from its parent module.
2 parents f3800db + 2b2462e commit 8d006c0

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

compiler/rustc_macros/src/symbols.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
126126

127127
let mut keyword_stream = quote! {};
128128
let mut symbols_stream = quote! {};
129-
let mut digits_stream = quote! {};
130129
let mut prefill_stream = quote! {};
131130
let mut counter = 0u32;
132131
let mut keys =
@@ -162,7 +161,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
162161
#value,
163162
});
164163
keyword_stream.extend(quote! {
165-
#[allow(non_upper_case_globals)]
166164
pub const #name: Symbol = Symbol::new(#counter);
167165
});
168166
counter += 1;
@@ -182,42 +180,39 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
182180
#value,
183181
});
184182
symbols_stream.extend(quote! {
185-
#[allow(rustc::default_hash_types)]
186-
#[allow(non_upper_case_globals)]
187183
pub const #name: Symbol = Symbol::new(#counter);
188184
});
189185
counter += 1;
190186
}
191187

192188
// Generate symbols for the strings "0", "1", ..., "9".
189+
let digits_base = counter;
190+
counter += 10;
193191
for n in 0..10 {
194192
let n = n.to_string();
195193
check_dup(Span::call_site(), &n, &mut errors);
196194
prefill_stream.extend(quote! {
197195
#n,
198196
});
199-
digits_stream.extend(quote! {
200-
Symbol::new(#counter),
201-
});
202-
counter += 1;
203197
}
198+
let _ = counter; // for future use
204199

205200
let output = quote! {
206-
macro_rules! keywords {
207-
() => {
208-
#keyword_stream
209-
}
210-
}
201+
const SYMBOL_DIGITS_BASE: u32 = #digits_base;
211202

212-
macro_rules! define_symbols {
213-
() => {
214-
#symbols_stream
203+
#[doc(hidden)]
204+
#[allow(non_upper_case_globals)]
205+
mod kw_generated {
206+
use super::Symbol;
207+
#keyword_stream
208+
}
215209

216-
#[allow(non_upper_case_globals)]
217-
pub const digits_array: &[Symbol; 10] = &[
218-
#digits_stream
219-
];
220-
}
210+
#[allow(rustc::default_hash_types)]
211+
#[allow(non_upper_case_globals)]
212+
#[doc(hidden)]
213+
pub mod sym_generated {
214+
use super::Symbol;
215+
#symbols_stream
221216
}
222217

223218
impl Interner {

compiler/rustc_span/src/symbol.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1577,31 +1577,31 @@ impl Interner {
15771577
/// Given that `kw` is imported, use them like `kw::keyword_name`.
15781578
/// For example `kw::Loop` or `kw::Break`.
15791579
pub mod kw {
1580-
use super::Symbol;
1581-
keywords!();
1580+
pub use super::kw_generated::*;
15821581
}
15831582

15841583
// This module has a very short name because it's used a lot.
15851584
/// This module contains all the defined non-keyword `Symbol`s.
15861585
///
15871586
/// Given that `sym` is imported, use them like `sym::symbol_name`.
15881587
/// For example `sym::rustfmt` or `sym::u8`.
1589-
#[allow(rustc::default_hash_types)]
15901588
pub mod sym {
15911589
use super::Symbol;
15921590
use std::convert::TryInto;
15931591

1594-
define_symbols!();
1592+
pub use super::sym_generated::*;
15951593

15961594
// Used from a macro in `librustc_feature/accepted.rs`
15971595
pub use super::kw::MacroRules as macro_rules;
15981596

1599-
// Get the symbol for an integer. The first few non-negative integers each
1600-
// have a static symbol and therefore are fast.
1597+
/// Get the symbol for an integer.
1598+
///
1599+
/// The first few non-negative integers each have a static symbol and therefore
1600+
/// are fast.
16011601
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
16021602
if let Result::Ok(idx) = n.try_into() {
1603-
if let Option::Some(&sym_) = digits_array.get(idx) {
1604-
return sym_;
1603+
if idx < 10 {
1604+
return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
16051605
}
16061606
}
16071607
Symbol::intern(&n.to_string())

0 commit comments

Comments
 (0)