Move standard library injection into libsyntax_ext

This commit is contained in:
Vadim Petrochenkov 2019-07-18 22:29:07 +03:00
parent f6eda99379
commit 4d535bdf59
8 changed files with 30 additions and 40 deletions

View File

@ -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,

View File

@ -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", || {

View File

@ -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);

View File

@ -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;

View File

@ -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<Vec<Span>>,
// Places where `async || ..` exprs were used and should be feature gated.
pub async_closure_spans: Lock<Vec<Span>>,
pub injected_crate_name: Once<Symbol>,
}
impl ParseSess {
@ -86,6 +88,7 @@ pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> 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(),
}
}

View File

@ -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

View File

@ -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) {

View File

@ -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<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 {
pub fn inject(
mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
) -> (ast::Crate, Option<Symbol>) {
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)))
}