Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
}
CastKind::Transmute | CastKind::Subtype => {
let value = self.eval_to_const(value)?;
if value.is_immediate_uninit() {
return Some(ImmTy::uninit(ty).into());
}
// `offset` for immediates generally only supports projections that match the
// type of the immediate. However, as a HACK, we exploit that it can also do
// limited transmutes: it only works between types with the same layout, and
Expand Down
7 changes: 7 additions & 0 deletions tests/mir-opt/const_prop/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ pub fn u8_array() -> [MaybeUninit<u8>; 8] {
// CHECK: _0 = const <uninit>;
[MaybeUninit::uninit(); 8]
}

// EMIT_MIR maybe_uninit.u8_array_assume_init.GVN.diff
pub fn u8_array_assume_init() -> [MaybeUninit<u8>; 8] {
// CHECK: fn u8_array_assume_init(
// CHECK: _0 = const <uninit>;
unsafe { MaybeUninit::uninit().assume_init() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- // MIR for `u8_array_assume_init` before GVN
+ // MIR for `u8_array_assume_init` after GVN

fn u8_array_assume_init() -> [MaybeUninit<u8>; 8] {
let mut _0: [std::mem::MaybeUninit<u8>; 8];
let mut _1: std::mem::MaybeUninit<[std::mem::MaybeUninit<u8>; 8]>;
scope 1 (inlined MaybeUninit::<[MaybeUninit<u8>; 8]>::uninit) {
}
scope 2 (inlined #[track_caller] MaybeUninit::<[MaybeUninit<u8>; 8]>::assume_init) {
scope 3 (inlined transmute_neo::<MaybeUninit<[MaybeUninit<u8>; 8]>, [MaybeUninit<u8>; 8]>) {
}
}

bb0: {
StorageLive(_1);
- _1 = MaybeUninit::<[MaybeUninit<u8>; 8]> { uninit: const () };
+ _1 = const <uninit>;
goto -> bb1;
}

bb1: {
- _0 = copy _1 as [std::mem::MaybeUninit<u8>; 8] (Transmute);
+ _0 = const <uninit>;
StorageDead(_1);
return;
}
+ }
+
+ ALLOC0 (size: 8, align: 1) {
+ __ __ __ __ __ __ __ __ │ ░░░░░░░░
+ }
+
+ ALLOC1 (size: 8, align: 1) {
+ __ __ __ __ __ __ __ __ │ ░░░░░░░░
}

Loading