Skip to content

Commit

Permalink
Rollup merge of rust-lang#54977 - estebank:macro-arg-parse, r=pnkfelix
Browse files Browse the repository at this point in the history
Accept `Option<Box<$t:ty>>` in macro argument

Given the following code, compile successfuly:

```
macro_rules! test {
    (
        fn fun() -> Option<Box<$t:ty>>;
    ) => {
        fn fun(x: $t) -> Option<Box<$t>>
        { Some(Box::new(x)) }
    }
}

test! {
    fn fun() -> Option<Box<i32>>;
}
```

Fix rust-lang#25274.
  • Loading branch information
pietroalbini committed Oct 25, 2018
2 parents 699f591 + c77a0cf commit f81e47f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,8 @@ fn is_in_follow(tok: &quoted::TokenTree, frag: &str) -> Result<bool, (String, &'
"path" | "ty" => match *tok {
TokenTree::Token(_, ref tok) => match *tok {
OpenDelim(token::DelimToken::Brace) | OpenDelim(token::DelimToken::Bracket) |
Comma | FatArrow | Colon | Eq | Gt | Semi | BinOp(token::Or) => Ok(true),
Comma | FatArrow | Colon | Eq | Gt | BinOp(token::Shr) | Semi |
BinOp(token::Or) => Ok(true),
Ident(i, false) if i.name == "as" || i.name == "where" => Ok(true),
_ => Ok(false)
},
Expand Down
16 changes: 16 additions & 0 deletions src/test/run-pass/macros/issue-25274.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
macro_rules! test {
(
fn fun() -> Option<Box<$t:ty>>;
) => {
fn fun(x: $t) -> Option<Box<$t>>
{ Some(Box::new(x)) }
}
}

test! {
fn fun() -> Option<Box<i32>>;
}

fn main() {
println!("{}", fun(0).unwrap());
}

0 comments on commit f81e47f

Please sign in to comment.