diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 2d1835514d4..0d431b010d6 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -63,7 +63,6 @@ use syntax::ext::hygiene::ExpnId; use syntax::print::pprust; use syntax::source_map::{respan, ExpnInfo, ExpnKind, DesugaringKind, Spanned}; -use syntax::std_inject; use syntax::symbol::{kw, sym, Symbol}; use syntax::tokenstream::{TokenStream, TokenTree}; use syntax::parse::token::{self, Token}; @@ -241,7 +240,7 @@ pub fn lower_crate( dep_graph.assert_ignored(); LoweringContext { - crate_root: std_inject::injected_crate_name().map(Symbol::intern), + crate_root: sess.parse_sess.injected_crate_name.try_get().copied(), sess, cstore, resolver, diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 58edb898c25..c7be6276f4a 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -278,7 +278,12 @@ pub fn register_plugins<'a>( krate = time(sess, "crate injection", || { let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s); - syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name, sess.edition()) + let (krate, name) = + syntax_ext::standard_library_imports::inject(krate, alt_std_name, sess.edition()); + if let Some(name) = name { + sess.parse_sess.injected_crate_name.set(name); + } + krate }); let registrars = time(sess, "plugin loading", || { diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index f52f7d9cfb3..41349cf72a1 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -34,7 +34,6 @@ use syntax::feature_gate::is_builtin_attr; use syntax::parse::token::{self, Token}; use syntax::span_err; -use syntax::std_inject::injected_crate_name; use syntax::symbol::{kw, sym}; use syntax::visit::{self, Visitor}; @@ -367,8 +366,10 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScop }; self.populate_module_if_necessary(module); - if injected_crate_name().map_or(false, |name| ident.name.as_str() == name) { - self.injected_crate = Some(module); + if let Some(name) = self.session.parse_sess.injected_crate_name.try_get() { + if name.as_str() == ident.name.as_str() { + self.injected_crate = Some(module); + } } let used = self.process_legacy_macro_imports(item, module, &parent_scope); diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 17f379f31b2..bb6a8dfb141 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -153,7 +153,6 @@ pub mod util { pub mod parse; pub mod ptr; pub mod show_span; -pub mod std_inject; pub use syntax_pos::edition; pub use syntax_pos::symbol; pub mod tokenstream; diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 225065c1cf1..1aac8bbb7aa 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -10,9 +10,10 @@ use crate::tokenstream::{TokenStream, TokenTree}; use crate::diagnostics::plugin::ErrorMap; use crate::print::pprust; +use crate::symbol::Symbol; use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder}; -use rustc_data_structures::sync::{Lrc, Lock}; +use rustc_data_structures::sync::{Lrc, Lock, Once}; use syntax_pos::{Span, SourceFile, FileName, MultiSpan}; use syntax_pos::edition::Edition; @@ -58,6 +59,7 @@ pub struct ParseSess { pub let_chains_spans: Lock>, // Places where `async || ..` exprs were used and should be feature gated. pub async_closure_spans: Lock>, + pub injected_crate_name: Once, } impl ParseSess { @@ -86,6 +88,7 @@ pub fn with_span_handler(handler: Handler, source_map: Lrc) -> ParseS param_attr_spans: Lock::new(Vec::new()), let_chains_spans: Lock::new(Vec::new()), async_closure_spans: Lock::new(Vec::new()), + injected_crate_name: Once::new(), } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 16e0bace925..c4623576395 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -10,7 +10,6 @@ use crate::print::pp::{self, Breaks}; use crate::print::pp::Breaks::{Consistent, Inconsistent}; use crate::ptr::P; -use crate::std_inject; use crate::symbol::{kw, sym}; use crate::tokenstream::{self, TokenStream, TokenTree}; @@ -114,7 +113,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap, is_expanded, }; - if is_expanded && std_inject::injected_crate_name().is_some() { + if is_expanded && sess.injected_crate_name.try_get().is_some() { // We need to print `#![no_std]` (and its feature gate) so that // compiling pretty-printed source won't inject libstd again. // However we don't want these attributes in the AST because diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index cd0d0886239..f49c75d7424 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -43,6 +43,7 @@ pub mod plugin_macro_defs; pub mod proc_macro_decls; +pub mod standard_library_imports; pub mod test_harness; pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, edition: Edition) { diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax_ext/standard_library_imports.rs similarity index 77% rename from src/libsyntax/std_inject.rs rename to src/libsyntax_ext/standard_library_imports.rs index 3fba81c0b69..81bb32d79a2 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax_ext/standard_library_imports.rs @@ -1,37 +1,22 @@ -use crate::ast; -use crate::attr; -use crate::edition::Edition; -use crate::ext::hygiene::{ExpnId, MacroKind}; -use crate::symbol::{Ident, Symbol, kw, sym}; -use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan}; -use crate::ptr::P; -use crate::tokenstream::TokenStream; - -use std::cell::Cell; -use std::iter; +use syntax::{ast, attr}; +use syntax::edition::Edition; +use syntax::ext::hygiene::{ExpnId, MacroKind}; +use syntax::ptr::P; +use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan}; +use syntax::symbol::{Ident, Symbol, kw, sym}; +use syntax::tokenstream::TokenStream; use syntax_pos::DUMMY_SP; -pub fn injected_crate_name() -> Option<&'static str> { - INJECTED_CRATE_NAME.with(|name| name.get()) -} +use std::iter; -thread_local! { - // A `Symbol` might make more sense here, but it doesn't work, probably for - // reasons relating to the use of thread-local storage for the Symbol - // interner. - static INJECTED_CRATE_NAME: Cell> = Cell::new(None); -} - -pub fn maybe_inject_crates_ref( - mut krate: ast::Crate, - alt_std_name: Option<&str>, - edition: Edition, -) -> ast::Crate { +pub fn inject( + mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition +) -> (ast::Crate, Option) { let rust_2018 = edition >= Edition::Edition2018; // the first name in this list is the crate name of the crate with the prelude let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) { - return krate; + return (krate, None); } else if attr::contains_name(&krate.attrs, sym::no_std) { if attr::contains_name(&krate.attrs, sym::compiler_builtins) { &["core"] @@ -73,8 +58,6 @@ pub fn maybe_inject_crates_ref( // the prelude. let name = names[0]; - INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name))); - let span = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable( ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition, [sym::prelude_import][..].into(), @@ -108,5 +91,5 @@ pub fn maybe_inject_crates_ref( tokens: None, })); - krate + (krate, Some(Symbol::intern(name))) }