Skip to content

Commit

Permalink
Don't gate the feature twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 12, 2023
1 parent e274372 commit 19e0c98
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
13 changes: 13 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
Expand Down
8 changes: 2 additions & 6 deletions tests/ui/feature-gates/feature-gate-never_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
_ => {}
}

Expand Down
56 changes: 12 additions & 44 deletions tests/ui/feature-gates/feature-gate-never_patterns.stderr
Original file line number Diff line number Diff line change
@@ -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(_),
| ^
Expand Down Expand Up @@ -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 | !
| ^
Expand All @@ -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 | ! => {}
| ^
Expand All @@ -78,40 +58,34 @@ 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(!),
| ^
|
= 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,
| ^
Expand All @@ -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`.

0 comments on commit 19e0c98

Please sign in to comment.