Skip to content

Commit 19e0c98

Browse files
committed
Don't gate the feature twice
1 parent e274372 commit 19e0c98

File tree

4 files changed

+31
-51
lines changed

4 files changed

+31
-51
lines changed

compiler/rustc_ast/src/ast.rs

+13
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,19 @@ impl Pat {
676676
});
677677
could_be_never_pattern
678678
}
679+
680+
/// Whether this contains a `!` pattern. This in particular means that a feature gate error will
681+
/// be raised if the feature is off. Used to avoid gating the feature twice.
682+
pub fn contains_never_pattern(&self) -> bool {
683+
let mut contains_never_pattern = false;
684+
self.walk(&mut |pat| {
685+
if matches!(pat.kind, PatKind::Never) {
686+
contains_never_pattern = true;
687+
}
688+
true
689+
});
690+
contains_never_pattern
691+
}
679692
}
680693

681694
/// A single field in a struct pattern.

compiler/rustc_parse/src/parser/expr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,10 @@ impl<'a> Parser<'a> {
29202920
arm_body = None;
29212921
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)]).map(
29222922
|x| {
2923-
this.sess.gated_spans.gate(sym::never_patterns, pat.span);
2923+
// Don't gate twice
2924+
if !pat.contains_never_pattern() {
2925+
this.sess.gated_spans.gate(sym::never_patterns, pat.span);
2926+
}
29242927
x
29252928
},
29262929
)

tests/ui/feature-gates/feature-gate-never_patterns.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ fn main() {
1414
match *ptr {
1515
!
1616
//~^ ERROR `!` patterns are experimental
17-
//~| ERROR `!` patterns are experimental
1817
}
1918
// Check that the gate operates even behind `cfg`.
2019
#[cfg(FALSE)]
2120
match *ptr {
2221
!
2322
//~^ ERROR `!` patterns are experimental
24-
//~| ERROR `!` patterns are experimental
2523
}
2624
#[cfg(FALSE)]
2725
match *ptr {
@@ -51,14 +49,12 @@ fn main() {
5149
match res {
5250
Ok(_) => {}
5351
Err(!),
54-
//~^ ERROR `match` arm with no body
55-
//~| ERROR `!` patterns are experimental
52+
//~^ ERROR `!` patterns are experimental
5653
}
5754
match res {
5855
Err(!) if false,
59-
//~^ ERROR `match` arm with no body
56+
//~^ ERROR `!` patterns are experimental
6057
//~| ERROR a guard on a never pattern will never be run
61-
//~| ERROR `!` patterns are experimental
6258
_ => {}
6359
}
6460

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unexpected `,` in pattern
2-
--> $DIR/feature-gate-never_patterns.rs:36:16
2+
--> $DIR/feature-gate-never_patterns.rs:34:16
33
|
44
LL | Some(_),
55
| ^
@@ -40,17 +40,7 @@ LL | !
4040
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
4141

4242
error[E0658]: `!` patterns are experimental
43-
--> $DIR/feature-gate-never_patterns.rs:15:13
44-
|
45-
LL | !
46-
| ^
47-
|
48-
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
49-
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
50-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
51-
52-
error[E0658]: `!` patterns are experimental
53-
--> $DIR/feature-gate-never_patterns.rs:22:13
43+
--> $DIR/feature-gate-never_patterns.rs:21:13
5444
|
5545
LL | !
5646
| ^
@@ -59,17 +49,7 @@ LL | !
5949
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
6050

6151
error[E0658]: `!` patterns are experimental
62-
--> $DIR/feature-gate-never_patterns.rs:22:13
63-
|
64-
LL | !
65-
| ^
66-
|
67-
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
68-
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
69-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
70-
71-
error[E0658]: `!` patterns are experimental
72-
--> $DIR/feature-gate-never_patterns.rs:28:13
52+
--> $DIR/feature-gate-never_patterns.rs:26:13
7353
|
7454
LL | ! => {}
7555
| ^
@@ -78,40 +58,34 @@ LL | ! => {}
7858
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
7959

8060
error: `match` arm with no body
81-
--> $DIR/feature-gate-never_patterns.rs:41:9
61+
--> $DIR/feature-gate-never_patterns.rs:39:9
8262
|
8363
LL | Some(_)
8464
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
8565

8666
error: `match` arm with no body
87-
--> $DIR/feature-gate-never_patterns.rs:46:9
67+
--> $DIR/feature-gate-never_patterns.rs:44:9
8868
|
8969
LL | Some(_) if false,
9070
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
9171

9272
error: `match` arm with no body
93-
--> $DIR/feature-gate-never_patterns.rs:48:9
73+
--> $DIR/feature-gate-never_patterns.rs:46:9
9474
|
9575
LL | Some(_) if false
9676
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
9777

9878
error[E0658]: `!` patterns are experimental
99-
--> $DIR/feature-gate-never_patterns.rs:53:13
79+
--> $DIR/feature-gate-never_patterns.rs:51:13
10080
|
10181
LL | Err(!),
10282
| ^
10383
|
10484
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
10585
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
10686

107-
error: `match` arm with no body
108-
--> $DIR/feature-gate-never_patterns.rs:53:9
109-
|
110-
LL | Err(!),
111-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
112-
11387
error[E0658]: `!` patterns are experimental
114-
--> $DIR/feature-gate-never_patterns.rs:58:13
88+
--> $DIR/feature-gate-never_patterns.rs:55:13
11589
|
11690
LL | Err(!) if false,
11791
| ^
@@ -120,30 +94,24 @@ LL | Err(!) if false,
12094
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
12195

12296
error: `match` arm with no body
123-
--> $DIR/feature-gate-never_patterns.rs:58:9
124-
|
125-
LL | Err(!) if false,
126-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
127-
128-
error: `match` arm with no body
129-
--> $DIR/feature-gate-never_patterns.rs:69:9
97+
--> $DIR/feature-gate-never_patterns.rs:65:9
13098
|
13199
LL | Some(_)
132100
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
133101

134102
error: `match` arm with no body
135-
--> $DIR/feature-gate-never_patterns.rs:75:9
103+
--> $DIR/feature-gate-never_patterns.rs:71:9
136104
|
137105
LL | Some(_) if false
138106
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
139107

140108
error: a guard on a never pattern will never be run
141-
--> $DIR/feature-gate-never_patterns.rs:58:19
109+
--> $DIR/feature-gate-never_patterns.rs:55:19
142110
|
143111
LL | Err(!) if false,
144112
| ^^^^^ help: remove this guard
145113

146-
error: aborting due to 18 previous errors
114+
error: aborting due to 14 previous errors
147115

148116
Some errors have detailed explanations: E0408, E0658.
149117
For more information about an error, try `rustc --explain E0408`.

0 commit comments

Comments
 (0)