diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-05-13 03:51:46 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-05-17 23:13:09 +0300 |
| commit | f4cbc2388f6a7abace3df9bf97b7758f0dfc0d4d (patch) | |
| tree | 0c22aec07a25c5662471b3d14b032d271b7086f9 /src/libsyntax/ext | |
| parent | 76bf3454da405fb3ccd56fca49a8423f2b72c40f (diff) | |
| download | rust-f4cbc2388f6a7abace3df9bf97b7758f0dfc0d4d.tar.gz rust-f4cbc2388f6a7abace3df9bf97b7758f0dfc0d4d.zip | |
Pass crate editions to macro expansions, update tests
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 31 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 26 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 9 |
3 files changed, 45 insertions, 21 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 3b76084f2fb..f7d4227977c 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -14,9 +14,10 @@ use ast::{self, Attribute, Name, PatKind, MetaItem}; use attr::HasAttrs; use codemap::{self, CodeMap, Spanned, respan}; use syntax_pos::{Span, MultiSpan, DUMMY_SP}; +use edition::Edition; use errors::{DiagnosticBuilder, DiagnosticId}; use ext::expand::{self, Expansion, Invocation}; -use ext::hygiene::{Mark, SyntaxContext}; +use ext::hygiene::{self, Mark, SyntaxContext}; use fold::{self, Folder}; use parse::{self, parser, DirectoryOwnership}; use parse::token; @@ -586,13 +587,13 @@ pub enum SyntaxExtension { MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>), /// A function-like procedural macro. TokenStream -> TokenStream. - ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>), + ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>, Edition), /// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream. /// The first TokenSteam is the attribute, the second is the annotated item. /// Allows modification of the input items and adding new items, similar to /// MultiModifier, but uses TokenStreams, rather than AST nodes. - AttrProcMacro(Box<AttrProcMacro + sync::Sync + sync::Send>), + AttrProcMacro(Box<AttrProcMacro + sync::Sync + sync::Send>, Edition), /// A normal, function-like syntax extension. /// @@ -608,6 +609,8 @@ pub enum SyntaxExtension { allow_internal_unsafe: bool, /// The macro's feature name if it is unstable, and the stability feature unstable_feature: Option<(Symbol, u32)>, + /// Edition of the crate in which the macro is defined + edition: Edition, }, /// A function-like syntax extension that has an extra ident before @@ -619,9 +622,8 @@ pub enum SyntaxExtension { /// The input is the annotated item. /// Allows generating code to implement a Trait for a given struct /// or enum item. - ProcMacroDerive(Box<MultiItemModifier + - sync::Sync + - sync::Send>, Vec<Symbol> /* inert attribute names */), + ProcMacroDerive(Box<MultiItemModifier + sync::Sync + sync::Send>, + Vec<Symbol> /* inert attribute names */, Edition), /// An attribute-like procedural macro that derives a builtin trait. BuiltinDerive(BuiltinDeriveFn), @@ -629,7 +631,7 @@ pub enum SyntaxExtension { /// A declarative macro, e.g. `macro m() {}`. /// /// The second element is the definition site span. - DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>), + DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>, Edition), } impl SyntaxExtension { @@ -660,6 +662,21 @@ impl SyntaxExtension { _ => false, } } + + pub fn edition(&self) -> Edition { + match *self { + SyntaxExtension::NormalTT { edition, .. } | + SyntaxExtension::DeclMacro(.., edition) | + SyntaxExtension::ProcMacro(.., edition) | + SyntaxExtension::AttrProcMacro(.., edition) | + SyntaxExtension::ProcMacroDerive(.., edition) => edition, + // Unstable legacy stuff + SyntaxExtension::IdentTT(..) | + SyntaxExtension::MultiDecorator(..) | + SyntaxExtension::MultiModifier(..) | + SyntaxExtension::BuiltinDerive(..) => hygiene::default_edition(), + } + } } pub type NamedSyntaxExtension = (Name, SyntaxExtension); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 1cf0a7077c2..ee96963362b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -502,7 +502,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: None, allow_internal_unstable: false, allow_internal_unsafe: false, - edition: hygiene::default_edition(), + edition: ext.edition(), } }); @@ -521,7 +521,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { items.push(item); Some(kind.expect_from_annotatables(items)) } - AttrProcMacro(ref mac) => { + AttrProcMacro(ref mac, ..) => { self.gate_proc_macro_attr_item(attr.span, &item); let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item { Annotatable::Item(item) => token::NtItem(item), @@ -610,7 +610,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { allow_internal_unstable, allow_internal_unsafe, // can't infer this type - unstable_feature: Option<(Symbol, u32)>| { + unstable_feature: Option<(Symbol, u32)>, + edition| { // feature-gate the macro invocation if let Some((feature, issue)) = unstable_feature { @@ -643,16 +644,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: def_site_span, allow_internal_unstable, allow_internal_unsafe, - edition: hygiene::default_edition(), + edition, }, }); Ok(()) }; let opt_expanded = match *ext { - DeclMacro(ref expand, def_span) => { + DeclMacro(ref expand, def_span, edition) => { if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s), - false, false, None) { + false, false, None, + edition) { dummy_span } else { kind.make_from(expand.expand(self.cx, span, mac.node.stream())) @@ -665,11 +667,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { allow_internal_unstable, allow_internal_unsafe, unstable_feature, + edition, } => { if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s), allow_internal_unstable, allow_internal_unsafe, - unstable_feature) { + unstable_feature, + edition) { dummy_span } else { kind.make_from(expander.expand(self.cx, span, mac.node.stream())) @@ -712,7 +716,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { kind.dummy(span) } - ProcMacro(ref expandfun) => { + ProcMacro(ref expandfun, edition) => { if ident.name != keywords::Invalid.name() { let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident); @@ -731,7 +735,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // FIXME probably want to follow macro_rules macros here. allow_internal_unstable: false, allow_internal_unsafe: false, - edition: hygiene::default_edition(), + edition, }, }); @@ -806,12 +810,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: None, allow_internal_unstable: false, allow_internal_unsafe: false, - edition: hygiene::default_edition(), + edition: ext.edition(), } }; match *ext { - ProcMacroDerive(ref ext, _) => { + ProcMacroDerive(ref ext, ..) => { invoc.expansion_data.mark.set_expn_info(expn_info); let span = span.with_ctxt(self.cx.backtrace()); let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index e96a0e838cf..d1a7e7aac26 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -10,6 +10,7 @@ use {ast, attr}; use syntax_pos::{Span, DUMMY_SP}; +use edition::Edition; use ext::base::{DummyResult, ExtCtxt, MacResult, SyntaxExtension}; use ext::base::{NormalTT, TTMacroExpander}; use ext::expand::{Expansion, ExpansionKind}; @@ -183,7 +184,8 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt, // Holy self-referential! /// Converts a `macro_rules!` invocation into a syntax extension. -pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item) -> SyntaxExtension { +pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition: Edition) + -> SyntaxExtension { let lhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("lhs")); let rhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("rhs")); @@ -298,10 +300,11 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item) -> Syntax def_info: Some((def.id, def.span)), allow_internal_unstable, allow_internal_unsafe, - unstable_feature + unstable_feature, + edition, } } else { - SyntaxExtension::DeclMacro(expander, Some((def.id, def.span))) + SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)), edition) } } |
