Rollup merge of #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 75530e9f72/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

---

try-job: aarch64-gnu
This commit is contained in:
Stuart Cook 2025-04-11 13:31:44 +10:00 committed by GitHub
commit 0abc6c6e98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 115 additions and 58 deletions

View file

@ -116,9 +116,13 @@ pub struct SessionGlobals {
}
impl SessionGlobals {
pub fn new(edition: Edition, sm_inputs: Option<SourceMapInputs>) -> SessionGlobals {
pub fn new(
edition: Edition,
extra_symbols: &[&'static str],
sm_inputs: Option<SourceMapInputs>,
) -> SessionGlobals {
SessionGlobals {
symbol_interner: symbol::Interner::fresh(),
symbol_interner: symbol::Interner::with_extra_symbols(extra_symbols),
span_interner: Lock::new(span_encoding::SpanInterner::default()),
metavar_spans: Default::default(),
hygiene_data: Lock::new(hygiene::HygieneData::new(edition)),
@ -129,6 +133,7 @@ impl SessionGlobals {
pub fn create_session_globals_then<R>(
edition: Edition,
extra_symbols: &[&'static str],
sm_inputs: Option<SourceMapInputs>,
f: impl FnOnce() -> R,
) -> R {
@ -137,7 +142,7 @@ pub fn create_session_globals_then<R>(
"SESSION_GLOBALS should never be overwritten! \
Use another thread if you need another SessionGlobals"
);
let session_globals = SessionGlobals::new(edition, sm_inputs);
let session_globals = SessionGlobals::new(edition, extra_symbols, sm_inputs);
SESSION_GLOBALS.set(&session_globals, f)
}
@ -156,7 +161,7 @@ where
F: FnOnce(&SessionGlobals) -> R,
{
if !SESSION_GLOBALS.is_set() {
let session_globals = SessionGlobals::new(edition, None);
let session_globals = SessionGlobals::new(edition, &[], None);
SESSION_GLOBALS.set(&session_globals, || SESSION_GLOBALS.with(f))
} else {
SESSION_GLOBALS.with(f)
@ -172,7 +177,7 @@ where
/// Default edition, no source map.
pub fn create_default_session_globals_then<R>(f: impl FnOnce() -> R) -> R {
create_session_globals_then(edition::DEFAULT_EDITION, None, f)
create_session_globals_then(edition::DEFAULT_EDITION, &[], None, f)
}
// If this ever becomes non thread-local, `decode_syntax_context`

View file

@ -2540,15 +2540,10 @@ rustc_index::newtype_index! {
}
impl Symbol {
const fn new(n: u32) -> Self {
pub const fn new(n: u32) -> Self {
Symbol(SymbolIndex::from_u32(n))
}
/// for use in Decoder only
pub fn new_from_decoded(n: u32) -> Self {
Self::new(n)
}
/// Maps a string to its interned representation.
#[rustc_diagnostic_item = "SymbolIntern"]
pub fn intern(string: &str) -> Self {
@ -2634,11 +2629,14 @@ struct InternerInner {
}
impl Interner {
fn prefill(init: &[&'static str]) -> Self {
Interner(Lock::new(InternerInner {
arena: Default::default(),
strings: init.iter().copied().collect(),
}))
fn prefill(init: &[&'static str], extra: &[&'static str]) -> Self {
let strings = FxIndexSet::from_iter(init.iter().copied().chain(extra.iter().copied()));
assert_eq!(
strings.len(),
init.len() + extra.len(),
"`init` or `extra` contain duplicate symbols",
);
Interner(Lock::new(InternerInner { arena: Default::default(), strings }))
}
#[inline]
@ -2762,9 +2760,9 @@ impl Symbol {
self != kw::Empty && self != kw::Underscore && !self.is_path_segment_keyword()
}
/// Is this symbol was interned in compiler's `symbols!` macro
pub fn is_preinterned(self) -> bool {
self.as_u32() < PREINTERNED_SYMBOLS_COUNT
/// Was this symbol predefined in the compiler's `symbols!` macro
pub fn is_predefined(self) -> bool {
self.as_u32() < PREDEFINED_SYMBOLS_COUNT
}
}

View file

@ -3,7 +3,7 @@ use crate::create_default_session_globals_then;
#[test]
fn interner_tests() {
let i = Interner::prefill(&[]);
let i = Interner::prefill(&[], &[]);
// first one is zero:
assert_eq!(i.intern("dog"), Symbol::new(0));
// re-use gets the same entry: