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/deriving/bounds.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/clone.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/cmp/eq.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/cmp/ord.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/cmp/partial_eq.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/cmp/partial_ord.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/debug.rs | 7 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/decodable.rs | 20 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/default.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/encodable.rs | 24 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/generic/ty.rs | 38 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/hash.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/mod.rs | 30 | ||||
| -rw-r--r-- | src/libsyntax_ext/lib.rs | 1 |
14 files changed, 77 insertions, 86 deletions
diff --git a/src/libsyntax_ext/deriving/bounds.rs b/src/libsyntax_ext/deriving/bounds.rs index 0bc802283fb..7f03001d9c6 100644 --- a/src/libsyntax_ext/deriving/bounds.rs +++ b/src/libsyntax_ext/deriving/bounds.rs @@ -8,9 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; - use syntax::ast::MetaItem; use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax_pos::Span; @@ -28,15 +28,10 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt, mitem: &MetaItem, item: &Annotatable, push: &mut FnMut(Annotatable)) { - let mut v = cx.crate_root.map(|s| vec![s]).unwrap_or(Vec::new()); - v.push("marker"); - v.push("Copy"); - let path = Path::new(v); - let trait_def = TraitDef { span, attributes: Vec::new(), - path, + path: path_std!(cx, marker::Copy), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/clone.rs b/src/libsyntax_ext/deriving/clone.rs index 5d93c2a5f72..35def632fc1 100644 --- a/src/libsyntax_ext/deriving/clone.rs +++ b/src/libsyntax_ext/deriving/clone.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; @@ -55,7 +56,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, })); } ItemKind::Union(..) => { - bounds = vec![Literal(path_std!(cx, core::marker::Copy))]; + bounds = vec![Literal(path_std!(cx, marker::Copy))]; is_shallow = true; substructure = combine_substructure(Box::new(|c, s, sub| { cs_clone_shallow("Clone", c, s, sub, true) @@ -79,7 +80,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::clone::Clone), + path: path_std!(cx, clone::Clone), additional_bounds: bounds, generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/cmp/eq.rs b/src/libsyntax_ext/deriving/cmp/eq.rs index a282ff5bd04..237c8654edf 100644 --- a/src/libsyntax_ext/deriving/cmp/eq.rs +++ b/src/libsyntax_ext/deriving/cmp/eq.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; @@ -30,7 +31,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::cmp::Eq), + path: path_std!(cx, cmp::Eq), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/cmp/ord.rs b/src/libsyntax_ext/deriving/cmp/ord.rs index 9b057aacece..1a392ac3765 100644 --- a/src/libsyntax_ext/deriving/cmp/ord.rs +++ b/src/libsyntax_ext/deriving/cmp/ord.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; @@ -28,7 +29,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::cmp::Ord), + path: path_std!(cx, cmp::Ord), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, @@ -38,7 +39,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec![borrowed_self()], - ret_ty: Literal(path_std!(cx, core::cmp::Ordering)), + ret_ty: Literal(path_std!(cx, cmp::Ordering)), attributes: attrs, is_unsafe: false, unify_fieldless_variants: true, diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs index e635c6bebcd..75db7cc1e4c 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::{path_local, path_std}; use deriving::generic::*; use deriving::generic::ty::*; @@ -93,7 +94,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::cmp::PartialEq), + path: path_std!(cx, cmp::PartialEq), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/cmp/partial_ord.rs b/src/libsyntax_ext/deriving/cmp/partial_ord.rs index e7e1c108760..92183c58eb2 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_ord.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_ord.rs @@ -10,6 +10,7 @@ pub use self::OrderingOp::*; +use deriving::{path_local, pathvec_std, path_std}; use deriving::generic::*; use deriving::generic::ty::*; @@ -45,11 +46,11 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt, } } } - let ordering_ty = Literal(path_std!(cx, core::cmp::Ordering)); - let ret_ty = Literal(Path::new_(pathvec_std!(cx, core::option::Option), + let ordering_ty = Literal(path_std!(cx, cmp::Ordering)); + let ret_ty = Literal(Path::new_(pathvec_std!(cx, option::Option), None, vec![Box::new(ordering_ty)], - true)); + PathKind::Std)); let inline = cx.meta_word(span, Symbol::intern("inline")); let attrs = vec![cx.attribute(span, inline)]; @@ -84,7 +85,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: vec![], - path: path_std!(cx, core::cmp::PartialOrd), + path: path_std!(cx, cmp::PartialOrd), additional_bounds: vec![], generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/debug.rs b/src/libsyntax_ext/deriving/debug.rs index ab6dd04520c..82fc09fca69 100644 --- a/src/libsyntax_ext/deriving/debug.rs +++ b/src/libsyntax_ext/deriving/debug.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; @@ -24,13 +25,13 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt, item: &Annotatable, push: &mut FnMut(Annotatable)) { // &mut ::std::fmt::Formatter - let fmtr = Ptr(Box::new(Literal(path_std!(cx, core::fmt::Formatter))), + let fmtr = Ptr(Box::new(Literal(path_std!(cx, fmt::Formatter))), Borrowed(None, ast::Mutability::Mutable)); let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::fmt::Debug), + path: path_std!(cx, fmt::Debug), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, @@ -40,7 +41,7 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec![fmtr], - ret_ty: Literal(path_std!(cx, core::fmt::Result)), + ret_ty: Literal(path_std!(cx, fmt::Result)), attributes: Vec::new(), is_unsafe: false, unify_fieldless_variants: false, diff --git a/src/libsyntax_ext/deriving/decodable.rs b/src/libsyntax_ext/deriving/decodable.rs index 517221af1d4..46dada256b8 100644 --- a/src/libsyntax_ext/deriving/decodable.rs +++ b/src/libsyntax_ext/deriving/decodable.rs @@ -10,7 +10,7 @@ //! The compiler code necessary for `#[derive(Decodable)]`. See encodable.rs for more. -use deriving; +use deriving::{self, pathvec_std}; use deriving::generic::*; use deriving::generic::ty::*; use deriving::warn_if_deprecated; @@ -46,20 +46,12 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, item: &Annotatable, push: &mut FnMut(Annotatable), krate: &'static str) { - if cx.crate_root != Some("std") { - // FIXME(#21880): lift this requirement. - cx.span_err(span, - "this trait cannot be derived with #![no_std] \ - or #![no_core]"); - return; - } - let typaram = &*deriving::hygienic_type_parameter(item, "__D"); let trait_def = TraitDef { span, attributes: Vec::new(), - path: Path::new_(vec![krate, "Decodable"], None, vec![], true), + path: Path::new_(vec![krate, "Decodable"], None, vec![], PathKind::Global), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, @@ -72,18 +64,18 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, vec![Path::new_(vec![krate, "Decoder"], None, vec![], - true)])], + PathKind::Global)])], }, explicit_self: None, args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mutable))], ret_ty: - Literal(Path::new_(pathvec_std!(cx, core::result::Result), + Literal(Path::new_(pathvec_std!(cx, result::Result), None, vec![Box::new(Self_), Box::new(Literal(Path::new_( - vec![typaram, "Error"], None, vec![], false + vec![typaram, "Error"], None, vec![], PathKind::Local )))], - true)), + PathKind::Std)), attributes: Vec::new(), is_unsafe: false, unify_fieldless_variants: false, diff --git a/src/libsyntax_ext/deriving/default.rs b/src/libsyntax_ext/deriving/default.rs index 0c8e3c39395..99e7bb4baef 100644 --- a/src/libsyntax_ext/deriving/default.rs +++ b/src/libsyntax_ext/deriving/default.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; @@ -28,7 +29,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt, let trait_def = TraitDef { span, attributes: Vec::new(), - path: path_std!(cx, core::default::Default), + path: path_std!(cx, default::Default), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/encodable.rs b/src/libsyntax_ext/deriving/encodable.rs index c2c862f043f..0e6e96438d8 100644 --- a/src/libsyntax_ext/deriving/encodable.rs +++ b/src/libsyntax_ext/deriving/encodable.rs @@ -92,7 +92,7 @@ //! } //! ``` -use deriving; +use deriving::{self, pathvec_std}; use deriving::generic::*; use deriving::generic::ty::*; use deriving::warn_if_deprecated; @@ -127,20 +127,12 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt, item: &Annotatable, push: &mut FnMut(Annotatable), krate: &'static str) { - if cx.crate_root != Some("std") { - // FIXME(#21880): lift this requirement. - cx.span_err(span, - "this trait cannot be derived with #![no_std] \ - or #![no_core]"); - return; - } - let typaram = &*deriving::hygienic_type_parameter(item, "__S"); let trait_def = TraitDef { span, attributes: Vec::new(), - path: Path::new_(vec![krate, "Encodable"], None, vec![], true), + path: Path::new_(vec![krate, "Encodable"], None, vec![], PathKind::Global), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), is_unsafe: false, @@ -150,19 +142,21 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt, name: "encode", generics: LifetimeBounds { lifetimes: Vec::new(), - bounds: vec![(typaram, - vec![Path::new_(vec![krate, "Encoder"], None, vec![], true)])] + bounds: vec![ + (typaram, + vec![Path::new_(vec![krate, "Encoder"], None, vec![], PathKind::Global)]) + ], }, explicit_self: borrowed_explicit_self(), args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mutable))], ret_ty: Literal(Path::new_( - pathvec_std!(cx, core::result::Result), + pathvec_std!(cx, result::Result), None, vec![Box::new(Tuple(Vec::new())), Box::new(Literal(Path::new_( - vec![typaram, "Error"], None, vec![], false + vec![typaram, "Error"], None, vec![], PathKind::Local )))], - true + PathKind::Std )), attributes: Vec::new(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs index 47b5f40832a..e4faf652389 100644 --- a/src/libsyntax_ext/deriving/generic/ty.rs +++ b/src/libsyntax_ext/deriving/generic/ty.rs @@ -21,6 +21,8 @@ use syntax::ext::build::AstBuilder; use syntax::codemap::respan; use syntax::ptr::P; use syntax_pos::Span; +use syntax_pos::hygiene::SyntaxContext; +use syntax_pos::symbol::keywords; /// The types of pointers #[derive(Clone, Eq, PartialEq)] @@ -36,29 +38,36 @@ pub enum PtrTy<'a> { /// for type parameters and a lifetime. #[derive(Clone, Eq, PartialEq)] pub struct Path<'a> { - pub path: Vec<&'a str>, - pub lifetime: Option<&'a str>, - pub params: Vec<Box<Ty<'a>>>, - pub global: bool, + path: Vec<&'a str>, + lifetime: Option<&'a str>, + params: Vec<Box<Ty<'a>>>, + kind: PathKind, +} + +#[derive(Clone, Eq, PartialEq)] +pub enum PathKind { + Local, + Global, + Std, } impl<'a> Path<'a> { pub fn new<'r>(path: Vec<&'r str>) -> Path<'r> { - Path::new_(path, None, Vec::new(), true) + Path::new_(path, None, Vec::new(), PathKind::Std) } pub fn new_local<'r>(path: &'r str) -> Path<'r> { - Path::new_(vec![path], None, Vec::new(), false) + Path::new_(vec![path], None, Vec::new(), PathKind::Local) } pub fn new_<'r>(path: Vec<&'r str>, lifetime: Option<&'r str>, params: Vec<Box<Ty<'r>>>, - global: bool) + kind: PathKind) -> Path<'r> { Path { path, lifetime, params, - global, + kind, } } @@ -76,11 +85,20 @@ impl<'a> Path<'a> { self_ty: Ident, self_generics: &Generics) -> ast::Path { - let idents = self.path.iter().map(|s| cx.ident_of(*s)).collect(); + let mut idents = self.path.iter().map(|s| cx.ident_of(*s)).collect(); let lt = mk_lifetimes(cx, span, &self.lifetime); let tys = self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect(); - cx.path_all(span, self.global, idents, lt, tys, Vec::new()) + match self.kind { + PathKind::Global => cx.path_all(span, true, idents, lt, tys, Vec::new()), + PathKind::Local => cx.path_all(span, false, idents, lt, tys, Vec::new()), + PathKind::Std => { + let def_site = SyntaxContext::empty().apply_mark(cx.current_expansion.mark); + idents.insert(0, Ident { ctxt: def_site, ..keywords::DollarCrate.ident() }); + cx.path_all(span, false, idents, lt, tys, Vec::new()) + } + } + } } diff --git a/src/libsyntax_ext/deriving/hash.rs b/src/libsyntax_ext/deriving/hash.rs index 6c78eea186d..b192ab2527e 100644 --- a/src/libsyntax_ext/deriving/hash.rs +++ b/src/libsyntax_ext/deriving/hash.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use deriving; +use deriving::{self, pathvec_std, path_std}; use deriving::generic::*; use deriving::generic::ty::*; @@ -24,7 +24,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, item: &Annotatable, push: &mut FnMut(Annotatable)) { - let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None, vec![], true); + let path = Path::new_(pathvec_std!(cx, hash::Hash), None, vec![], PathKind::Std); let typaram = &*deriving::hygienic_type_parameter(item, "__H"); @@ -41,7 +41,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, name: "hash", generics: LifetimeBounds { lifetimes: Vec::new(), - bounds: vec![(typaram, vec![path_std!(cx, core::hash::Hasher)])], + bounds: vec![(typaram, vec![path_std!(cx, hash::Hasher)])], }, explicit_self: borrowed_explicit_self(), args: vec![Ptr(Box::new(Literal(arg)), diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index ccf3d550234..a6696b53369 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -19,32 +19,16 @@ use syntax::ptr::P; use syntax::symbol::Symbol; use syntax_pos::Span; -macro_rules! pathvec { - ($($x:ident)::+) => ( - vec![ $( stringify!($x) ),+ ] - ) +macro path_local($x:ident) { + generic::ty::Path::new_local(stringify!($x)) } -macro_rules! path_local { - ($x:ident) => ( - ::deriving::generic::ty::Path::new_local(stringify!($x)) - ) -} - -macro_rules! pathvec_std { - ($cx:expr, $first:ident :: $($rest:ident)::+) => ({ - let mut v = pathvec![$($rest)::+]; - if let Some(s) = $cx.crate_root { - v.insert(0, s); - } - v - }) -} +macro pathvec_std($cx:expr, $($rest:ident)::+) {{ + vec![ $( stringify!($rest) ),+ ] +}} -macro_rules! path_std { - ($($x:tt)*) => ( - ::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) ) - ) +macro path_std($($x:tt)*) { + generic::ty::Path::new( pathvec_std!( $($x)* ) ) } pub mod bounds; diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 42bbb4ae0cb..82d6ee5afa0 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -16,6 +16,7 @@ #![deny(warnings)] #![feature(proc_macro_internals)] +#![feature(decl_macro)] extern crate fmt_macros; #[macro_use] |
