From 19e0c984d3ff44c6e273ddae2f327e8ad8726fae Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 12 Dec 2023 14:52:05 +0100 Subject: [PATCH] Don't gate the feature twice --- compiler/rustc_ast/src/ast.rs | 13 +++++ compiler/rustc_parse/src/parser/expr.rs | 5 +- .../feature-gate-never_patterns.rs | 8 +-- .../feature-gate-never_patterns.stderr | 56 ++++--------------- 4 files changed, 31 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 190fae95652..5755ae8a8bc 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -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. diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index bdaf8db1311..5b0011e9f70 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -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 }, ) diff --git a/tests/ui/feature-gates/feature-gate-never_patterns.rs b/tests/ui/feature-gates/feature-gate-never_patterns.rs index 97c68e460ea..f3910622313 100644 --- a/tests/ui/feature-gates/feature-gate-never_patterns.rs +++ b/tests/ui/feature-gates/feature-gate-never_patterns.rs @@ -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 _ => {} } diff --git a/tests/ui/feature-gates/feature-gate-never_patterns.stderr b/tests/ui/feature-gates/feature-gate-never_patterns.stderr index 4ba80603d9f..dd10829d495 100644 --- a/tests/ui/feature-gates/feature-gate-never_patterns.stderr +++ b/tests/ui/feature-gates/feature-gate-never_patterns.stderr @@ -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 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 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 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`.