Skip to content

Commit de7d4a8

Browse files
Don't reset cast kind without also updating the operand in simplify_cast
1 parent c215e80 commit de7d4a8

4 files changed

+15
-13
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1367,16 +1367,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13671367

13681368
fn simplify_cast(
13691369
&mut self,
1370-
kind: &mut CastKind,
1371-
operand: &mut Operand<'tcx>,
1370+
initial_kind: &mut CastKind,
1371+
initial_operand: &mut Operand<'tcx>,
13721372
to: Ty<'tcx>,
13731373
location: Location,
13741374
) -> Option<VnIndex> {
13751375
use CastKind::*;
13761376
use rustc_middle::ty::adjustment::PointerCoercion::*;
13771377

1378-
let mut from = operand.ty(self.local_decls, self.tcx);
1379-
let mut value = self.simplify_operand(operand, location)?;
1378+
let mut from = initial_operand.ty(self.local_decls, self.tcx);
1379+
let mut kind = *initial_kind;
1380+
let mut value = self.simplify_operand(initial_operand, location)?;
13801381
if from == to {
13811382
return Some(value);
13821383
}
@@ -1400,7 +1401,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14001401
&& to.is_unsafe_ptr()
14011402
&& self.pointers_have_same_metadata(from, to)
14021403
{
1403-
*kind = PtrToPtr;
1404+
kind = PtrToPtr;
14041405
was_updated_this_iteration = true;
14051406
}
14061407

@@ -1443,7 +1444,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14431444
to: inner_to,
14441445
} = *self.get(value)
14451446
{
1446-
let new_kind = match (inner_kind, *kind) {
1447+
let new_kind = match (inner_kind, kind) {
14471448
// Even if there's a narrowing cast in here that's fine, because
14481449
// things like `*mut [i32] -> *mut i32 -> *const i32` and
14491450
// `*mut [i32] -> *const [i32] -> *const i32` can skip the middle in MIR.
@@ -1471,7 +1472,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14711472
_ => None,
14721473
};
14731474
if let Some(new_kind) = new_kind {
1474-
*kind = new_kind;
1475+
kind = new_kind;
14751476
from = inner_from;
14761477
value = inner_value;
14771478
was_updated_this_iteration = true;
@@ -1489,10 +1490,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14891490
}
14901491

14911492
if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
1492-
*operand = op;
1493+
*initial_operand = op;
1494+
*initial_kind = kind;
14931495
}
14941496

1495-
Some(self.insert(Value::Cast { kind: *kind, value, from, to }))
1497+
Some(self.insert(Value::Cast { kind, value, from, to }))
14961498
}
14971499

14981500
fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> {

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// EMIT_MIR dont_reset_cast_kind_without_updating_operand.test.GVN.diff
55

66
fn test() {
7-
let vp_ctx: &Box<()>= &Box::new(());
7+
let vp_ctx: &Box<()> = &Box::new(());
88
let slf: *const () = &raw const **vp_ctx;
99
let bytes = std::ptr::slice_from_raw_parts(slf, 1);
1010
let _x = foo(bytes);
1111
}
1212

1313
fn foo(bytes: *const [()]) -> *mut () {
1414
bytes as *mut ()
15-
}
15+
}

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
StorageLive(_8);
142142
_8 = copy _5;
143143
- _7 = copy _8 as *mut () (PtrToPtr);
144-
+ _7 = copy _5 as *mut () (Transmute);
144+
+ _7 = copy _5 as *mut () (PtrToPtr);
145145
StorageDead(_8);
146146
StorageDead(_7);
147147
- StorageDead(_5);

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
StorageLive(_8);
6767
_8 = copy _5;
6868
- _7 = copy _8 as *mut () (PtrToPtr);
69-
+ _7 = copy _5 as *mut () (Transmute);
69+
+ _7 = copy _5 as *mut () (PtrToPtr);
7070
StorageDead(_8);
7171
StorageDead(_7);
7272
- StorageDead(_5);

0 commit comments

Comments
 (0)