diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-09-08 01:42:12 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-11-04 14:56:07 +0300 |
| commit | e7cedc9972ae753402fece658b5b9f580f4fc5f3 (patch) | |
| tree | 1a9b86a156544b097f67ee1ffa02178c17ad6195 /src/libsyntax_expand | |
| parent | cba93685377bc74a2fde1eb8e7a086039b038e94 (diff) | |
expand: Feature gate out-of-line modules in proc macro input
Diffstat (limited to 'src/libsyntax_expand')
| -rw-r--r-- | src/libsyntax_expand/expand.rs | 66 |
1 files changed, 50 insertions, 16 deletions
diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index fc521e5edc0..bdb50dbfb4f 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -17,9 +17,10 @@ use syntax::parse::token; use syntax::parse::parser::Parser; use syntax::print::pprust; use syntax::ptr::P; +use syntax::sess::ParseSess; use syntax::symbol::{sym, Symbol}; use syntax::tokenstream::{TokenStream, TokenTree}; -use syntax::visit::Visitor; +use syntax::visit::{self, Visitor}; use syntax::util::map_in_place::MapInPlace; use errors::{Applicability, FatalError}; @@ -615,6 +616,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } InvocationKind::Attr { attr, mut item, .. } => match ext { SyntaxExtensionKind::Attr(expander) => { + self.gate_proc_macro_input(&item); self.gate_proc_macro_attr_item(span, &item); let item_tok = TokenTree::token(token::Interpolated(Lrc::new(match item { Annotatable::Item(item) => token::NtItem(item), @@ -664,6 +666,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if !item.derive_allowed() { return fragment_kind.dummy(span); } + if let SyntaxExtensionKind::Derive(..) = ext { + self.gate_proc_macro_input(&item); + } let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path }; let items = expander.expand(self.cx, span, &meta, item); fragment_kind.expect_from_annotatables(items) @@ -692,21 +697,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) { - let (kind, gate) = match *item { - Annotatable::Item(ref item) => { - match item.kind { - ItemKind::Mod(_) if self.cx.ecfg.proc_macro_hygiene() => return, - ItemKind::Mod(_) => ("modules", sym::proc_macro_hygiene), - _ => return, - } + let kind = match item { + Annotatable::Item(item) => match &item.kind { + ItemKind::Mod(m) if m.inline => "modules", + _ => return, } - Annotatable::TraitItem(_) => return, - Annotatable::ImplItem(_) => return, - Annotatable::ForeignItem(_) => return, - Annotatable::Stmt(_) | - Annotatable::Expr(_) if self.cx.ecfg.proc_macro_hygiene() => return, - Annotatable::Stmt(_) => ("statements", sym::proc_macro_hygiene), - Annotatable::Expr(_) => ("expressions", sym::proc_macro_hygiene), + Annotatable::TraitItem(_) + | Annotatable::ImplItem(_) + | Annotatable::ForeignItem(_) => return, + Annotatable::Stmt(_) => "statements", + Annotatable::Expr(_) => "expressions", Annotatable::Arm(..) | Annotatable::Field(..) | Annotatable::FieldPat(..) @@ -716,15 +716,49 @@ impl<'a, 'b> MacroExpander<'a, 'b> { | Annotatable::Variant(..) => panic!("unexpected annotatable"), }; + if self.cx.ecfg.proc_macro_hygiene() { + return + } emit_feature_err( self.cx.parse_sess, - gate, + sym::proc_macro_hygiene, span, GateIssue::Language, &format!("custom attributes cannot be applied to {}", kind), ); } + fn gate_proc_macro_input(&self, annotatable: &Annotatable) { + struct GateProcMacroInput<'a> { + parse_sess: &'a ParseSess, + } + + impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> { + fn visit_item(&mut self, item: &'ast ast::Item) { + match &item.kind { + ast::ItemKind::Mod(module) if !module.inline => { + emit_feature_err( + self.parse_sess, + sym::proc_macro_hygiene, + item.span, + GateIssue::Language, + "non-inline modules in proc macro input are unstable", + ); + } + _ => {} + } + + visit::walk_item(self, item); + } + + fn visit_mac(&mut self, _: &'ast ast::Mac) {} + } + + if !self.cx.ecfg.proc_macro_hygiene() { + annotatable.visit_with(&mut GateProcMacroInput { parse_sess: self.cx.parse_sess }); + } + } + fn gate_proc_macro_expansion_kind(&self, span: Span, kind: AstFragmentKind) { let kind = match kind { AstFragmentKind::Expr | |
