Skip to content

Commit

Permalink
Rollup merge of #89321 - tmiasko:rebase-resume-arg, r=estebank
Browse files Browse the repository at this point in the history
Rebase resume argument projections during state transform

When remapping a resume argument with projections rebase them on top of
the new base.

The case where resume argument has projections is unusual, but might
arise with box syntax where the assignment is performed directly into
the box without an intermediate temporary.

Fixes #85635.
  • Loading branch information
Manishearth authored Oct 1, 2021
2 parents e77d163 + 8901ea2 commit 2726955
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,16 @@ impl MutVisitor<'tcx> for TransformVisitor<'tcx> {
let source_info = data.terminator().source_info;
// We must assign the value first in case it gets declared dead below
data.statements.extend(self.make_state(state_idx, v, source_info));
let state = if let Some((resume, resume_arg)) = resume {
let state = if let Some((resume, mut resume_arg)) = resume {
// Yield
let state = 3 + self.suspension_points.len();

// The resume arg target location might itself be remapped if its base local is
// live across a yield.
let resume_arg =
if let Some(&(ty, variant, idx)) = self.remap.get(&resume_arg.local) {
self.make_field(variant, idx, ty)
replace_base(&mut resume_arg, self.make_field(variant, idx, ty), self.tcx);
resume_arg
} else {
resume_arg
};
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/generator/yield-in-box.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// run-pass

// Test that box-statements with yields in them work.

#![feature(generators, box_syntax)]
#![feature(generators, box_syntax, generator_trait)]
use std::pin::Pin;
use std::ops::Generator;
use std::ops::GeneratorState;

fn main() {
let x = 0i32;
Expand All @@ -15,4 +17,8 @@ fn main() {
_t => {}
}
};

let mut g = |_| box yield;
assert_eq!(Pin::new(&mut g).resume(1), GeneratorState::Yielded(()));
assert_eq!(Pin::new(&mut g).resume(2), GeneratorState::Complete(box 2));
}
2 changes: 1 addition & 1 deletion src/test/ui/generator/yield-in-box.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unused generator that must be used
--> $DIR/yield-in-box.rs:9:5
--> $DIR/yield-in-box.rs:11:5
|
LL | / || {
LL | | let y = 2u32;
Expand Down

0 comments on commit 2726955

Please sign in to comment.