Skip to content

Commit da9e0be

Browse files
committed
Allow | after pat in macro_rules!
Per rust-lang#20563 (comment) This enables writing macros that accept `$($pattern:pat)|+` and expand to the same thing in a `match` statement. See for example [the `matches!` macro](https://github.com/SimonSapin/rust-std-candidates#the-matches-macro).
1 parent 2e2372c commit da9e0be

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ext::tt::macro_parser::{parse, parse_or_else};
1919
use parse::lexer::{new_tt_reader, new_tt_reader_with_doc_flag};
2020
use parse::parser::Parser;
2121
use parse::attr::ParserAttr;
22-
use parse::token::{special_idents, gensym_ident, NtTT, Token};
22+
use parse::token::{special_idents, gensym_ident, NtTT, Token, BinOpToken};
2323
use parse::token::Token::*;
2424
use parse::token;
2525
use print;
@@ -433,7 +433,7 @@ fn is_in_follow(cx: &ExtCtxt, tok: &Token, frag: &str) -> bool {
433433
},
434434
"pat" => {
435435
match *tok {
436-
FatArrow | Comma | Eq => true,
436+
FatArrow | Comma | Eq | BinOp(BinOpToken::Or) => true,
437437
_ => false
438438
}
439439
},

src/test/compile-fail/macro-input-future-proofing.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ macro_rules! errors_everywhere {
1818
($bl:block < ) => ();
1919
($pa:pat >) => (); //~ ERROR `$pa:pat` is followed by `>`, which is not allowed for `pat`
2020
($pa:pat , ) => ();
21-
($pa:pat | ) => (); //~ ERROR `$pa:pat` is followed by `|`
2221
($pa:pat $pb:pat $ty:ty ,) => ();
2322
//~^ ERROR `$pa:pat` is followed by `$pb:pat`, which is not allowed
2423
//~^^ ERROR `$pb:pat` is followed by `$ty:ty`, which is not allowed

src/test/run-pass/matches-macro.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 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+
12+
//! https://github.com/rust-lang/rust/pull/20824
13+
14+
// ignore-pretty
15+
16+
macro_rules! matches {
17+
($expression: expr, $($pattern:pat)|+) => {
18+
matches!($expression, $($pattern)|+ if true)
19+
};
20+
($expression: expr, $($pattern:pat)|+ if $guard: expr) => {
21+
match $expression {
22+
$($pattern)|+ => $guard,
23+
_ => false
24+
}
25+
};
26+
}
27+
28+
pub fn main() {
29+
let foo = Some("-12");
30+
assert!(matches!(foo, Some(bar) if
31+
matches!(bar.char_at(0), '+' | '-') &&
32+
matches!(bar.char_at(1), '0'...'9')
33+
));
34+
}

0 commit comments

Comments
 (0)