diff options
| author | bors <bors@rust-lang.org> | 2018-05-20 05:57:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-05-20 05:57:41 +0000 |
| commit | ccb5e973f7546cef6c389a5378cdfbf2fcb595f1 (patch) | |
| tree | 0f52d956edc7676181f0b045f19943fdf526a8a9 /src/libsyntax | |
| parent | 522aa5e00bde42fa8ad380b30763ed4921a7c3a0 (diff) | |
| parent | 5e4bac31b82241db4ca8e9234a4f368c30e8966a (diff) | |
| download | rust-ccb5e973f7546cef6c389a5378cdfbf2fcb595f1.tar.gz rust-ccb5e973f7546cef6c389a5378cdfbf2fcb595f1.zip | |
Auto merge of #50820 - alexcrichton:no-modules, r=petrochenkov
rustc: Disallow modules and macros in expansions This commit feature gates generating modules and macro definitions in procedural macro expansions. Custom derive is exempt from this check as it would be a large retroactive breaking change (#50587). It's hoped that we can hopefully stem the bleeding to figure out a better solution here before opening up the floodgates. The restriction here is specifically targeted at surprising hygiene results [1] that result in non-"copy/paste" behavior. Hygiene and procedural macros is intended to be avoided as much as possible for Macros 1.2 by saying everything is "as if you copy/pasted the code", but modules and macros are sort of weird exceptions to this rule that aren't fully fleshed out. [1]: https://github.com/rust-lang/rust/issues/50504#issuecomment-387734625 cc #50504
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 60 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 1 |
2 files changed, 56 insertions, 5 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index ee96963362b..146db632c07 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -21,7 +21,7 @@ use ext::placeholders::{placeholder, PlaceholderExpander}; use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err}; use fold; use fold::*; -use parse::{DirectoryOwnership, PResult}; +use parse::{DirectoryOwnership, PResult, ParseSess}; use parse::token::{self, Token}; use parse::parser::Parser; use ptr::P; @@ -31,7 +31,7 @@ use syntax_pos::{Span, DUMMY_SP, FileName}; use syntax_pos::hygiene::ExpnFormat; use tokenstream::{TokenStream, TokenTree}; use util::small_vector::SmallVector; -use visit::Visitor; +use visit::{self, Visitor}; use std::collections::HashMap; use std::fs::File; @@ -533,7 +533,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { })).into(); let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span); let tok_result = mac.expand(self.cx, attr.span, input, item_tok); - self.parse_expansion(tok_result, kind, &attr.path, attr.span) + let res = self.parse_expansion(tok_result, kind, &attr.path, attr.span); + self.gate_proc_macro_expansion(attr.span, &res); + res } ProcMacroDerive(..) | BuiltinDerive(..) => { self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path)); @@ -592,6 +594,50 @@ impl<'a, 'b> MacroExpander<'a, 'b> { ); } + fn gate_proc_macro_expansion(&self, span: Span, expansion: &Option<Expansion>) { + if self.cx.ecfg.proc_macro_gen() { + return + } + let expansion = match expansion { + Some(expansion) => expansion, + None => return, + }; + + expansion.visit_with(&mut DisallowModules { + span, + parse_sess: self.cx.parse_sess, + }); + + struct DisallowModules<'a> { + span: Span, + parse_sess: &'a ParseSess, + } + + impl<'ast, 'a> Visitor<'ast> for DisallowModules<'a> { + fn visit_item(&mut self, i: &'ast ast::Item) { + let name = match i.node { + ast::ItemKind::Mod(_) => Some("modules"), + ast::ItemKind::MacroDef(_) => Some("macro definitions"), + _ => None, + }; + if let Some(name) = name { + emit_feature_err( + self.parse_sess, + "proc_macro_gen", + self.span, + GateIssue::Language, + &format!("procedural macros cannot expand to {}", name), + ); + } + visit::walk_item(self, i); + } + + fn visit_mac(&mut self, _mac: &'ast ast::Mac) { + // ... + } + } + } + /// Expand a macro invocation. Returns the result of expansion. fn expand_bang_invoc(&mut self, invoc: Invocation, @@ -740,7 +786,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }); let tok_result = expandfun.expand(self.cx, span, mac.node.stream()); - self.parse_expansion(tok_result, kind, path, span) + let result = self.parse_expansion(tok_result, kind, path, span); + self.gate_proc_macro_expansion(span, &result); + result } } }; @@ -823,7 +871,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: DUMMY_SP, node: ast::MetaItemKind::Word, }; - Some(kind.expect_from_annotatables(ext.expand(self.cx, span, &dummy, item))) + let items = ext.expand(self.cx, span, &dummy, item); + Some(kind.expect_from_annotatables(items)) } BuiltinDerive(func) => { expn_info.callee.allow_internal_unstable = true; @@ -1500,6 +1549,7 @@ impl<'feat> ExpansionConfig<'feat> { fn proc_macro_enabled = proc_macro, fn macros_in_extern_enabled = macros_in_extern, fn proc_macro_mod = proc_macro_mod, + fn proc_macro_gen = proc_macro_gen, fn proc_macro_expr = proc_macro_expr, fn proc_macro_non_items = proc_macro_non_items, } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index d247b5402ef..604df862dc2 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -451,6 +451,7 @@ declare_features! ( (active, proc_macro_mod, "1.27.0", None, None), (active, proc_macro_expr, "1.27.0", None, None), (active, proc_macro_non_items, "1.27.0", None, None), + (active, proc_macro_gen, "1.27.0", None, None), // #[doc(alias = "...")] (active, doc_alias, "1.27.0", Some(50146), None), |
