Skip to content

Commit

Permalink
Rollup merge of #72048 - jonas-schievink:visit-return, r=oli-obk
Browse files Browse the repository at this point in the history
Visit move out of `_0` when visiting `return`

Closes #72032
  • Loading branch information
Dylan-DPC authored May 12, 2020
2 parents 5fe77e5 + e22cc99 commit 0f95630
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/librustc_middle/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,29 @@ macro_rules! make_mir_visitor {
TerminatorKind::Goto { .. } |
TerminatorKind::Resume |
TerminatorKind::Abort |
TerminatorKind::Return |
TerminatorKind::GeneratorDrop |
TerminatorKind::Unreachable |
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } => {
}

TerminatorKind::Return => {
// `return` logically moves from the return place `_0`. Note that the place
// cannot be changed by any visitor, though.
let $($mutability)? local = RETURN_PLACE;
self.visit_local(
& $($mutability)? local,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
source_location,
);

assert_eq!(
local,
RETURN_PLACE,
"`MutVisitor` tried to mutate return place of `return` terminator"
);
}

TerminatorKind::SwitchInt {
discr,
switch_ty,
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_mir/transform/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ impl<'tcx> MirPass<'tcx> for CopyPropagation {
}
// Conservatively gives up if the dest is an argument,
// because there may be uses of the original argument value.
if body.local_kind(dest_local) == LocalKind::Arg {
// Also gives up on the return place, as we cannot propagate into its implicit
// use by `return`.
if matches!(
body.local_kind(dest_local),
LocalKind::Arg | LocalKind::ReturnPointer
) {
debug!(" Can't copy-propagate local: dest {:?} (argument)", dest_local);
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
*local = self.to;
}
}

fn visit_terminator_kind(&mut self, kind: &mut TerminatorKind<'tcx>, location: Location) {
match kind {
TerminatorKind::Return => {
// Do not replace the implicit `_0` access here, as that's not possible. The
// transform already handles `return` correctly.
}
_ => self.super_terminator_kind(kind, location),
}
}
}

struct DerefArgVisitor<'tcx> {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
}

fn visit_terminator_kind(&mut self, kind: &mut TerminatorKind<'tcx>, loc: Location) {
self.super_terminator_kind(kind, loc);
// Don't try to modify the implicit `_0` access on return (`return` terminators are
// replaced down below anyways).
if !matches!(kind, TerminatorKind::Return) {
self.super_terminator_kind(kind, loc);
}

match *kind {
TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
Expand Down

0 comments on commit 0f95630

Please sign in to comment.