about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-30 22:44:33 +0000
committerbors <bors@rust-lang.org>2023-12-30 22:44:33 +0000
commitd868bc28422a5b296ba6a23a83384fbfefa40264 (patch)
tree84e52466de7535e334787e2262326293526db7ee
parent2a3e63551fe21458637480a97b65a2d15dec8062 (diff)
parentd5b2d88a1aa4c0832c54415a78b7c8905926d6f7 (diff)
downloadrust-d868bc28422a5b296ba6a23a83384fbfefa40264.tar.gz
rust-d868bc28422a5b296ba6a23a83384fbfefa40264.zip
Auto merge of #119284 - Nadrieril:fix-bodiless-arm-parse, r=cjgillot
Don't drop a hir node after lowering

Fixes https://github.com/rust-lang/rust/issues/119271.

It seems that all hir nodes that get allocated an id must be placed within the hir on pain of ICEs. In https://github.com/rust-lang/rust/pull/118527 I dropped guards on never patterns since they're not useful, which caused the ICE.
-rw-r--r--compiler/rustc_ast_lowering/src/expr.rs4
-rw-r--r--tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.rs10
-rw-r--r--tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.stderr27
3 files changed, 38 insertions, 3 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs
index e568da9bbc0..ba858d49acf 100644
--- a/compiler/rustc_ast_lowering/src/expr.rs
+++ b/compiler/rustc_ast_lowering/src/expr.rs
@@ -546,7 +546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
 
     fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
         let pat = self.lower_pat(&arm.pat);
-        let mut guard = arm.guard.as_ref().map(|cond| {
+        let guard = arm.guard.as_ref().map(|cond| {
             if let ExprKind::Let(pat, scrutinee, span, is_recovered) = &cond.kind {
                 hir::Guard::IfLet(self.arena.alloc(hir::Let {
                     hir_id: self.next_id(),
@@ -578,10 +578,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 }
             } else if let Some(body) = &arm.body {
                 self.dcx().emit_err(NeverPatternWithBody { span: body.span });
-                guard = None;
             } else if let Some(g) = &arm.guard {
                 self.dcx().emit_err(NeverPatternWithGuard { span: g.span });
-                guard = None;
             }
 
             // We add a fake `loop {}` arm body so that it typecks to `!`.
diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.rs b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.rs
new file mode 100644
index 00000000000..2490909b6a5
--- /dev/null
+++ b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.rs
@@ -0,0 +1,10 @@
+fn main() {}
+
+fn attr_in_guard() {
+    match None::<u32> {
+        Some(!) //~ ERROR `!` patterns are experimental
+            if #[deny(unused_mut)] //~ ERROR attributes on expressions are experimental
+            false //~ ERROR a guard on a never pattern will never be run
+    }
+    match false {}
+}
diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.stderr b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.stderr
new file mode 100644
index 00000000000..335e6c6db5f
--- /dev/null
+++ b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-119271-never-arm-attr-in-guard.stderr
@@ -0,0 +1,27 @@
+error[E0658]: attributes on expressions are experimental
+  --> $DIR/ICE-119271-never-arm-attr-in-guard.rs:6:16
+   |
+LL |             if #[deny(unused_mut)]
+   |                ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
+   = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
+
+error[E0658]: `!` patterns are experimental
+  --> $DIR/ICE-119271-never-arm-attr-in-guard.rs:5:14
+   |
+LL |         Some(!)
+   |              ^
+   |
+   = 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: a guard on a never pattern will never be run
+  --> $DIR/ICE-119271-never-arm-attr-in-guard.rs:7:13
+   |
+LL |             false
+   |             ^^^^^ help: remove this guard
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0658`.