Move standard library injection into libsyntax_ext
This commit is contained in:
parent
f6eda99379
commit
4d535bdf59
8 changed files with 30 additions and 40 deletions
|
@ -63,7 +63,6 @@ use syntax::errors;
|
||||||
use syntax::ext::hygiene::ExpnId;
|
use syntax::ext::hygiene::ExpnId;
|
||||||
use syntax::print::pprust;
|
use syntax::print::pprust;
|
||||||
use syntax::source_map::{respan, ExpnInfo, ExpnKind, DesugaringKind, Spanned};
|
use syntax::source_map::{respan, ExpnInfo, ExpnKind, DesugaringKind, Spanned};
|
||||||
use syntax::std_inject;
|
|
||||||
use syntax::symbol::{kw, sym, Symbol};
|
use syntax::symbol::{kw, sym, Symbol};
|
||||||
use syntax::tokenstream::{TokenStream, TokenTree};
|
use syntax::tokenstream::{TokenStream, TokenTree};
|
||||||
use syntax::parse::token::{self, Token};
|
use syntax::parse::token::{self, Token};
|
||||||
|
@ -241,7 +240,7 @@ pub fn lower_crate(
|
||||||
dep_graph.assert_ignored();
|
dep_graph.assert_ignored();
|
||||||
|
|
||||||
LoweringContext {
|
LoweringContext {
|
||||||
crate_root: std_inject::injected_crate_name().map(Symbol::intern),
|
crate_root: sess.parse_sess.injected_crate_name.try_get().copied(),
|
||||||
sess,
|
sess,
|
||||||
cstore,
|
cstore,
|
||||||
resolver,
|
resolver,
|
||||||
|
|
|
@ -278,7 +278,12 @@ pub fn register_plugins<'a>(
|
||||||
|
|
||||||
krate = time(sess, "crate injection", || {
|
krate = time(sess, "crate injection", || {
|
||||||
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
|
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", || {
|
let registrars = time(sess, "plugin loading", || {
|
||||||
|
|
|
@ -34,7 +34,6 @@ use syntax::ext::hygiene::ExpnId;
|
||||||
use syntax::feature_gate::is_builtin_attr;
|
use syntax::feature_gate::is_builtin_attr;
|
||||||
use syntax::parse::token::{self, Token};
|
use syntax::parse::token::{self, Token};
|
||||||
use syntax::span_err;
|
use syntax::span_err;
|
||||||
use syntax::std_inject::injected_crate_name;
|
|
||||||
use syntax::symbol::{kw, sym};
|
use syntax::symbol::{kw, sym};
|
||||||
use syntax::visit::{self, Visitor};
|
use syntax::visit::{self, Visitor};
|
||||||
|
|
||||||
|
@ -367,8 +366,10 @@ impl<'a> Resolver<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.populate_module_if_necessary(module);
|
self.populate_module_if_necessary(module);
|
||||||
if injected_crate_name().map_or(false, |name| ident.name.as_str() == name) {
|
if let Some(name) = self.session.parse_sess.injected_crate_name.try_get() {
|
||||||
self.injected_crate = Some(module);
|
if name.as_str() == ident.name.as_str() {
|
||||||
|
self.injected_crate = Some(module);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let used = self.process_legacy_macro_imports(item, module, &parent_scope);
|
let used = self.process_legacy_macro_imports(item, module, &parent_scope);
|
||||||
|
|
|
@ -153,7 +153,6 @@ pub mod mut_visit;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
pub mod ptr;
|
pub mod ptr;
|
||||||
pub mod show_span;
|
pub mod show_span;
|
||||||
pub mod std_inject;
|
|
||||||
pub use syntax_pos::edition;
|
pub use syntax_pos::edition;
|
||||||
pub use syntax_pos::symbol;
|
pub use syntax_pos::symbol;
|
||||||
pub mod tokenstream;
|
pub mod tokenstream;
|
||||||
|
|
|
@ -10,9 +10,10 @@ use crate::parse::token::TokenKind;
|
||||||
use crate::tokenstream::{TokenStream, TokenTree};
|
use crate::tokenstream::{TokenStream, TokenTree};
|
||||||
use crate::diagnostics::plugin::ErrorMap;
|
use crate::diagnostics::plugin::ErrorMap;
|
||||||
use crate::print::pprust;
|
use crate::print::pprust;
|
||||||
|
use crate::symbol::Symbol;
|
||||||
|
|
||||||
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
|
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::{Span, SourceFile, FileName, MultiSpan};
|
||||||
use syntax_pos::edition::Edition;
|
use syntax_pos::edition::Edition;
|
||||||
|
|
||||||
|
@ -58,6 +59,7 @@ pub struct ParseSess {
|
||||||
pub let_chains_spans: Lock<Vec<Span>>,
|
pub let_chains_spans: Lock<Vec<Span>>,
|
||||||
// Places where `async || ..` exprs were used and should be feature gated.
|
// Places where `async || ..` exprs were used and should be feature gated.
|
||||||
pub async_closure_spans: Lock<Vec<Span>>,
|
pub async_closure_spans: Lock<Vec<Span>>,
|
||||||
|
pub injected_crate_name: Once<Symbol>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParseSess {
|
impl ParseSess {
|
||||||
|
@ -86,6 +88,7 @@ impl ParseSess {
|
||||||
param_attr_spans: Lock::new(Vec::new()),
|
param_attr_spans: Lock::new(Vec::new()),
|
||||||
let_chains_spans: Lock::new(Vec::new()),
|
let_chains_spans: Lock::new(Vec::new()),
|
||||||
async_closure_spans: Lock::new(Vec::new()),
|
async_closure_spans: Lock::new(Vec::new()),
|
||||||
|
injected_crate_name: Once::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ use crate::parse::{self, ParseSess};
|
||||||
use crate::print::pp::{self, Breaks};
|
use crate::print::pp::{self, Breaks};
|
||||||
use crate::print::pp::Breaks::{Consistent, Inconsistent};
|
use crate::print::pp::Breaks::{Consistent, Inconsistent};
|
||||||
use crate::ptr::P;
|
use crate::ptr::P;
|
||||||
use crate::std_inject;
|
|
||||||
use crate::symbol::{kw, sym};
|
use crate::symbol::{kw, sym};
|
||||||
use crate::tokenstream::{self, TokenStream, TokenTree};
|
use crate::tokenstream::{self, TokenStream, TokenTree};
|
||||||
|
|
||||||
|
@ -114,7 +113,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
|
||||||
is_expanded,
|
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
|
// We need to print `#![no_std]` (and its feature gate) so that
|
||||||
// compiling pretty-printed source won't inject libstd again.
|
// compiling pretty-printed source won't inject libstd again.
|
||||||
// However we don't want these attributes in the AST because
|
// However we don't want these attributes in the AST because
|
||||||
|
|
|
@ -43,6 +43,7 @@ mod trace_macros;
|
||||||
|
|
||||||
pub mod plugin_macro_defs;
|
pub mod plugin_macro_defs;
|
||||||
pub mod proc_macro_decls;
|
pub mod proc_macro_decls;
|
||||||
|
pub mod standard_library_imports;
|
||||||
pub mod test_harness;
|
pub mod test_harness;
|
||||||
|
|
||||||
pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, edition: Edition) {
|
pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, edition: Edition) {
|
||||||
|
|
|
@ -1,37 +1,22 @@
|
||||||
use crate::ast;
|
use syntax::{ast, attr};
|
||||||
use crate::attr;
|
use syntax::edition::Edition;
|
||||||
use crate::edition::Edition;
|
use syntax::ext::hygiene::{ExpnId, MacroKind};
|
||||||
use crate::ext::hygiene::{ExpnId, MacroKind};
|
use syntax::ptr::P;
|
||||||
use crate::symbol::{Ident, Symbol, kw, sym};
|
use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
|
||||||
use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
|
use syntax::symbol::{Ident, Symbol, kw, sym};
|
||||||
use crate::ptr::P;
|
use syntax::tokenstream::TokenStream;
|
||||||
use crate::tokenstream::TokenStream;
|
|
||||||
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::iter;
|
|
||||||
use syntax_pos::DUMMY_SP;
|
use syntax_pos::DUMMY_SP;
|
||||||
|
|
||||||
pub fn injected_crate_name() -> Option<&'static str> {
|
use std::iter;
|
||||||
INJECTED_CRATE_NAME.with(|name| name.get())
|
|
||||||
}
|
|
||||||
|
|
||||||
thread_local! {
|
pub fn inject(
|
||||||
// A `Symbol` might make more sense here, but it doesn't work, probably for
|
mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
|
||||||
// reasons relating to the use of thread-local storage for the Symbol
|
) -> (ast::Crate, Option<Symbol>) {
|
||||||
// interner.
|
|
||||||
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn maybe_inject_crates_ref(
|
|
||||||
mut krate: ast::Crate,
|
|
||||||
alt_std_name: Option<&str>,
|
|
||||||
edition: Edition,
|
|
||||||
) -> ast::Crate {
|
|
||||||
let rust_2018 = edition >= Edition::Edition2018;
|
let rust_2018 = edition >= Edition::Edition2018;
|
||||||
|
|
||||||
// the first name in this list is the crate name of the crate with the prelude
|
// 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) {
|
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) {
|
} else if attr::contains_name(&krate.attrs, sym::no_std) {
|
||||||
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
|
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
|
||||||
&["core"]
|
&["core"]
|
||||||
|
@ -73,8 +58,6 @@ pub fn maybe_inject_crates_ref(
|
||||||
// the prelude.
|
// the prelude.
|
||||||
let name = names[0];
|
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(
|
let span = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
|
||||||
ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
|
ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
|
||||||
[sym::prelude_import][..].into(),
|
[sym::prelude_import][..].into(),
|
||||||
|
@ -108,5 +91,5 @@ pub fn maybe_inject_crates_ref(
|
||||||
tokens: None,
|
tokens: None,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
krate
|
(krate, Some(Symbol::intern(name)))
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue