Don't gate the feature twice

This commit is contained in:
Nadrieril 2023-12-12 14:52:05 +01:00
parent e274372689
commit 19e0c984d3
4 changed files with 31 additions and 51 deletions

View File

@ -676,6 +676,19 @@ impl Pat {
});
could_be_never_pattern
}
/// Whether this contains a `!` pattern. This in particular means that a feature gate error will
/// be raised if the feature is off. Used to avoid gating the feature twice.
pub fn contains_never_pattern(&self) -> bool {
let mut contains_never_pattern = false;
self.walk(&mut |pat| {
if matches!(pat.kind, PatKind::Never) {
contains_never_pattern = true;
}
true
});
contains_never_pattern
}
}
/// A single field in a struct pattern.

View File

@ -2920,7 +2920,10 @@ impl<'a> Parser<'a> {
arm_body = None;
this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)]).map(
|x| {
this.sess.gated_spans.gate(sym::never_patterns, pat.span);
// Don't gate twice
if !pat.contains_never_pattern() {
this.sess.gated_spans.gate(sym::never_patterns, pat.span);
}
x
},
)

View File

@ -14,14 +14,12 @@ fn main() {
match *ptr {
!
//~^ ERROR `!` patterns are experimental
//~| ERROR `!` patterns are experimental
}
// Check that the gate operates even behind `cfg`.
#[cfg(FALSE)]
match *ptr {
!
//~^ ERROR `!` patterns are experimental
//~| ERROR `!` patterns are experimental
}
#[cfg(FALSE)]
match *ptr {
@ -51,14 +49,12 @@ fn main() {
match res {
Ok(_) => {}
Err(!),
//~^ ERROR `match` arm with no body
//~| ERROR `!` patterns are experimental
//~^ ERROR `!` patterns are experimental
}
match res {
Err(!) if false,
//~^ ERROR `match` arm with no body
//~^ ERROR `!` patterns are experimental
//~| ERROR a guard on a never pattern will never be run
//~| ERROR `!` patterns are experimental
_ => {}
}

View File

@ -1,5 +1,5 @@
error: unexpected `,` in pattern
--> $DIR/feature-gate-never_patterns.rs:36:16
--> $DIR/feature-gate-never_patterns.rs:34:16
|
LL | Some(_),
| ^
@ -40,17 +40,7 @@ LL | !
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:15:13
|
LL | !
| ^
|
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:22:13
--> $DIR/feature-gate-never_patterns.rs:21:13
|
LL | !
| ^
@ -59,17 +49,7 @@ LL | !
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:22:13
|
LL | !
| ^
|
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:28:13
--> $DIR/feature-gate-never_patterns.rs:26:13
|
LL | ! => {}
| ^
@ -78,25 +58,25 @@ LL | ! => {}
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:41:9
--> $DIR/feature-gate-never_patterns.rs:39:9
|
LL | Some(_)
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:46:9
--> $DIR/feature-gate-never_patterns.rs:44:9
|
LL | Some(_) if false,
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:48:9
--> $DIR/feature-gate-never_patterns.rs:46:9
|
LL | Some(_) if false
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:53:13
--> $DIR/feature-gate-never_patterns.rs:51:13
|
LL | Err(!),
| ^
@ -104,14 +84,8 @@ LL | Err(!),
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:53:9
|
LL | Err(!),
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
error[E0658]: `!` patterns are experimental
--> $DIR/feature-gate-never_patterns.rs:58:13
--> $DIR/feature-gate-never_patterns.rs:55:13
|
LL | Err(!) if false,
| ^
@ -120,30 +94,24 @@ LL | Err(!) if false,
= help: add `#![feature(never_patterns)]` to the crate attributes to enable
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:58:9
|
LL | Err(!) if false,
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:69:9
--> $DIR/feature-gate-never_patterns.rs:65:9
|
LL | Some(_)
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
error: `match` arm with no body
--> $DIR/feature-gate-never_patterns.rs:75:9
--> $DIR/feature-gate-never_patterns.rs:71:9
|
LL | Some(_) if false
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
error: a guard on a never pattern will never be run
--> $DIR/feature-gate-never_patterns.rs:58:19
--> $DIR/feature-gate-never_patterns.rs:55:19
|
LL | Err(!) if false,
| ^^^^^ help: remove this guard
error: aborting due to 18 previous errors
error: aborting due to 14 previous errors
Some errors have detailed explanations: E0408, E0658.
For more information about an error, try `rustc --explain E0408`.