Skip to content

Commit 5e4bac3

Browse files
committed
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]: #50504 (comment) cc #50504
1 parent 4208bd5 commit 5e4bac3

File tree

6 files changed

+153
-6
lines changed

6 files changed

+153
-6
lines changed

src/libsyntax/ext/expand.rs

+55-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use ext::placeholders::{placeholder, PlaceholderExpander};
2121
use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
2222
use fold;
2323
use fold::*;
24-
use parse::{DirectoryOwnership, PResult};
24+
use parse::{DirectoryOwnership, PResult, ParseSess};
2525
use parse::token::{self, Token};
2626
use parse::parser::Parser;
2727
use ptr::P;
@@ -31,7 +31,7 @@ use syntax_pos::{Span, DUMMY_SP, FileName};
3131
use syntax_pos::hygiene::ExpnFormat;
3232
use tokenstream::{TokenStream, TokenTree};
3333
use util::small_vector::SmallVector;
34-
use visit::Visitor;
34+
use visit::{self, Visitor};
3535

3636
use std::collections::HashMap;
3737
use std::fs::File;
@@ -532,7 +532,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
532532
})).into();
533533
let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span);
534534
let tok_result = mac.expand(self.cx, attr.span, input, item_tok);
535-
self.parse_expansion(tok_result, kind, &attr.path, attr.span)
535+
let res = self.parse_expansion(tok_result, kind, &attr.path, attr.span);
536+
self.gate_proc_macro_expansion(attr.span, &res);
537+
res
536538
}
537539
ProcMacroDerive(..) | BuiltinDerive(..) => {
538540
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
@@ -591,6 +593,50 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
591593
);
592594
}
593595

596+
fn gate_proc_macro_expansion(&self, span: Span, expansion: &Option<Expansion>) {
597+
if self.cx.ecfg.proc_macro_gen() {
598+
return
599+
}
600+
let expansion = match expansion {
601+
Some(expansion) => expansion,
602+
None => return,
603+
};
604+
605+
expansion.visit_with(&mut DisallowModules {
606+
span,
607+
parse_sess: self.cx.parse_sess,
608+
});
609+
610+
struct DisallowModules<'a> {
611+
span: Span,
612+
parse_sess: &'a ParseSess,
613+
}
614+
615+
impl<'ast, 'a> Visitor<'ast> for DisallowModules<'a> {
616+
fn visit_item(&mut self, i: &'ast ast::Item) {
617+
let name = match i.node {
618+
ast::ItemKind::Mod(_) => Some("modules"),
619+
ast::ItemKind::MacroDef(_) => Some("macro definitions"),
620+
_ => None,
621+
};
622+
if let Some(name) = name {
623+
emit_feature_err(
624+
self.parse_sess,
625+
"proc_macro_gen",
626+
self.span,
627+
GateIssue::Language,
628+
&format!("procedural macros cannot expand to {}", name),
629+
);
630+
}
631+
visit::walk_item(self, i);
632+
}
633+
634+
fn visit_mac(&mut self, _mac: &'ast ast::Mac) {
635+
// ...
636+
}
637+
}
638+
}
639+
594640
/// Expand a macro invocation. Returns the result of expansion.
595641
fn expand_bang_invoc(&mut self,
596642
invoc: Invocation,
@@ -732,7 +778,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
732778
});
733779

734780
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
735-
self.parse_expansion(tok_result, kind, path, span)
781+
let result = self.parse_expansion(tok_result, kind, path, span);
782+
self.gate_proc_macro_expansion(span, &result);
783+
result
736784
}
737785
}
738786
};
@@ -814,7 +862,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
814862
span: DUMMY_SP,
815863
node: ast::MetaItemKind::Word,
816864
};
817-
Some(kind.expect_from_annotatables(ext.expand(self.cx, span, &dummy, item)))
865+
let items = ext.expand(self.cx, span, &dummy, item);
866+
Some(kind.expect_from_annotatables(items))
818867
}
819868
BuiltinDerive(func) => {
820869
expn_info.callee.allow_internal_unstable = true;
@@ -1491,6 +1540,7 @@ impl<'feat> ExpansionConfig<'feat> {
14911540
fn proc_macro_enabled = proc_macro,
14921541
fn macros_in_extern_enabled = macros_in_extern,
14931542
fn proc_macro_mod = proc_macro_mod,
1543+
fn proc_macro_gen = proc_macro_gen,
14941544
fn proc_macro_expr = proc_macro_expr,
14951545
fn proc_macro_non_items = proc_macro_non_items,
14961546
}

src/libsyntax/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ declare_features! (
451451
(active, proc_macro_mod, "1.27.0", None, None),
452452
(active, proc_macro_expr, "1.27.0", None, None),
453453
(active, proc_macro_non_items, "1.27.0", None, None),
454+
(active, proc_macro_gen, "1.27.0", None, None),
454455

455456
// #[doc(alias = "...")]
456457
(active, doc_alias, "1.27.0", Some(50146), None),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
#![feature(proc_macro)]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::*;
19+
20+
#[proc_macro_attribute]
21+
pub fn attr2mod(_: TokenStream, _: TokenStream) -> TokenStream {
22+
"mod test {}".parse().unwrap()
23+
}
24+
25+
#[proc_macro_attribute]
26+
pub fn attr2mac1(_: TokenStream, _: TokenStream) -> TokenStream {
27+
"macro_rules! foo1 { (a) => (a) }".parse().unwrap()
28+
}
29+
30+
#[proc_macro_attribute]
31+
pub fn attr2mac2(_: TokenStream, _: TokenStream) -> TokenStream {
32+
"macro foo2(a) { a }".parse().unwrap()
33+
}
34+
35+
#[proc_macro]
36+
pub fn mac2mod(_: TokenStream) -> TokenStream {
37+
"mod test2 {}".parse().unwrap()
38+
}
39+
40+
#[proc_macro]
41+
pub fn mac2mac1(_: TokenStream) -> TokenStream {
42+
"macro_rules! foo3 { (a) => (a) }".parse().unwrap()
43+
}
44+
45+
#[proc_macro]
46+
pub fn mac2mac2(_: TokenStream) -> TokenStream {
47+
"macro foo4(a) { a }".parse().unwrap()
48+
}
49+
50+
#[proc_macro]
51+
pub fn tricky(_: TokenStream) -> TokenStream {
52+
"fn foo() {
53+
mod test {}
54+
macro_rules! foo { (a) => (a) }
55+
}".parse().unwrap()
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:more-gates.rs
12+
13+
#![feature(proc_macro)]
14+
15+
extern crate more_gates as foo;
16+
17+
use foo::*;
18+
19+
#[attr2mod]
20+
//~^ ERROR: cannot expand to modules
21+
pub fn a() {}
22+
#[attr2mac1]
23+
//~^ ERROR: cannot expand to macro definitions
24+
pub fn a() {}
25+
#[attr2mac2]
26+
//~^ ERROR: cannot expand to macro definitions
27+
pub fn a() {}
28+
29+
mac2mod!(); //~ ERROR: cannot expand to modules
30+
mac2mac1!(); //~ ERROR: cannot expand to macro definitions
31+
mac2mac2!(); //~ ERROR: cannot expand to macro definitions
32+
33+
tricky!();
34+
//~^ ERROR: cannot expand to modules
35+
//~| ERROR: cannot expand to macro definitions
36+
37+
fn main() {}

src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// gate-test-proc_macro_mod line
1515
// gate-test-proc_macro_expr
1616
// gate-test-proc_macro_mod
17+
// gate-test-proc_macro_gen
1718

1819
#![feature(proc_macro, stmt_expr_attributes)]
1920

@@ -29,10 +30,12 @@ fn _test_inner() {
2930
}
3031

3132
#[a] //~ ERROR: custom attributes cannot be applied to modules
33+
//~| ERROR: procedural macros cannot expand to modules
3234
mod _test2 {}
3335

3436
mod _test2_inner {
3537
#![a] //~ ERROR: custom attributes cannot be applied to modules
38+
//~| ERROR: procedural macros cannot expand to modules
3639
}
3740

3841
#[a = y] //~ ERROR: must only be followed by a delimiter token

src/test/run-pass-fulldeps/macro-quote-test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// aux-build:hello_macro.rs
1414
// ignore-stage1
1515

16-
#![feature(use_extern_macros, proc_macro_non_items)]
16+
#![feature(use_extern_macros, proc_macro_non_items, proc_macro_gen)]
1717

1818
extern crate hello_macro;
1919

0 commit comments

Comments
 (0)