Skip to content

Commit e121305

Browse files
authored
Rollup merge of rust-lang#55792 - oli-obk:propsicle, r=RalfJung
Prevent ICE in const-prop array oob check fixes rust-lang#55772 fixes rust-lang#54541
2 parents d2aeef0 + 1206549 commit e121305

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

src/librustc_mir/transform/const_prop.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
591591
if let TerminatorKind::Assert { expected, msg, cond, .. } = kind {
592592
if let Some(value) = self.eval_operand(cond, source_info) {
593593
trace!("assertion on {:?} should be {:?}", value, expected);
594-
let expected = Immediate::Scalar(Scalar::from_bool(*expected).into());
595-
if expected != value.0.to_immediate() {
594+
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
595+
if expected != self.ecx.read_scalar(value.0).unwrap() {
596596
// poison all places this operand references so that further code
597597
// doesn't use the invalid value
598598
match cond {
@@ -628,20 +628,20 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
628628
let len = self
629629
.eval_operand(len, source_info)
630630
.expect("len must be const");
631-
let len = match len.0.to_immediate() {
632-
Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits {
631+
let len = match self.ecx.read_scalar(len.0) {
632+
Ok(ScalarMaybeUndef::Scalar(Scalar::Bits {
633633
bits, ..
634634
})) => bits,
635-
_ => bug!("const len not primitive: {:?}", len),
635+
other => bug!("const len not primitive: {:?}", other),
636636
};
637637
let index = self
638638
.eval_operand(index, source_info)
639639
.expect("index must be const");
640-
let index = match index.0.to_immediate() {
641-
Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits {
640+
let index = match self.ecx.read_scalar(index.0) {
641+
Ok(ScalarMaybeUndef::Scalar(Scalar::Bits {
642642
bits, ..
643643
})) => bits,
644-
_ => bug!("const index not primitive: {:?}", index),
644+
other => bug!("const index not primitive: {:?}", other),
645645
};
646646
format!(
647647
"index out of bounds: \

src/test/ui/consts/const-prop-ice.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
[0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
3+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: index out of bounds: the len is 3 but the index is 3
2+
--> $DIR/const-prop-ice.rs:2:5
3+
|
4+
LL | [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[deny(const_err)] on by default
8+
9+
error: aborting due to previous error
10+

src/test/ui/consts/const-prop-ice2.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
enum Enum { One=1 }
3+
let xs=[0;1 as usize];
4+
println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1
5+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: index out of bounds: the len is 1 but the index is 1
2+
--> $DIR/const-prop-ice2.rs:4:20
3+
|
4+
LL | println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[deny(const_err)] on by default
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)