Skip to content

Commit d9e47be

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

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 9 additions & 0 deletions
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`.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// For backwards-compatibility, all the cases that parse as `Pattern` under the feature gate must
28+
// have been parse errors before.
29+
fn main() {
30+
// For backwards compatibility this does not match `$p:pat`.
31+
assert_eq!(detect_pat!(!), Never);
32+
33+
// Edition 2018 parses both of these cases as `Other`. Both editions have been parsing the
34+
// second case as `Other` before, so we mustn't change that.
35+
assert_eq!(detect_pat!(true | !), Pattern);
36+
//[e2018]~^ ERROR mismatched types
37+
assert_eq!(detect_pat!(! | true), Other);
38+
39+
// These are never patterns; they take no body when they're in a match arm.
40+
assert_eq!(detect_pat!((!)), Pattern);
41+
assert_eq!(detect_pat!((true, !)), Pattern);
42+
assert_eq!(detect_pat!(Some(!)), Pattern);
43+
44+
// These count as normal patterns.
45+
assert_eq!(detect_pat!((! | true)), Pattern);
46+
assert_eq!(detect_pat!((Ok(x) | Err(&!))), Pattern);
47+
}

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

Lines changed: 5 additions & 0 deletions
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)