Skip to content

Commit 372afcf

Browse files
committed
Auto merge of #83445 - erikdesjardins:rmunion, r=RalfJung
RemoveZsts: don't touch unions This should fix a Miri ICE r? `@RalfJung`
2 parents dbc37a9 + d5c1ad5 commit 372afcf

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

compiler/rustc_mir/src/transform/remove_zsts.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Removes assignments to ZST places.
22
33
use crate::transform::MirPass;
4-
use rustc_middle::mir::{Body, StatementKind};
4+
use rustc_middle::mir::tcx::PlaceTy;
5+
use rustc_middle::mir::{Body, LocalDecls, Place, StatementKind};
56
use rustc_middle::ty::{self, Ty, TyCtxt};
67

78
pub struct RemoveZsts;
@@ -28,6 +29,9 @@ impl<'tcx> MirPass<'tcx> for RemoveZsts {
2829
if !layout.is_zst() {
2930
continue;
3031
}
32+
if involves_a_union(place, local_decls, tcx) {
33+
continue;
34+
}
3135
if tcx.consider_optimizing(|| {
3236
format!(
3337
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
@@ -55,3 +59,31 @@ fn maybe_zst(ty: Ty<'_>) -> bool {
5559
_ => false,
5660
}
5761
}
62+
63+
/// Miri lazily allocates memory for locals on assignment,
64+
/// so we must preserve writes to unions and union fields,
65+
/// or it will ICE on reads of those fields.
66+
fn involves_a_union<'tcx>(
67+
place: Place<'tcx>,
68+
local_decls: &LocalDecls<'tcx>,
69+
tcx: TyCtxt<'tcx>,
70+
) -> bool {
71+
let mut place_ty = PlaceTy::from_ty(local_decls[place.local].ty);
72+
if is_union(place_ty.ty) {
73+
return true;
74+
}
75+
for elem in place.projection {
76+
place_ty = place_ty.projection_ty(tcx, elem);
77+
if is_union(place_ty.ty) {
78+
return true;
79+
}
80+
}
81+
return false;
82+
}
83+
84+
fn is_union(ty: Ty<'_>) -> bool {
85+
match ty.kind() {
86+
ty::Adt(def, _) if def.is_union() => true,
87+
_ => false,
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// MIR for `get_union` after RemoveZsts
2+
3+
fn get_union() -> Foo {
4+
let mut _0: Foo; // return place in scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:12:19: 12:22
5+
let mut _1: (); // in scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:13:14: 13:16
6+
7+
bb0: {
8+
StorageLive(_1); // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:13:14: 13:16
9+
(_0.0: ()) = move _1; // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:13:5: 13:18
10+
StorageDead(_1); // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:13:17: 13:18
11+
return; // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:14:2: 14:2
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -Zmir-opt-level=3
2+
3+
// Ensure RemoveZsts doesn't remove ZST assignments to union fields,
4+
// which causes problems in Miri.
5+
6+
union Foo {
7+
x: (),
8+
y: u64,
9+
}
10+
11+
// EMIT_MIR remove_zsts_dont_touch_unions.get_union.RemoveZsts.after.mir
12+
fn get_union() -> Foo {
13+
Foo { x: () }
14+
}
15+
16+
17+
fn main() {
18+
get_union();
19+
}

0 commit comments

Comments
 (0)