Skip to content

Commit e4c64a1

Browse files
committed
Auto merge of #25713 - Stebalien:pattern, r=alexcrichton
Needed to support: ```rust match X { pattern if Y ... } for pattern in Y {} ``` IMO, this shouldn't require an RFC because it can't interfere with any future language changes (because `pattern if` and `pattern in` are already legal in rust) and can't cause any ambiguity.
2 parents fe85342 + f21655e commit e4c64a1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/doc/trpl/macros.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ There are additional rules regarding the next token after a metavariable:
478478

479479
* `expr` variables may only be followed by one of: `=> , ;`
480480
* `ty` and `path` variables may only be followed by one of: `=> , : = > as`
481-
* `pat` variables may only be followed by one of: `=> , =`
481+
* `pat` variables may only be followed by one of: `=> , = if in`
482482
* Other variables may be followed by any token.
483483

484484
These rules provide some flexibility for Rust’s syntax to evolve without

src/libsyntax/ext/tt/macro_rules.rs

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ fn is_in_follow(_: &ExtCtxt, tok: &Token, frag: &str) -> Result<bool, String> {
495495
"pat" => {
496496
match *tok {
497497
FatArrow | Comma | Eq => Ok(true),
498+
Ident(i, _) if i.as_str() == "if" || i.as_str() == "in" => Ok(true),
498499
_ => Ok(false)
499500
}
500501
},

src/test/run-pass/macro-pat-follow.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 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+
macro_rules! pat_in {
12+
($p:pat in $e:expr) => {{
13+
let mut iter = $e.into_iter();
14+
while let $p = iter.next() {}
15+
}}
16+
}
17+
18+
macro_rules! pat_if {
19+
($p:pat if $e:expr) => {{
20+
match Some(1u8) {
21+
$p if $e => {},
22+
_ => {}
23+
}
24+
}}
25+
}
26+
27+
fn main() {
28+
pat_in!(Some(_) in 0..10);
29+
pat_if!(Some(x) if x > 0);
30+
}

0 commit comments

Comments
 (0)