Skip to content

Commit ccb5e97

Browse files
committed
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]: #50504 (comment) cc #50504
2 parents 522aa5e + 5e4bac3 commit ccb5e97

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;
@@ -533,7 +533,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
533533
})).into();
534534
let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span);
535535
let tok_result = mac.expand(self.cx, attr.span, input, item_tok);
536-
self.parse_expansion(tok_result, kind, &attr.path, attr.span)
536+
let res = self.parse_expansion(tok_result, kind, &attr.path, attr.span);
537+
self.gate_proc_macro_expansion(attr.span, &res);
538+
res
537539
}
538540
ProcMacroDerive(..) | BuiltinDerive(..) => {
539541
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
@@ -592,6 +594,50 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
592594
);
593595
}
594596

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

742788
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
743-
self.parse_expansion(tok_result, kind, path, span)
789+
let result = self.parse_expansion(tok_result, kind, path, span);
790+
self.gate_proc_macro_expansion(span, &result);
791+
result
744792
}
745793
}
746794
};
@@ -823,7 +871,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
823871
span: DUMMY_SP,
824872
node: ast::MetaItemKind::Word,
825873
};
826-
Some(kind.expect_from_annotatables(ext.expand(self.cx, span, &dummy, item)))
874+
let items = ext.expand(self.cx, span, &dummy, item);
875+
Some(kind.expect_from_annotatables(items))
827876
}
828877
BuiltinDerive(func) => {
829878
expn_info.callee.allow_internal_unstable = true;
@@ -1500,6 +1549,7 @@ impl<'feat> ExpansionConfig<'feat> {
15001549
fn proc_macro_enabled = proc_macro,
15011550
fn macros_in_extern_enabled = macros_in_extern,
15021551
fn proc_macro_mod = proc_macro_mod,
1552+
fn proc_macro_gen = proc_macro_gen,
15031553
fn proc_macro_expr = proc_macro_expr,
15041554
fn proc_macro_non_items = proc_macro_non_items,
15051555
}

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)