Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit af220c3

Browse files
authored
Rollup merge of rust-lang#138682 - Alexendoo:extra-symbols, r=fee1-dead
Allow drivers to supply a list of extra symbols to intern Allows adding new symbols as `const`s in external drivers, desirable in Clippy so we can use them in patterns to replace code like https://github.com/rust-lang/rust/blob/75530e9f72a1990ed2305e16fd51d02f47048f12/src/tools/clippy/clippy_lints/src/casts/cast_ptr_alignment.rs#L66 The Clippy change adds a couple symbols as a demo, the exact `clippy_utils` API and replacing other usages can be done on the Clippy side to minimise sync conflicts
2 parents 3a9ab8d + abe6e88 commit af220c3

File tree

22 files changed

+104
-47
lines changed

22 files changed

+104
-47
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
264264
hash_untracked_state: None,
265265
register_lints: None,
266266
override_queries: None,
267+
extra_symbols: Vec::new(),
267268
make_codegen_backend: None,
268269
registry: diagnostics_registry(),
269270
using_internal_features: &USING_INTERNAL_FEATURES,

compiler/rustc_hir/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn def_path_hash_depends_on_crate_id() {
1717
// the crate by changing the crate disambiguator (e.g. via bumping the
1818
// crate's version number).
1919

20-
create_session_globals_then(Edition::Edition2024, None, || {
20+
create_session_globals_then(Edition::Edition2024, &[], None, || {
2121
let id0 = StableCrateId::new(Symbol::intern("foo"), false, vec!["1".to_string()], "");
2222
let id1 = StableCrateId::new(Symbol::intern("foo"), false, vec!["2".to_string()], "");
2323

compiler/rustc_interface/src/interface.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ pub struct Config {
348348
/// the list of queries.
349349
pub override_queries: Option<fn(&Session, &mut Providers)>,
350350

351+
/// An extra set of symbols to add to the symbol interner, the symbol indices
352+
/// will start at [`PREDEFINED_SYMBOLS_COUNT`](rustc_span::symbol::PREDEFINED_SYMBOLS_COUNT)
353+
pub extra_symbols: Vec<&'static str>,
354+
351355
/// This is a callback from the driver that is called to create a codegen backend.
352356
///
353357
/// Has no uses within this repository, but is used by bjorn3 for "the
@@ -409,6 +413,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
409413
&early_dcx,
410414
config.opts.edition,
411415
config.opts.unstable_opts.threads,
416+
&config.extra_symbols,
412417
SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
413418
|current_gcx| {
414419
// The previous `early_dcx` can't be reused here because it doesn't

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
checksum_hash_kind,
5454
});
5555

56-
rustc_span::create_session_globals_then(DEFAULT_EDITION, sm_inputs, || {
56+
rustc_span::create_session_globals_then(DEFAULT_EDITION, &[], sm_inputs, || {
5757
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
5858
let io = CompilerIO {
5959
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },

compiler/rustc_interface/src/util.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
117117
thread_stack_size: usize,
118118
edition: Edition,
119119
sm_inputs: SourceMapInputs,
120+
extra_symbols: &[&'static str],
120121
f: F,
121122
) -> R {
122123
// The "thread pool" is a single spawned thread in the non-parallel
@@ -134,9 +135,12 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
134135
// name contains null bytes.
135136
let r = builder
136137
.spawn_scoped(s, move || {
137-
rustc_span::create_session_globals_then(edition, Some(sm_inputs), || {
138-
f(CurrentGcx::new())
139-
})
138+
rustc_span::create_session_globals_then(
139+
edition,
140+
extra_symbols,
141+
Some(sm_inputs),
142+
|| f(CurrentGcx::new()),
143+
)
140144
})
141145
.unwrap()
142146
.join();
@@ -152,6 +156,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
152156
thread_builder_diag: &EarlyDiagCtxt,
153157
edition: Edition,
154158
threads: usize,
159+
extra_symbols: &[&'static str],
155160
sm_inputs: SourceMapInputs,
156161
f: F,
157162
) -> R {
@@ -168,12 +173,18 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
168173
let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
169174

170175
if !sync::is_dyn_thread_safe() {
171-
return run_in_thread_with_globals(thread_stack_size, edition, sm_inputs, |current_gcx| {
172-
// Register the thread for use with the `WorkerLocal` type.
173-
registry.register();
174-
175-
f(current_gcx)
176-
});
176+
return run_in_thread_with_globals(
177+
thread_stack_size,
178+
edition,
179+
sm_inputs,
180+
extra_symbols,
181+
|current_gcx| {
182+
// Register the thread for use with the `WorkerLocal` type.
183+
registry.register();
184+
185+
f(current_gcx)
186+
},
187+
);
177188
}
178189

179190
let current_gcx = FromDyn::from(CurrentGcx::new());
@@ -230,7 +241,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
230241
// pool. Upon creation, each worker thread created gets a copy of the
231242
// session globals in TLS. This is possible because `SessionGlobals` impls
232243
// `Send` in the parallel compiler.
233-
rustc_span::create_session_globals_then(edition, Some(sm_inputs), || {
244+
rustc_span::create_session_globals_then(edition, extra_symbols, Some(sm_inputs), || {
234245
rustc_span::with_session_globals(|session_globals| {
235246
let session_globals = FromDyn::from(session_globals);
236247
builder

compiler/rustc_macros/src/symbols.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,14 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
295295
}
296296

297297
let symbol_digits_base = entries.map["0"].idx;
298-
let preinterned_symbols_count = entries.len();
298+
let predefined_symbols_count = entries.len();
299299
let output = quote! {
300300
const SYMBOL_DIGITS_BASE: u32 = #symbol_digits_base;
301-
const PREINTERNED_SYMBOLS_COUNT: u32 = #preinterned_symbols_count;
301+
302+
/// The number of predefined symbols; this is the the first index for
303+
/// extra pre-interned symbols in an Interner created via
304+
/// [`Interner::with_extra_symbols`].
305+
pub const PREDEFINED_SYMBOLS_COUNT: u32 = #predefined_symbols_count;
302306

303307
#[doc(hidden)]
304308
#[allow(non_upper_case_globals)]
@@ -315,10 +319,13 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
315319
}
316320

317321
impl Interner {
318-
pub(crate) fn fresh() -> Self {
319-
Interner::prefill(&[
320-
#prefill_stream
321-
])
322+
/// Creates an `Interner` with the predefined symbols from the `symbols!` macro and
323+
/// any extra symbols provided by external drivers such as Clippy
324+
pub(crate) fn with_extra_symbols(extra_symbols: &[&'static str]) -> Self {
325+
Interner::prefill(
326+
&[#prefill_stream],
327+
extra_symbols,
328+
)
322329
}
323330
}
324331
};

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
564564
}
565565
SYMBOL_PREINTERNED => {
566566
let symbol_index = self.read_u32();
567-
Symbol::new_from_decoded(symbol_index)
567+
Symbol::new(symbol_index)
568568
}
569569
_ => unreachable!(),
570570
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
202202

203203
fn encode_symbol(&mut self, symbol: Symbol) {
204204
// if symbol preinterned, emit tag and symbol index
205-
if symbol.is_preinterned() {
205+
if symbol.is_predefined() {
206206
self.opaque.emit_u8(SYMBOL_PREINTERNED);
207207
self.opaque.emit_u32(symbol.as_u32());
208208
} else {

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
676676
}
677677
SYMBOL_PREINTERNED => {
678678
let symbol_index = self.read_u32();
679-
Symbol::new_from_decoded(symbol_index)
679+
Symbol::new(symbol_index)
680680
}
681681
_ => unreachable!(),
682682
}
@@ -893,7 +893,7 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
893893
// copy&paste impl from rustc_metadata
894894
fn encode_symbol(&mut self, symbol: Symbol) {
895895
// if symbol preinterned, emit tag and symbol index
896-
if symbol.is_preinterned() {
896+
if symbol.is_predefined() {
897897
self.encoder.emit_u8(SYMBOL_PREINTERNED);
898898
self.encoder.emit_u32(symbol.as_u32());
899899
} else {

compiler/rustc_span/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ pub struct SessionGlobals {
116116
}
117117

118118
impl SessionGlobals {
119-
pub fn new(edition: Edition, sm_inputs: Option<SourceMapInputs>) -> SessionGlobals {
119+
pub fn new(
120+
edition: Edition,
121+
extra_symbols: &[&'static str],
122+
sm_inputs: Option<SourceMapInputs>,
123+
) -> SessionGlobals {
120124
SessionGlobals {
121-
symbol_interner: symbol::Interner::fresh(),
125+
symbol_interner: symbol::Interner::with_extra_symbols(extra_symbols),
122126
span_interner: Lock::new(span_encoding::SpanInterner::default()),
123127
metavar_spans: Default::default(),
124128
hygiene_data: Lock::new(hygiene::HygieneData::new(edition)),
@@ -129,6 +133,7 @@ impl SessionGlobals {
129133

130134
pub fn create_session_globals_then<R>(
131135
edition: Edition,
136+
extra_symbols: &[&'static str],
132137
sm_inputs: Option<SourceMapInputs>,
133138
f: impl FnOnce() -> R,
134139
) -> R {
@@ -137,7 +142,7 @@ pub fn create_session_globals_then<R>(
137142
"SESSION_GLOBALS should never be overwritten! \
138143
Use another thread if you need another SessionGlobals"
139144
);
140-
let session_globals = SessionGlobals::new(edition, sm_inputs);
145+
let session_globals = SessionGlobals::new(edition, extra_symbols, sm_inputs);
141146
SESSION_GLOBALS.set(&session_globals, f)
142147
}
143148

@@ -156,7 +161,7 @@ where
156161
F: FnOnce(&SessionGlobals) -> R,
157162
{
158163
if !SESSION_GLOBALS.is_set() {
159-
let session_globals = SessionGlobals::new(edition, None);
164+
let session_globals = SessionGlobals::new(edition, &[], None);
160165
SESSION_GLOBALS.set(&session_globals, || SESSION_GLOBALS.with(f))
161166
} else {
162167
SESSION_GLOBALS.with(f)
@@ -172,7 +177,7 @@ where
172177

173178
/// Default edition, no source map.
174179
pub fn create_default_session_globals_then<R>(f: impl FnOnce() -> R) -> R {
175-
create_session_globals_then(edition::DEFAULT_EDITION, None, f)
180+
create_session_globals_then(edition::DEFAULT_EDITION, &[], None, f)
176181
}
177182

178183
// If this ever becomes non thread-local, `decode_syntax_context`

0 commit comments

Comments
 (0)