Skip to content

Commit d0474fb

Browse files
committed
Auto merge of rust-lang#117807 - RalfJung:raw-str-slice, r=davidtwco
patterns: don't ice when encountering a raw str slice Fixes rust-lang#117806
2 parents 7d0e1bc + f6a49ba commit d0474fb

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::mir;
1111
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
1212
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
1313
use rustc_span::DUMMY_SP;
14-
use rustc_target::abi::VariantIdx;
14+
use rustc_target::abi::{Abi, VariantIdx};
1515

1616
#[instrument(skip(ecx), level = "debug")]
1717
fn branches<'tcx>(
@@ -101,11 +101,16 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
101101
// Not all raw pointers are allowed, as we cannot properly test them for
102102
// equality at compile-time (see `ptr_guaranteed_cmp`).
103103
// However we allow those that are just integers in disguise.
104-
// (We could allow wide raw pointers where both sides are integers in the future,
105-
// but for now we reject them.)
106-
let Ok(val) = ecx.read_scalar(place) else {
104+
// First, get the pointer. Remember it might be wide!
105+
let Ok(val) = ecx.read_immediate(place) else {
107106
return Err(ValTreeCreationError::Other);
108107
};
108+
// We could allow wide raw pointers where both sides are integers in the future,
109+
// but for now we reject them.
110+
if matches!(val.layout.abi, Abi::ScalarPair(..)) {
111+
return Err(ValTreeCreationError::Other);
112+
}
113+
let val = val.to_scalar();
109114
// We are in the CTFE machine, so ptr-to-int casts will fail.
110115
// This can only be `Ok` if `val` already is an integer.
111116
let Ok(val) = val.try_to_int() else {

tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ fn foo2(x: *const u8) {
2323

2424
const D: *const [u8; 4] = b"abcd";
2525

26+
const STR: *const str = "abcd";
27+
2628
fn main() {
2729
match D {
2830
D => {} //~ERROR: behave unpredictably
2931
//~| previously accepted
3032
_ => {}
3133
}
34+
35+
match STR {
36+
STR => {} //~ERROR: behave unpredictably
37+
//~| previously accepted
38+
_ => {}
39+
}
3240
}

tests/ui/consts/const_in_pattern/issue-34784-match-on-non-int-raw-ptr.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@ LL | C_INNER => {}
2222
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
2323

2424
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.
25-
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:28:9
25+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:30:9
2626
|
2727
LL | D => {}
2828
| ^
2929
|
3030
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3131
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
3232

33-
error: aborting due to 3 previous errors
33+
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.
34+
--> $DIR/issue-34784-match-on-non-int-raw-ptr.rs:36:9
35+
|
36+
LL | STR => {}
37+
| ^^^
38+
|
39+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
40+
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/70861>
41+
42+
error: aborting due to 4 previous errors
3443

0 commit comments

Comments
 (0)