Skip to content

Commit 0151061

Browse files
committed
NRVO: Allow occurrences of the return place in var debug info
The non-use occurrence of the return place in var debug info does not currently inhibit NRVO optimization, but it will fail assertion in `visit_place` when optimization is performed. Relax assertion check to allow the return place in var debug info. This case might be impossible to hit in optimization pipelines as of now, but can be encountered in customized mir-opt-level=2 pipeline with copy propagation disabled. For example in: ``` pub fn b(s: String) -> String { a(s) } #[inline] pub fn a(s: String) -> String { let x = s; let y = x; y } ```
1 parent 0d0f6b1 commit 0151061

File tree

1 file changed

+5
-4
lines changed
  • compiler/rustc_mir/src/transform

1 file changed

+5
-4
lines changed

compiler/rustc_mir/src/transform/nrvo.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir::Mutability;
22
use rustc_index::bit_set::HybridBitSet;
3-
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
3+
use rustc_middle::mir::visit::{MutVisitor, NonUseContext, PlaceContext, Visitor};
44
use rustc_middle::mir::{self, BasicBlock, Local, Location};
55
use rustc_middle::ty::TyCtxt;
66

@@ -196,9 +196,10 @@ impl MutVisitor<'tcx> for RenameToReturnPlace<'tcx> {
196196
self.super_terminator(terminator, loc);
197197
}
198198

199-
fn visit_local(&mut self, l: &mut Local, _: PlaceContext, _: Location) {
200-
assert_ne!(*l, mir::RETURN_PLACE);
201-
if *l == self.to_rename {
199+
fn visit_local(&mut self, l: &mut Local, ctxt: PlaceContext, _: Location) {
200+
if *l == mir::RETURN_PLACE {
201+
assert_eq!(ctxt, PlaceContext::NonUse(NonUseContext::VarDebugInfo));
202+
} else if *l == self.to_rename {
202203
*l = mir::RETURN_PLACE;
203204
}
204205
}

0 commit comments

Comments
 (0)