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/test | |
| 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/test')
4 files changed, 97 insertions, 1 deletions
diff --git a/src/test/compile-fail-fulldeps/proc-macro/auxiliary/more-gates.rs b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/more-gates.rs new file mode 100644 index 00000000000..def12f8e4ec --- /dev/null +++ b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/more-gates.rs @@ -0,0 +1,56 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// no-prefer-dynamic + +#![crate_type = "proc-macro"] +#![feature(proc_macro)] + +extern crate proc_macro; + +use proc_macro::*; + +#[proc_macro_attribute] +pub fn attr2mod(_: TokenStream, _: TokenStream) -> TokenStream { + "mod test {}".parse().unwrap() +} + +#[proc_macro_attribute] +pub fn attr2mac1(_: TokenStream, _: TokenStream) -> TokenStream { + "macro_rules! foo1 { (a) => (a) }".parse().unwrap() +} + +#[proc_macro_attribute] +pub fn attr2mac2(_: TokenStream, _: TokenStream) -> TokenStream { + "macro foo2(a) { a }".parse().unwrap() +} + +#[proc_macro] +pub fn mac2mod(_: TokenStream) -> TokenStream { + "mod test2 {}".parse().unwrap() +} + +#[proc_macro] +pub fn mac2mac1(_: TokenStream) -> TokenStream { + "macro_rules! foo3 { (a) => (a) }".parse().unwrap() +} + +#[proc_macro] +pub fn mac2mac2(_: TokenStream) -> TokenStream { + "macro foo4(a) { a }".parse().unwrap() +} + +#[proc_macro] +pub fn tricky(_: TokenStream) -> TokenStream { + "fn foo() { + mod test {} + macro_rules! foo { (a) => (a) } + }".parse().unwrap() +} diff --git a/src/test/compile-fail-fulldeps/proc-macro/more-gates.rs b/src/test/compile-fail-fulldeps/proc-macro/more-gates.rs new file mode 100644 index 00000000000..a799f79ef70 --- /dev/null +++ b/src/test/compile-fail-fulldeps/proc-macro/more-gates.rs @@ -0,0 +1,37 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:more-gates.rs + +#![feature(proc_macro)] + +extern crate more_gates as foo; + +use foo::*; + +#[attr2mod] +//~^ ERROR: cannot expand to modules +pub fn a() {} +#[attr2mac1] +//~^ ERROR: cannot expand to macro definitions +pub fn a() {} +#[attr2mac2] +//~^ ERROR: cannot expand to macro definitions +pub fn a() {} + +mac2mod!(); //~ ERROR: cannot expand to modules +mac2mac1!(); //~ ERROR: cannot expand to macro definitions +mac2mac2!(); //~ ERROR: cannot expand to macro definitions + +tricky!(); +//~^ ERROR: cannot expand to modules +//~| ERROR: cannot expand to macro definitions + +fn main() {} diff --git a/src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs index fff433b90ce..70b2b5fdd33 100644 --- a/src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs +++ b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs @@ -14,6 +14,7 @@ // gate-test-proc_macro_mod line // gate-test-proc_macro_expr // gate-test-proc_macro_mod +// gate-test-proc_macro_gen #![feature(proc_macro, stmt_expr_attributes)] @@ -29,10 +30,12 @@ fn _test_inner() { } #[a] //~ ERROR: custom attributes cannot be applied to modules +//~| ERROR: procedural macros cannot expand to modules mod _test2 {} mod _test2_inner { #![a] //~ ERROR: custom attributes cannot be applied to modules + //~| ERROR: procedural macros cannot expand to modules } #[a = y] //~ ERROR: must only be followed by a delimiter token diff --git a/src/test/run-pass-fulldeps/macro-quote-test.rs b/src/test/run-pass-fulldeps/macro-quote-test.rs index 1f6a340c7e8..2349fa68c65 100644 --- a/src/test/run-pass-fulldeps/macro-quote-test.rs +++ b/src/test/run-pass-fulldeps/macro-quote-test.rs @@ -13,7 +13,7 @@ // aux-build:hello_macro.rs // ignore-stage1 -#![feature(use_extern_macros, proc_macro_non_items)] +#![feature(use_extern_macros, proc_macro_non_items, proc_macro_gen)] extern crate hello_macro; |
