Skip to content

Commit ebd60b9

Browse files
committed
Do not flatten derefs with ProjectionElem::Index.
1 parent 8a87857 commit ebd60b9

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
756756
&& let Some(v) = self.simplify_place_value(&mut pointee, location)
757757
{
758758
value = v;
759-
place_ref = pointee.project_deeper(&place.projection[index..], self.tcx).as_ref();
759+
// `pointee` holds a `Place`, so `ProjectionElem::Index` holds a `Local`.
760+
// That local is SSA, but we otherwise have no guarantee on that local's value at
761+
// the current location compared to its value where `pointee` was borrowed.
762+
if pointee.projection.iter().all(|elem| !matches!(elem, ProjectionElem::Index(_))) {
763+
place_ref =
764+
pointee.project_deeper(&place.projection[index..], self.tcx).as_ref();
765+
}
760766
}
761767
if let Some(local) = self.try_as_local(value, location) {
762768
// Both `local` and `Place { local: place.local, projection: projection[..index] }`
@@ -774,7 +780,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
774780
&& let Some(v) = self.simplify_place_value(&mut pointee, location)
775781
{
776782
value = v;
777-
place_ref = pointee.project_deeper(&[], self.tcx).as_ref();
783+
// `pointee` holds a `Place`, so `ProjectionElem::Index` holds a `Local`.
784+
// That local is SSA, but we otherwise have no guarantee on that local's value at
785+
// the current location compared to its value where `pointee` was borrowed.
786+
if pointee.projection.iter().all(|elem| !matches!(elem, ProjectionElem::Index(_))) {
787+
place_ref = pointee.project_deeper(&[], self.tcx).as_ref();
788+
}
778789
}
779790
if let Some(new_local) = self.try_as_local(value, location) {
780791
place_ref = PlaceRef { local: new_local, projection: &[] };

tests/mir-opt/gvn.dereference_indexing.GVN.panic-abort.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
+ nop;
4444
StorageLive(_8);
4545
StorageLive(_9);
46-
- _9 = copy (*_3);
47-
+ _9 = copy _1[_4];
46+
_9 = copy (*_3);
4847
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind unreachable];
4948
}
5049

tests/mir-opt/gvn.dereference_indexing.GVN.panic-unwind.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
+ nop;
4444
StorageLive(_8);
4545
StorageLive(_9);
46-
- _9 = copy (*_3);
47-
+ _9 = copy _1[_4];
46+
_9 = copy (*_3);
4847
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind continue];
4948
}
5049

0 commit comments

Comments
 (0)