-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/compile-fail-fulldeps/proc-macro/auxiliary/more-gates.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters