Allow drivers to supply a list of extra symbols to intern
This commit is contained in:
parent
87e60a7d28
commit
f740326216
23 changed files with 115 additions and 58 deletions
|
@ -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`
|
||||
|
|
|
@ -2506,15 +2506,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 {
|
||||
|
@ -2600,11 +2595,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]
|
||||
|
@ -2729,9 +2727,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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue