about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-11-11 11:12:09 +0100
committerRalf Jung <post@ralfj.de>2023-11-12 09:43:08 +0100
commitf6a49ba416aee1f57426c80a96f79287f6dec9d3 (patch)
treeb5f6d62c45b919e0adc67937a937e68a427523a0
parent1db4b12494f698754b925f55061eb9e6b3241423 (diff)
downloadrust-f6a49ba416aee1f57426c80a96f79287f6dec9d3.tar.gz
rust-f6a49ba416aee1f57426c80a96f79287f6dec9d3.zip
patterns: don't ice when encountering a raw str slice
-rw-r--r--compiler/rustc_const_eval/src/const_eval/valtrees.rs13
-rw-r--r--tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs8
-rw-r--r--tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr13
3 files changed, 28 insertions, 6 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
index ed2d81727f7..2bee4b45ad0 100644
--- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs
+++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
@@ -11,7 +11,7 @@ use rustc_middle::mir;
 use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
 use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
 use rustc_span::DUMMY_SP;
-use rustc_target::abi::VariantIdx;
+use rustc_target::abi::{Abi, VariantIdx};
 
 #[instrument(skip(ecx), level = "debug")]
 fn branches<'tcx>(
@@ -101,11 +101,16 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
             // Not all raw pointers are allowed, as we cannot properly test them for
             // equality at compile-time (see `ptr_guaranteed_cmp`).
             // However we allow those that are just integers in disguise.
-            // (We could allow wide raw pointers where both sides are integers in the future,
-            // but for now we reject them.)
-            let Ok(val) = ecx.read_scalar(place) else {
+            // First, get the pointer. Remember it might be wide!
+            let Ok(val) = ecx.read_immediate(place) else {
                 return Err(ValTreeCreationError::Other);
             };
+            // We could allow wide raw pointers where both sides are integers in the future,
+            // but for now we reject them.
+            if matches!(val.layout.abi, Abi::ScalarPair(..)) {
+                return Err(ValTreeCreationError::Other);
+            }
+            let val = val.to_scalar();
             // We are in the CTFE machine, so ptr-to-int casts will fail.
             // This can only be `Ok` if `val` already is an integer.
             let Ok(val) = val.try_to_int() else {
diff --git a/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs b/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs
index 2491071d1e1..6285427f59c 100644
--- a/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs
+++ b/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs
@@ -23,10 +23,18 @@ fn foo2(x: *const u8) {
 
 const D: *const [u8; 4] = b"abcd";
 
+const STR: *const str = "abcd";
+
 fn main() {
     match D {
         D => {} //~ERROR: behave unpredictably
         //~| previously accepted
         _ => {}
     }
+
+    match STR {
+        STR => {} //~ERROR: behave unpredictably
+        //~| previously accepted
+        _ => {}
+    }
 }
diff --git a/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr b/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr
index ab53346b5ee..1546f23908c 100644
--- a/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr
+++ b/tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr
@@ -22,7 +22,7 @@ LL |         C_INNER => {}
    = note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
 
 error: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
-  --> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:28:9
+  --> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:30:9
    |
 LL |         D => {}
    |         ^
@@ -30,5 +30,14 @@ LL |         D => {}
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
 
-error: aborting due to 3 previous errors
+error: function pointers and raw pointers not derived from integers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
+  --> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:36:9
+   |
+LL |         STR => {}
+   |         ^^^
+   |
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
+
+error: aborting due to 4 previous errors