Skip to content

Commit 0c0ddcd

Browse files
committed
Move standard library injection into libsyntax_ext
1 parent 11137c6 commit 0c0ddcd

File tree

8 files changed

+30
-40
lines changed

8 files changed

+30
-40
lines changed

src/librustc/hir/lowering.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use syntax::errors;
6363
use syntax::ext::hygiene::ExpnId;
6464
use syntax::print::pprust;
6565
use syntax::source_map::{respan, ExpnInfo, ExpnKind, DesugaringKind, Spanned};
66-
use syntax::std_inject;
6766
use syntax::symbol::{kw, sym, Symbol};
6867
use syntax::tokenstream::{TokenStream, TokenTree};
6968
use syntax::parse::token::{self, Token};
@@ -241,7 +240,7 @@ pub fn lower_crate(
241240
dep_graph.assert_ignored();
242241

243242
LoweringContext {
244-
crate_root: std_inject::injected_crate_name().map(Symbol::intern),
243+
crate_root: sess.parse_sess.injected_crate_name.try_get().copied(),
245244
sess,
246245
cstore,
247246
resolver,

src/librustc_interface/passes.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,12 @@ pub fn register_plugins<'a>(
278278

279279
krate = time(sess, "crate injection", || {
280280
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
281-
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name, sess.edition())
281+
let (krate, name) =
282+
syntax_ext::standard_library_imports::inject(krate, alt_std_name, sess.edition());
283+
if let Some(name) = name {
284+
sess.parse_sess.injected_crate_name.set(name);
285+
}
286+
krate
282287
});
283288

284289
let registrars = time(sess, "plugin loading", || {

src/librustc_resolve/build_reduced_graph.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use syntax::ext::tt::macro_rules;
3535
use syntax::feature_gate::is_builtin_attr;
3636
use syntax::parse::token::{self, Token};
3737
use syntax::span_err;
38-
use syntax::std_inject::injected_crate_name;
3938
use syntax::symbol::{kw, sym};
4039
use syntax::visit::{self, Visitor};
4140

@@ -368,8 +367,10 @@ impl<'a> Resolver<'a> {
368367
};
369368

370369
self.populate_module_if_necessary(module);
371-
if injected_crate_name().map_or(false, |name| ident.name.as_str() == name) {
372-
self.injected_crate = Some(module);
370+
if let Some(name) = self.session.parse_sess.injected_crate_name.try_get() {
371+
if name.as_str() == ident.name.as_str() {
372+
self.injected_crate = Some(module);
373+
}
373374
}
374375

375376
let used = self.process_legacy_macro_imports(item, module, &parent_scope);

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ pub mod mut_visit;
153153
pub mod parse;
154154
pub mod ptr;
155155
pub mod show_span;
156-
pub mod std_inject;
157156
pub use syntax_pos::edition;
158157
pub use syntax_pos::symbol;
159158
pub mod tokenstream;

src/libsyntax/parse/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use crate::parse::token::TokenKind;
1010
use crate::tokenstream::{TokenStream, TokenTree};
1111
use crate::diagnostics::plugin::ErrorMap;
1212
use crate::print::pprust;
13+
use crate::symbol::Symbol;
1314

1415
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
15-
use rustc_data_structures::sync::{Lrc, Lock};
16+
use rustc_data_structures::sync::{Lrc, Lock, Once};
1617
use syntax_pos::{Span, SourceFile, FileName, MultiSpan};
1718
use syntax_pos::edition::Edition;
1819

@@ -58,6 +59,7 @@ pub struct ParseSess {
5859
pub let_chains_spans: Lock<Vec<Span>>,
5960
// Places where `async || ..` exprs were used and should be feature gated.
6061
pub async_closure_spans: Lock<Vec<Span>>,
62+
pub injected_crate_name: Once<Symbol>,
6163
}
6264

6365
impl ParseSess {
@@ -86,6 +88,7 @@ impl ParseSess {
8688
param_attr_spans: Lock::new(Vec::new()),
8789
let_chains_spans: Lock::new(Vec::new()),
8890
async_closure_spans: Lock::new(Vec::new()),
91+
injected_crate_name: Once::new(),
8992
}
9093
}
9194

src/libsyntax/print/pprust.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::parse::{self, ParseSess};
1010
use crate::print::pp::{self, Breaks};
1111
use crate::print::pp::Breaks::{Consistent, Inconsistent};
1212
use crate::ptr::P;
13-
use crate::std_inject;
1413
use crate::symbol::{kw, sym};
1514
use crate::tokenstream::{self, TokenStream, TokenTree};
1615

@@ -114,7 +113,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
114113
is_expanded,
115114
};
116115

117-
if is_expanded && std_inject::injected_crate_name().is_some() {
116+
if is_expanded && sess.injected_crate_name.try_get().is_some() {
118117
// We need to print `#![no_std]` (and its feature gate) so that
119118
// compiling pretty-printed source won't inject libstd again.
120119
// However we don't want these attributes in the AST because

src/libsyntax_ext/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod test;
3333
mod trace_macros;
3434

3535
pub mod proc_macro_decls;
36+
pub mod standard_library_imports;
3637
pub mod test_harness;
3738

3839
use rustc_data_structures::sync::Lrc;

src/libsyntax/std_inject.rs renamed to src/libsyntax_ext/standard_library_imports.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
use crate::ast;
2-
use crate::attr;
3-
use crate::edition::Edition;
4-
use crate::ext::hygiene::{ExpnId, MacroKind};
5-
use crate::symbol::{Ident, Symbol, kw, sym};
6-
use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
7-
use crate::ptr::P;
8-
use crate::tokenstream::TokenStream;
9-
10-
use std::cell::Cell;
11-
use std::iter;
1+
use syntax::{ast, attr};
2+
use syntax::edition::Edition;
3+
use syntax::ext::hygiene::{ExpnId, MacroKind};
4+
use syntax::ptr::P;
5+
use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
6+
use syntax::symbol::{Ident, Symbol, kw, sym};
7+
use syntax::tokenstream::TokenStream;
128
use syntax_pos::DUMMY_SP;
139

14-
pub fn injected_crate_name() -> Option<&'static str> {
15-
INJECTED_CRATE_NAME.with(|name| name.get())
16-
}
17-
18-
thread_local! {
19-
// A `Symbol` might make more sense here, but it doesn't work, probably for
20-
// reasons relating to the use of thread-local storage for the Symbol
21-
// interner.
22-
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
23-
}
10+
use std::iter;
2411

25-
pub fn maybe_inject_crates_ref(
26-
mut krate: ast::Crate,
27-
alt_std_name: Option<&str>,
28-
edition: Edition,
29-
) -> ast::Crate {
12+
pub fn inject(
13+
mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
14+
) -> (ast::Crate, Option<Symbol>) {
3015
let rust_2018 = edition >= Edition::Edition2018;
3116

3217
// the first name in this list is the crate name of the crate with the prelude
3318
let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
34-
return krate;
19+
return (krate, None);
3520
} else if attr::contains_name(&krate.attrs, sym::no_std) {
3621
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
3722
&["core"]
@@ -73,8 +58,6 @@ pub fn maybe_inject_crates_ref(
7358
// the prelude.
7459
let name = names[0];
7560

76-
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
77-
7861
let span = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
7962
ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
8063
[sym::prelude_import][..].into(),
@@ -108,5 +91,5 @@ pub fn maybe_inject_crates_ref(
10891
tokens: None,
10992
}));
11093

111-
krate
94+
(krate, Some(Symbol::intern(name)))
11295
}

0 commit comments

Comments
 (0)