diff options
| author | bors <bors@rust-lang.org> | 2017-12-13 11:09:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-12-13 11:09:55 +0000 |
| commit | 3dfbc88a626625be01e112da11ec367e2fc71bb3 (patch) | |
| tree | 3123349d2f450ac5317944d5f5803b20c3eca65b /src/libsyntax/ext | |
| parent | 61100840e5c978a99b0489e8eaa922da06c05f65 (diff) | |
| parent | 85d19b33357897c51d80727a4208f46b19c5c5a6 (diff) | |
| download | rust-3dfbc88a626625be01e112da11ec367e2fc71bb3.tar.gz rust-3dfbc88a626625be01e112da11ec367e2fc71bb3.zip | |
Auto merge of #46550 - jseyfried:cleanup_builtin_hygiene, r=nrc
macros: hygienize use of `core`/`std` in builtin macros Today, if a builtin macro wants to access an item from `core` or `std` (depending `#![no_std]`), it generates `::core::path::to::item` or `::std::path::to::item` respectively (c.f. `fn std_path()` in `libsyntax/ext/base.rs`). This PR refactors the builtin macros to instead always emit `$crate::path::to::item` here. That is, the def site of builtin macros is taken to be in `extern crate core;` or `extern crate std;`. Since builtin macros are macros 1.0 (i.e. mostly unhygienic), changing the def site can only effect the resolution of `$crate`. r? @nrc
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 15 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 2 |
3 files changed, 10 insertions, 12 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 6c96692f719..bb1b7da7dba 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -21,10 +21,11 @@ use fold::{self, Folder}; use parse::{self, parser, DirectoryOwnership}; use parse::token; use ptr::P; -use symbol::Symbol; +use symbol::{keywords, Ident, Symbol}; use util::small_vector::SmallVector; use std::collections::HashMap; +use std::iter; use std::path::PathBuf; use std::rc::Rc; use std::default::Default; @@ -664,7 +665,6 @@ pub struct ExpansionData { pub struct ExtCtxt<'a> { pub parse_sess: &'a parse::ParseSess, pub ecfg: expand::ExpansionConfig<'a>, - pub crate_root: Option<&'static str>, pub root_path: PathBuf, pub resolver: &'a mut Resolver, pub resolve_err_count: usize, @@ -680,7 +680,6 @@ impl<'a> ExtCtxt<'a> { ExtCtxt { parse_sess, ecfg, - crate_root: None, root_path: PathBuf::new(), resolver, resolve_err_count: 0, @@ -822,12 +821,10 @@ impl<'a> ExtCtxt<'a> { ast::Ident::from_str(st) } pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> { - let mut v = Vec::new(); - if let Some(s) = self.crate_root { - v.push(self.ident_of(s)); - } - v.extend(components.iter().map(|s| self.ident_of(s))); - v + let def_site = SyntaxContext::empty().apply_mark(self.current_expansion.mark); + iter::once(Ident { ctxt: def_site, ..keywords::DollarCrate.ident() }) + .chain(components.iter().map(|s| self.ident_of(s))) + .collect() } pub fn name_of(&self, st: &str) -> ast::Name { Symbol::intern(st) diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 25eef6db930..82e7747b014 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -319,9 +319,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> { types: Vec<P<ast::Ty>>, bindings: Vec<ast::TypeBinding> ) -> ast::Path { + use syntax::parse::token; + let last_identifier = idents.pop().unwrap(); let mut segments: Vec<ast::PathSegment> = Vec::new(); - if global { + if global && + !idents.first().map_or(false, |&ident| token::Ident(ident).is_path_segment_keyword()) { segments.push(ast::PathSegment::crate_root(span)); } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 0d1b1c65a29..ecb396f259f 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -25,7 +25,6 @@ use parse::{DirectoryOwnership, PResult}; use parse::token::{self, Token}; use parse::parser::Parser; use ptr::P; -use std_inject; use symbol::Symbol; use symbol::keywords; use syntax_pos::{Span, DUMMY_SP}; @@ -219,7 +218,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { - self.cx.crate_root = std_inject::injected_crate_name(&krate); let mut module = ModuleData { mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)], directory: self.cx.codemap().span_to_unmapped_path(krate.span), |
