-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always capture tokens for
macro_rules!
arguments
- Loading branch information
Showing
9 changed files
with
205 additions
and
5 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
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,20 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream, TokenTree, Group, Delimiter}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn first(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let tokens: TokenStream = "#[derive(Second)]".parse().unwrap(); | ||
let wrapped = TokenTree::Group(Group::new(Delimiter::None, item.into_iter().collect())); | ||
tokens.into_iter().chain(std::iter::once(wrapped)).collect() | ||
} | ||
|
||
#[proc_macro_derive(Second)] | ||
pub fn second(item: TokenStream) -> TokenStream { | ||
TokenStream::new() | ||
} |
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,12 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
use proc_macro::TokenStream; | ||
|
||
#[proc_macro] | ||
pub fn recollect(tokens: TokenStream) -> TokenStream { | ||
tokens.into_iter().collect() | ||
} |
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,48 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream, TokenTree, Group}; | ||
|
||
fn find_my_ident(tokens: TokenStream) -> Option<TokenStream> { | ||
for token in tokens { | ||
if let TokenTree::Ident(ident) = &token { | ||
if ident.to_string() == "hidden_ident" { | ||
return Some(vec![token].into_iter().collect()) | ||
} | ||
} else if let TokenTree::Group(g) = token { | ||
if let Some(stream) = find_my_ident(g.stream()) { | ||
return Some(stream) | ||
} | ||
} | ||
} | ||
return None; | ||
} | ||
|
||
|
||
#[proc_macro_derive(WeirdDerive)] | ||
pub fn weird_derive(item: TokenStream) -> TokenStream { | ||
let my_ident = find_my_ident(item).expect("Missing 'my_ident'!"); | ||
let tokens: TokenStream = "call_it!();".parse().unwrap(); | ||
let final_call = tokens.into_iter().map(|tree| { | ||
if let TokenTree::Group(g) = tree { | ||
return Group::new(g.delimiter(), my_ident.clone()).into() | ||
} else { | ||
return tree | ||
} | ||
}).collect(); | ||
final_call | ||
} | ||
|
||
#[proc_macro] | ||
pub fn recollect(item: TokenStream) -> TokenStream { | ||
item.into_iter().collect() | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn recollect_attr(_attr: TokenStream, mut item: TokenStream) -> TokenStream { | ||
item.into_iter().collect() | ||
} |
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,22 @@ | ||
// aux-build:test-macros.rs | ||
// check-pass | ||
|
||
extern crate test_macros; | ||
use test_macros::recollect; | ||
|
||
macro_rules! use_expr { | ||
($expr:expr) => { | ||
recollect!($expr) | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
struct Foo; | ||
impl Foo { | ||
#[allow(dead_code)] | ||
fn use_self(self) { | ||
drop(use_expr!(self)); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// aux-build:first-second.rs | ||
// FIXME: The spans here are bad, see PR #73084 | ||
|
||
extern crate first_second; | ||
use first_second::*; | ||
|
||
macro_rules! produce_it { | ||
($name:ident) => { | ||
#[first] //~ ERROR cannot find type | ||
struct $name { | ||
field: MissingType | ||
} | ||
} | ||
} | ||
|
||
produce_it!(MyName); | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
} |
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,9 @@ | ||
error[E0412]: cannot find type `MissingType` in this scope | ||
--> $DIR/macro-rules-derive.rs:9:9 | ||
| | ||
LL | #[first] | ||
| ^^^^^^^^ not found in this scope | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0412`. |
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,48 @@ | ||
// aux-build:weird-hygiene.rs | ||
// check-pass | ||
// FIXME: This should actually error, see PR #73084 | ||
|
||
#![feature(stmt_expr_attributes)] | ||
#![feature(proc_macro_hygiene)] | ||
|
||
extern crate weird_hygiene; | ||
use weird_hygiene::*; | ||
|
||
macro_rules! other { | ||
($tokens:expr) => { | ||
macro_rules! call_it { | ||
($outer_ident:ident) => { | ||
macro_rules! inner { | ||
() => { | ||
$outer_ident; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(WeirdDerive)] | ||
enum MyEnum { | ||
Value = (stringify!($tokens + hidden_ident), 1).1 | ||
} | ||
|
||
inner!(); | ||
} | ||
} | ||
|
||
macro_rules! invoke_it { | ||
($token:expr) => { | ||
#[recollect_attr] { | ||
$token; | ||
hidden_ident | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
// `other` and `invoke_it` are both macro_rules! macros, | ||
// so it should be impossible for them to ever see `hidden_ident`, | ||
// even if they invoke a proc macro. | ||
let hidden_ident = "Hello1"; | ||
other!(50); | ||
invoke_it!(25); | ||
} |