Skip to content

Commit 35b5157

Browse files
committed
Auto merge of #68170 - wesleywiser:stop_const_prop_ref_taking, r=oli-obk
Turn off const propagation of ref taking Fixes #67529 Fixes #67640 Fixes #67641 Fixes #67862 r? @oli-obk
2 parents 1b117d7 + 7f65475 commit 35b5157

File tree

6 files changed

+82
-22
lines changed

6 files changed

+82
-22
lines changed

src/librustc_mir/transform/const_prop.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -636,28 +636,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
636636
self.check_binary_op(*op, left, right, source_info, place_layout, overflow_check)?;
637637
}
638638

639-
// Work around: avoid ICE in miri. FIXME(wesleywiser)
640-
// The Miri engine ICEs when taking a reference to an uninitialized unsized
641-
// local. There's nothing it can do here: taking a reference needs an allocation
642-
// which needs to know the size. Normally that's okay as during execution
643-
// (e.g. for CTFE) it can never happen. But here in const_prop
644-
// unknown data is uninitialized, so if e.g. a function argument is unsized
645-
// and has a reference taken, we get an ICE.
639+
// Do not try creating references (#67862)
646640
Rvalue::Ref(_, _, place_ref) => {
647-
trace!("checking Ref({:?})", place_ref);
641+
trace!("skipping Ref({:?})", place_ref);
648642

649-
if let Some(local) = place_ref.as_local() {
650-
let alive = if let LocalValue::Live(_) = self.ecx.frame().locals[local].value {
651-
true
652-
} else {
653-
false
654-
};
655-
656-
if !alive {
657-
trace!("skipping Ref({:?}) to uninitialized local", place);
658-
return None;
659-
}
660-
}
643+
return None;
661644
}
662645

663646
_ => {}

src/test/mir-opt/const_prop/ref_deref_project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
*(&(4, 5).1);
2+
*(&(4, 5).1); // This does not currently propagate (#67862)
33
}
44

55
// END RUST SOURCE
@@ -35,7 +35,7 @@ fn main() {
3535
// ...
3636
// _4 = const main::promoted[0];
3737
// _2 = &((*_4).1: i32);
38-
// _1 = const 5i32;
38+
// _1 = (*_2);
3939
// ...
4040
// }
4141
// END rustc.main.ConstProp.after.mir

src/test/ui/consts/issue-67529.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: -Z mir-opt-level=2
2+
// run-pass
3+
4+
struct Baz<T: ?Sized> {
5+
a: T
6+
}
7+
8+
fn main() {
9+
let d : Baz<[i32; 4]> = Baz { a: [1,2,3,4] };
10+
assert_eq!([1, 2, 3, 4], d.a);
11+
}

src/test/ui/consts/issue-67640.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Z mir-opt-level=3
2+
// run-pass
3+
4+
struct X {
5+
x: isize
6+
}
7+
8+
fn f1(a: &mut X, b: &mut isize, c: isize) -> isize {
9+
let r = a.x + *b + c;
10+
a.x = 0;
11+
*b = 10;
12+
return r;
13+
}
14+
15+
fn f2<F>(a: isize, f: F) -> isize where F: FnOnce(isize) { f(1); return a; }
16+
17+
pub fn main() {
18+
let mut a = X {x: 1};
19+
let mut b = 2;
20+
let c = 3;
21+
assert_eq!(f1(&mut a, &mut b, c), 6);
22+
assert_eq!(a.x, 0);
23+
assert_eq!(f2(a.x, |_| a.x = 50), 0);
24+
}

src/test/ui/consts/issue-67641.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Z mir-opt-level=2
2+
// run-pass
3+
4+
use std::cell::Cell;
5+
6+
#[derive(Debug)]
7+
struct B<'a> {
8+
a: [Cell<Option<&'a B<'a>>>; 2]
9+
}
10+
11+
impl<'a> B<'a> {
12+
fn new() -> B<'a> {
13+
B { a: [Cell::new(None), Cell::new(None)] }
14+
}
15+
}
16+
17+
fn f() {
18+
let b2 = B::new();
19+
b2.a[0].set(Some(&b2));
20+
}
21+
22+
fn main() {
23+
f();
24+
}

src/test/ui/consts/issue-67862.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Z mir-opt-level=2
2+
// run-pass
3+
4+
fn e220() -> (i64, i64) {
5+
#[inline(never)]
6+
fn get_displacement() -> [i64; 2] {
7+
[139776, 963904]
8+
}
9+
10+
let res = get_displacement();
11+
match (&res[0], &res[1]) {
12+
(arg0, arg1) => (*arg0, *arg1),
13+
}
14+
}
15+
16+
fn main() {
17+
assert_eq!(e220(), (139776, 963904));
18+
}

0 commit comments

Comments
 (0)