Skip to content

Commit 49bc67f

Browse files
committed
Document behavior of ! with MbE
1 parent 8d39ec1 commit 49bc67f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/macros.rs:34:39
3+
|
4+
LL | assert_eq!(detect_pat!(true | !), Pattern);
5+
| ^^^^^^^ expected `Other`, found `Pattern`
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// revisions: e2018 e2021
2+
//[e2018] edition:2018
3+
//[e2021] edition:2021
4+
//[e2021] check-pass
5+
#![feature(never_patterns)]
6+
#![allow(incomplete_features)]
7+
8+
#[derive(Debug, PartialEq, Eq)]
9+
struct Pattern;
10+
#[derive(Debug, PartialEq, Eq)]
11+
struct Never;
12+
#[derive(Debug, PartialEq, Eq)]
13+
struct Other;
14+
15+
macro_rules! detect_pat {
16+
($p:pat) => {
17+
Pattern
18+
};
19+
(!) => {
20+
Never
21+
};
22+
($($x:tt)*) => {
23+
Other
24+
};
25+
}
26+
27+
fn main() {
28+
// For backwards compatibility this does not match `$p:pat`. This is similar to how or-patterns
29+
// without parens don't match `$p:pat` either.
30+
assert_eq!(detect_pat!(!), Never);
31+
32+
// Edition 2018 parses the first one as `Other`.
33+
// FIXME(never_patterns): Neither edition parses the second one as a pattern.
34+
assert_eq!(detect_pat!(true | !), Pattern);
35+
//[e2018]~^ ERROR mismatched types
36+
assert_eq!(detect_pat!(! | true), Other);
37+
38+
// These are never patterns; they take no body when they're in a match arm.
39+
assert_eq!(detect_pat!((!)), Pattern);
40+
assert_eq!(detect_pat!((true, !)), Pattern);
41+
assert_eq!(detect_pat!(Some(!)), Pattern);
42+
43+
// These count as normal patterns.
44+
assert_eq!(detect_pat!((! | true)), Pattern);
45+
assert_eq!(detect_pat!((Ok(x) | Err(&!))), Pattern);
46+
}

tests/ui/rfcs/rfc-0000-never_patterns/parse.rs

+5
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ fn parse(x: Void) {
6868
//~^ ERROR top-level or-patterns are not allowed in `let` bindings
6969
let (Ok(_) | Err(!)) = &res;
7070
let (Ok(_) | Err(&!)) = res.as_ref();
71+
72+
let ! = x;
73+
let y @ ! = x;
7174
}
75+
76+
fn foo(!: Void) {}

0 commit comments

Comments
 (0)