Skip to content

Commit f740326

Browse files
committed
Allow drivers to supply a list of extra symbols to intern
1 parent 87e60a7 commit f740326

File tree

23 files changed

+116
-59
lines changed

23 files changed

+116
-59
lines changed

Diff for: compiler/rustc_driver_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
259259
hash_untracked_state: None,
260260
register_lints: None,
261261
override_queries: None,
262+
extra_symbols: Vec::new(),
262263
make_codegen_backend: None,
263264
registry: diagnostics_registry(),
264265
using_internal_features: &USING_INTERNAL_FEATURES,

Diff for: compiler/rustc_hir/src/tests.rs

+1-1
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

Diff for: compiler/rustc_interface/src/interface.rs

+5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ pub struct Config {
340340
/// the list of queries.
341341
pub override_queries: Option<fn(&Session, &mut Providers)>,
342342

343+
/// An extra set of symbols to add to the symbol interner, the symbol indices
344+
/// will start at [`PREDEFINED_SYMBOLS_COUNT`](rustc_span::symbol::PREDEFINED_SYMBOLS_COUNT)
345+
pub extra_symbols: Vec<&'static str>,
346+
343347
/// This is a callback from the driver that is called to create a codegen backend.
344348
///
345349
/// Has no uses within this repository, but is used by bjorn3 for "the
@@ -401,6 +405,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
401405
&early_dcx,
402406
config.opts.edition,
403407
config.opts.unstable_opts.threads,
408+
&config.extra_symbols,
404409
SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
405410
|current_gcx| {
406411
// The previous `early_dcx` can't be reused here because it doesn't

Diff for: compiler/rustc_interface/src/tests.rs

+1-1
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() },

Diff for: compiler/rustc_interface/src/util.rs

+21-10
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());
@@ -217,7 +228,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
217228
// pool. Upon creation, each worker thread created gets a copy of the
218229
// session globals in TLS. This is possible because `SessionGlobals` impls
219230
// `Send` in the parallel compiler.
220-
rustc_span::create_session_globals_then(edition, Some(sm_inputs), || {
231+
rustc_span::create_session_globals_then(edition, extra_symbols, Some(sm_inputs), || {
221232
rustc_span::with_session_globals(|session_globals| {
222233
let session_globals = FromDyn::from(session_globals);
223234
builder

Diff for: compiler/rustc_macros/src/symbols.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ pub(super) fn symbols(input: TokenStream) -> TokenStream {
142142
output
143143
}
144144

145-
struct Preinterned {
145+
struct Predefined {
146146
idx: u32,
147147
span_of_name: Span,
148148
}
149149

150150
struct Entries {
151-
map: HashMap<String, Preinterned>,
151+
map: HashMap<String, Predefined>,
152152
}
153153

154154
impl Entries {
@@ -163,7 +163,7 @@ impl Entries {
163163
prev.idx
164164
} else {
165165
let idx = self.len();
166-
self.map.insert(s.to_string(), Preinterned { idx, span_of_name: span });
166+
self.map.insert(s.to_string(), Predefined { idx, span_of_name: span });
167167
idx
168168
}
169169
}
@@ -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
};

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
562562
Symbol::intern(s)
563563
})
564564
}
565-
SYMBOL_PREINTERNED => {
565+
SYMBOL_PREDEFINED => {
566566
let symbol_index = self.read_u32();
567-
Symbol::new_from_decoded(symbol_index)
567+
Symbol::new(symbol_index)
568568
}
569569
_ => unreachable!(),
570570
}

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
201201
}
202202

203203
fn encode_symbol(&mut self, symbol: Symbol) {
204-
// if symbol preinterned, emit tag and symbol index
205-
if symbol.is_preinterned() {
206-
self.opaque.emit_u8(SYMBOL_PREINTERNED);
204+
// if symbol predefined, emit tag and symbol index
205+
if symbol.is_predefined() {
206+
self.opaque.emit_u8(SYMBOL_PREDEFINED);
207207
self.opaque.emit_u32(symbol.as_u32());
208208
} else {
209209
// otherwise write it as string or as offset to it

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl SpanTag {
576576
// Tags for encoding Symbol's
577577
const SYMBOL_STR: u8 = 0;
578578
const SYMBOL_OFFSET: u8 = 1;
579-
const SYMBOL_PREINTERNED: u8 = 2;
579+
const SYMBOL_PREDEFINED: u8 = 2;
580580

581581
pub fn provide(providers: &mut Providers) {
582582
encoder::provide(providers);

Diff for: compiler/rustc_middle/src/query/on_disk_cache.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const TAG_EXPN_DATA: u8 = 1;
4545
// Tags for encoding Symbol's
4646
const SYMBOL_STR: u8 = 0;
4747
const SYMBOL_OFFSET: u8 = 1;
48-
const SYMBOL_PREINTERNED: u8 = 2;
48+
const SYMBOL_PREDEFINED: u8 = 2;
4949

5050
/// Provides an interface to incremental compilation data cached from the
5151
/// previous compilation session. This data will eventually include the results
@@ -673,9 +673,9 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
673673
Symbol::intern(s)
674674
})
675675
}
676-
SYMBOL_PREINTERNED => {
676+
SYMBOL_PREDEFINED => {
677677
let symbol_index = self.read_u32();
678-
Symbol::new_from_decoded(symbol_index)
678+
Symbol::new(symbol_index)
679679
}
680680
_ => unreachable!(),
681681
}
@@ -891,9 +891,9 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
891891

892892
// copy&paste impl from rustc_metadata
893893
fn encode_symbol(&mut self, symbol: Symbol) {
894-
// if symbol preinterned, emit tag and symbol index
895-
if symbol.is_preinterned() {
896-
self.encoder.emit_u8(SYMBOL_PREINTERNED);
894+
// if symbol predefined, emit tag and symbol index
895+
if symbol.is_predefined() {
896+
self.encoder.emit_u8(SYMBOL_PREDEFINED);
897897
self.encoder.emit_u32(symbol.as_u32());
898898
} else {
899899
// otherwise write it as string or as offset to it

Diff for: compiler/rustc_span/src/lib.rs

+10-5
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`

Diff for: compiler/rustc_span/src/symbol.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -2506,15 +2506,10 @@ rustc_index::newtype_index! {
25062506
}
25072507

25082508
impl Symbol {
2509-
const fn new(n: u32) -> Self {
2509+
pub const fn new(n: u32) -> Self {
25102510
Symbol(SymbolIndex::from_u32(n))
25112511
}
25122512

2513-
/// for use in Decoder only
2514-
pub fn new_from_decoded(n: u32) -> Self {
2515-
Self::new(n)
2516-
}
2517-
25182513
/// Maps a string to its interned representation.
25192514
#[rustc_diagnostic_item = "SymbolIntern"]
25202515
pub fn intern(string: &str) -> Self {
@@ -2600,11 +2595,14 @@ struct InternerInner {
26002595
}
26012596

26022597
impl Interner {
2603-
fn prefill(init: &[&'static str]) -> Self {
2604-
Interner(Lock::new(InternerInner {
2605-
arena: Default::default(),
2606-
strings: init.iter().copied().collect(),
2607-
}))
2598+
fn prefill(init: &[&'static str], extra: &[&'static str]) -> Self {
2599+
let strings = FxIndexSet::from_iter(init.iter().copied().chain(extra.iter().copied()));
2600+
assert_eq!(
2601+
strings.len(),
2602+
init.len() + extra.len(),
2603+
"`init` or `extra` contain duplicate symbols",
2604+
);
2605+
Interner(Lock::new(InternerInner { arena: Default::default(), strings }))
26082606
}
26092607

26102608
#[inline]
@@ -2729,9 +2727,9 @@ impl Symbol {
27292727
self != kw::Empty && self != kw::Underscore && !self.is_path_segment_keyword()
27302728
}
27312729

2732-
/// Is this symbol was interned in compiler's `symbols!` macro
2733-
pub fn is_preinterned(self) -> bool {
2734-
self.as_u32() < PREINTERNED_SYMBOLS_COUNT
2730+
/// Was this symbol predefined in the compiler's `symbols!` macro
2731+
pub fn is_predefined(self) -> bool {
2732+
self.as_u32() < PREDEFINED_SYMBOLS_COUNT
27352733
}
27362734
}
27372735

Diff for: compiler/rustc_span/src/symbol/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::create_default_session_globals_then;
33

44
#[test]
55
fn interner_tests() {
6-
let i = Interner::prefill(&[]);
6+
let i = Interner::prefill(&[], &[]);
77
// first one is zero:
88
assert_eq!(i.intern("dog"), Symbol::new(0));
99
// re-use gets the same entry:

Diff for: src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub(crate) fn create_config(
321321
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
322322
};
323323
}),
324+
extra_symbols: Vec::new(),
324325
make_codegen_backend: None,
325326
registry: rustc_driver::diagnostics_registry(),
326327
ice_file: None,

Diff for: src/librustdoc/doctest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
191191
hash_untracked_state: None,
192192
register_lints: Some(Box::new(crate::lint::register_lints)),
193193
override_queries: None,
194+
extra_symbols: Vec::new(),
194195
make_codegen_backend: None,
195196
registry: rustc_driver::diagnostics_registry(),
196197
ice_file: None,

Diff for: src/tools/clippy/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ path = "src/driver.rs"
2525
[dependencies]
2626
clippy_config = { path = "clippy_config" }
2727
clippy_lints = { path = "clippy_lints" }
28+
clippy_utils = { path = "clippy_utils" }
2829
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }
2930
tempfile = { version = "3.3", optional = true }
3031
termize = "0.1"

Diff for: src/tools/clippy/clippy_lints/src/attrs/deprecated_cfg_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::msrvs::{self, MsrvStack};
4+
use clippy_utils::sym;
45
use rustc_ast::AttrStyle;
56
use rustc_errors::Applicability;
67
use rustc_lint::EarlyContext;
7-
use rustc_span::sym;
88

99
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
1010
// check cfg_attr
@@ -18,7 +18,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
1818
&& msrv.meets(msrvs::TOOL_ATTRIBUTES)
1919
// check for `rustfmt_skip` and `rustfmt::skip`
2020
&& let Some(skip_item) = &items[1].meta_item()
21-
&& (skip_item.has_name(sym!(rustfmt_skip))
21+
&& (skip_item.has_name(sym::rustfmt_skip)
2222
|| skip_item
2323
.path
2424
.segments

0 commit comments

Comments
 (0)