Skip to content

Commit 0e9f6ec

Browse files
committed
Auto merge of #30579 - dotdash:30478, r=arielb1
`auto_ref()` currently returns an Rvalue datum for the ref'd value, which is fine for thin pointers, but for fat pointers this means that once the pointer is moved out of that datum, its memory will be marked as dead. And because there is not necessarily an intermediate temporary involved we can end up marking memory as dead that is actually still used. As I don't want to break the micro-optimization for thin pointers by always returning an Lvalue datum, I decided to only do so for fat pointers. Fix #30478
2 parents b4707eb + 575f690 commit 0e9f6ec

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/librustc_trans/trans/expr.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2187,15 +2187,19 @@ fn auto_ref<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
21872187
let referent_ty = lv_datum.ty;
21882188
let ptr_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReStatic), referent_ty);
21892189

2190+
// Construct the resulting datum. The right datum to return here would be an Lvalue datum,
2191+
// because there is cleanup scheduled and the datum doesn't own the data, but for thin pointers
2192+
// we microoptimize it to be an Rvalue datum to avoid the extra alloca and level of
2193+
// indirection and for thin pointers, this has no ill effects.
2194+
let kind = if type_is_sized(bcx.tcx(), referent_ty) {
2195+
RvalueExpr(Rvalue::new(ByValue))
2196+
} else {
2197+
LvalueExpr(lv_datum.kind)
2198+
};
2199+
21902200
// Get the pointer.
21912201
let llref = lv_datum.to_llref();
2192-
2193-
// Construct the resulting datum, using what was the "by ref"
2194-
// ValueRef of type `referent_ty` to be the "by value" ValueRef
2195-
// of type `&referent_ty`.
2196-
// Pointers to DST types are non-immediate, and therefore still use ByRef.
2197-
let kind = if type_is_sized(bcx.tcx(), referent_ty) { ByValue } else { ByRef };
2198-
DatumBlock::new(bcx, Datum::new(llref, ptr_ty, RvalueExpr(Rvalue::new(kind))))
2202+
DatumBlock::new(bcx, Datum::new(llref, ptr_ty, kind))
21992203
}
22002204

22012205
fn deref_multiple<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,

0 commit comments

Comments
 (0)