about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-08-19 23:33:22 +0000
committerbors <bors@rust-lang.org>2021-08-19 23:33:22 +0000
commitebedfedcd82678d5b0592227ca5e6ca31a5afb8f (patch)
tree52a709f2598af445bf76dc722926b8a1c82c6dd8
parent6d64f7f695943541fe12bb960971403f440d7225 (diff)
parent2e61659bd1f3998520abc77f5459c529083a7a3d (diff)
downloadrust-ebedfedcd82678d5b0592227ca5e6ca31a5afb8f.tar.gz
rust-ebedfedcd82678d5b0592227ca5e6ca31a5afb8f.zip
Auto merge of #87996 - sexxi-goose:fix-87988, r=nikomatsakis
RFC2229 Add missing edge case

Closes https://github.com/rust-lang/rust/issues/87988

This PR fixes an ICE where a match discriminant is not being read when expected. This ICE was the result of a missing edge case which assumed that if a pattern is of type `PatKind::TupleStruct(..) | PatKind::Path(..) | PatKind::Struct(..) | PatKind::Tuple(..)` then a place could only be a multi variant if the place is of type kind Adt.
-rw-r--r--compiler/rustc_typeck/src/expr_use_visitor.rs3
-rw-r--r--src/test/ui/closures/2229_closure_analysis/issue-87988.rs19
-rw-r--r--src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs44
3 files changed, 66 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs
index 3b241317aa3..b0c95939bb7 100644
--- a/compiler/rustc_typeck/src/expr_use_visitor.rs
+++ b/compiler/rustc_typeck/src/expr_use_visitor.rs
@@ -269,6 +269,9 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
                                     if def.variants.len() > 1 {
                                         needs_to_be_read = true;
                                     }
+                                } else {
+                                    // If it is not ty::Adt, then it should be read
+                                    needs_to_be_read = true;
                                 }
                             }
                             PatKind::Lit(_) | PatKind::Range(..) => {
diff --git a/src/test/ui/closures/2229_closure_analysis/issue-87988.rs b/src/test/ui/closures/2229_closure_analysis/issue-87988.rs
new file mode 100644
index 00000000000..27e7fabf11a
--- /dev/null
+++ b/src/test/ui/closures/2229_closure_analysis/issue-87988.rs
@@ -0,0 +1,19 @@
+// run-pass
+// edition:2021
+
+const LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: i32 = 0x01;
+const LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: i32 = 0x02;
+
+pub fn hotplug_callback(event: i32) {
+    let _ = || {
+        match event {
+            LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED => (),
+            LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT => (),
+            _ => (),
+        };
+    };
+}
+
+fn main() {
+    hotplug_callback(1);
+}
diff --git a/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs b/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs
new file mode 100644
index 00000000000..914ebbe26a5
--- /dev/null
+++ b/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs
@@ -0,0 +1,44 @@
+// run-pass
+// edition:2021
+
+const PATTERN_REF: &str = "Hello World";
+const NUMBER: i32 = 30;
+const NUMBER_POINTER: *const i32 = &NUMBER;
+
+pub fn edge_case_ref(event: &str) {
+    let _ = || {
+        match event {
+            PATTERN_REF => (),
+            _ => (),
+        };
+    };
+}
+
+pub fn edge_case_str(event: String) {
+    let _ = || {
+        match event.as_str() {
+            "hello" => (),
+            _ => (),
+        };
+    };
+}
+
+pub fn edge_case_raw_ptr(event: *const i32) {
+    let _ = || {
+        match event {
+            NUMBER_POINTER => (),
+            _ => (),
+        };
+    };
+}
+
+pub fn edge_case_char(event: char) {
+    let _ = || {
+        match event {
+            'a' => (),
+            _ => (),
+        };
+    };
+}
+
+fn main() {}