about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2023-12-12 14:52:05 +0100
committerNadrieril <nadrieril+git@gmail.com>2023-12-12 14:52:05 +0100
commit19e0c984d3ff44c6e273ddae2f327e8ad8726fae (patch)
tree7a7a3ea8a9f032d325a4e3c8d457fc7bedf40bb8
parente274372689928972e4e78a24d615f6c4d375f7d4 (diff)
downloadrust-19e0c984d3ff44c6e273ddae2f327e8ad8726fae.tar.gz
rust-19e0c984d3ff44c6e273ddae2f327e8ad8726fae.zip
Don't gate the feature twice
-rw-r--r--compiler/rustc_ast/src/ast.rs13
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs5
-rw-r--r--tests/ui/feature-gates/feature-gate-never_patterns.rs8
-rw-r--r--tests/ui/feature-gates/feature-gate-never_patterns.stderr56
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 <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`.