Skip to content

Commit 988cd5d

Browse files
authored
Rollup merge of rust-lang#63306 - RalfJung:retag, r=varkor
Adapt AddRetag for shallow retagging With rust-lang/miri#872, Miri only retags "bare" references, not those nested in compound types. This adjust `Retag` statement generation to don't emit retags if they are definitely not a bare reference. I also expanded the mir-opt test to cover the `Retag` in the drop shim, which had previously not been tested.
2 parents 7e96825 + 2122fe4 commit 988cd5d

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/librustc_mir/transform/add_retag.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ fn is_stable(
4242
}
4343
}
4444

45-
/// Determine whether this type may have a reference in it, recursing below compound types but
46-
/// not below references.
47-
fn may_have_reference<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
45+
/// Determine whether this type may be a reference (or box), and thus needs retagging.
46+
fn may_be_reference<'tcx>(ty: Ty<'tcx>) -> bool {
4847
match ty.sty {
4948
// Primitive types that are not references
5049
ty::Bool | ty::Char |
@@ -55,15 +54,12 @@ fn may_have_reference<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
5554
// References
5655
ty::Ref(..) => true,
5756
ty::Adt(..) if ty.is_box() => true,
58-
// Compound types
59-
ty::Array(ty, ..) | ty::Slice(ty) =>
60-
may_have_reference(ty, tcx),
61-
ty::Tuple(tys) =>
62-
tys.iter().any(|ty| may_have_reference(ty.expect_ty(), tcx)),
63-
ty::Adt(adt, substs) =>
64-
adt.variants.iter().any(|v| v.fields.iter().any(|f|
65-
may_have_reference(f.ty(tcx, substs), tcx)
66-
)),
57+
// Compound types are not references
58+
ty::Array(..) |
59+
ty::Slice(..) |
60+
ty::Tuple(..) |
61+
ty::Adt(..) =>
62+
false,
6763
// Conservative fallback
6864
_ => true,
6965
}
@@ -80,7 +76,7 @@ impl MirPass for AddRetag {
8076
// FIXME: Instead of giving up for unstable places, we should introduce
8177
// a temporary and retag on that.
8278
is_stable(place.as_ref())
83-
&& may_have_reference(place.ty(&*local_decls, tcx).ty, tcx)
79+
&& may_be_reference(place.ty(&*local_decls, tcx).ty)
8480
};
8581

8682
// PART 1

src/test/mir-opt/retag.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore-wasm32-bare compiled with panic=abort by default
12
// ignore-tidy-linelength
23
// compile-flags: -Z mir-emit-retag -Z mir-opt-level=0 -Z span_free_formats
34

@@ -11,6 +12,10 @@ impl Test {
1112
fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 { x }
1213
}
1314

15+
impl Drop for Test {
16+
fn drop(&mut self) {}
17+
}
18+
1419
fn main() {
1520
let mut x = 0;
1621
{
@@ -60,10 +65,12 @@ fn main() {
6065
// ...
6166
// bb0: {
6267
// ...
63-
// _3 = const Test::foo(move _4, move _6) -> bb1;
68+
// _3 = const Test::foo(move _4, move _6) -> [return: bb2, unwind: bb3];
6469
// }
6570
//
66-
// bb1: {
71+
// ...
72+
//
73+
// bb2: {
6774
// Retag(_3);
6875
// ...
6976
// _9 = move _3;
@@ -80,25 +87,20 @@ fn main() {
8087
// _12 = move _13 as *mut i32 (Misc);
8188
// Retag([raw] _12);
8289
// ...
83-
// _16 = move _17(move _18) -> bb2;
90+
// _16 = move _17(move _18) -> bb5;
8491
// }
8592
//
86-
// bb2: {
93+
// bb5: {
8794
// Retag(_16);
8895
// ...
89-
// _20 = const Test::foo_shr(move _21, move _23) -> bb3;
90-
// }
91-
//
92-
// bb3: {
93-
// ...
94-
// return;
96+
// _20 = const Test::foo_shr(move _21, move _23) -> [return: bb6, unwind: bb7];
9597
// }
9698
//
9799
// ...
98100
// }
99101
// END rustc.main.EraseRegions.after.mir
100102
// START rustc.main-{{closure}}.EraseRegions.after.mir
101-
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(20), local_id: 72 }], _2: &i32) -> &i32 {
103+
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(22), local_id: 72 }], _2: &i32) -> &i32 {
102104
// ...
103105
// bb0: {
104106
// Retag([fn entry] _1);
@@ -113,3 +115,17 @@ fn main() {
113115
// }
114116
// }
115117
// END rustc.main-{{closure}}.EraseRegions.after.mir
118+
// START rustc.ptr-real_drop_in_place.Test.SimplifyCfg-make_shim.after.mir
119+
// fn std::ptr::real_drop_in_place(_1: &mut Test) -> () {
120+
// ...
121+
// bb0: {
122+
// Retag([raw] _1);
123+
// _2 = &mut (*_1);
124+
// _3 = const <Test as std::ops::Drop>::drop(move _2) -> bb1;
125+
// }
126+
//
127+
// bb1: {
128+
// return;
129+
// }
130+
// }
131+
// END rustc.ptr-real_drop_in_place.Test.SimplifyCfg-make_shim.after.mir

0 commit comments

Comments
 (0)