Skip to content

Commit f049636

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 67de6ce + 3df672f commit f049636

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-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

+26-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ impl Test {
1111
fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 { x }
1212
}
1313

14+
impl Drop for Test {
15+
fn drop(&mut self) {}
16+
}
17+
1418
fn main() {
1519
let mut x = 0;
1620
{
@@ -60,10 +64,12 @@ fn main() {
6064
// ...
6165
// bb0: {
6266
// ...
63-
// _3 = const Test::foo(move _4, move _6) -> bb1;
67+
// _3 = const Test::foo(move _4, move _6) -> [return: bb2, unwind: bb3];
6468
// }
6569
//
66-
// bb1: {
70+
// ...
71+
//
72+
// bb2: {
6773
// Retag(_3);
6874
// ...
6975
// _9 = move _3;
@@ -80,25 +86,20 @@ fn main() {
8086
// _12 = move _13 as *mut i32 (Misc);
8187
// Retag([raw] _12);
8288
// ...
83-
// _16 = move _17(move _18) -> bb2;
89+
// _16 = move _17(move _18) -> bb5;
8490
// }
8591
//
86-
// bb2: {
92+
// bb5: {
8793
// Retag(_16);
8894
// ...
89-
// _20 = const Test::foo_shr(move _21, move _23) -> bb3;
90-
// }
91-
//
92-
// bb3: {
93-
// ...
94-
// return;
95+
// _20 = const Test::foo_shr(move _21, move _23) -> [return: bb6, unwind: bb7];
9596
// }
9697
//
9798
// ...
9899
// }
99100
// END rustc.main.EraseRegions.after.mir
100101
// START rustc.main-{{closure}}.EraseRegions.after.mir
101-
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(20), local_id: 72 }], _2: &i32) -> &i32 {
102+
// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(22), local_id: 72 }], _2: &i32) -> &i32 {
102103
// ...
103104
// bb0: {
104105
// Retag([fn entry] _1);
@@ -113,3 +114,17 @@ fn main() {
113114
// }
114115
// }
115116
// END rustc.main-{{closure}}.EraseRegions.after.mir
117+
// START rustc.ptr-real_drop_in_place.Test.SimplifyCfg-make_shim.after.mir
118+
// fn std::ptr::real_drop_in_place(_1: &mut Test) -> () {
119+
// ...
120+
// bb0: {
121+
// Retag([raw] _1);
122+
// _2 = &mut (*_1);
123+
// _3 = const <Test as std::ops::Drop>::drop(move _2) -> bb1;
124+
// }
125+
//
126+
// bb1: {
127+
// return;
128+
// }
129+
// }
130+
// END rustc.ptr-real_drop_in_place.Test.SimplifyCfg-make_shim.after.mir

0 commit comments

Comments
 (0)