1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This is the implementation of the pass which transforms generators into state machines.
13 //! MIR generation for generators creates a function which has a self argument which
14 //! passes by value. This argument is effectively a generator type which only contains upvars and
15 //! is only used for this argument inside the MIR for the generator.
16 //! It is passed by value to enable upvars to be moved out of it. Drop elaboration runs on that
17 //! MIR before this pass and creates drop flags for MIR locals.
18 //! It will also drop the generator argument (which only consists of upvars) if any of the upvars
19 //! are moved out of. This pass elaborates the drops of upvars / generator argument in the case
20 //! that none of the upvars were moved out of. This is because we cannot have any drops of this
21 //! generator in the MIR, since it is used to create the drop glue for the generator. We'd get
22 //! infinite recursion otherwise.
24 //! This pass creates the implementation for the Generator::resume function and the drop shim
25 //! for the generator based on the MIR input. It converts the generator argument from Self to
26 //! &mut Self adding derefs in the MIR as needed. It computes the final layout of the generator
27 //! struct which looks like this:
28 //! First upvars are stored
29 //! It is followed by the generator state field.
30 //! Then finally the MIR locals which are live across a suspension point are stored.
32 //! struct Generator {
38 //! This pass computes the meaning of the state field and the MIR locals which are live
39 //! across a suspension point. There are however two hardcoded generator states:
40 //! 0 - Generator have not been resumed yet
41 //! 1 - Generator has returned / is completed
42 //! 2 - Generator has been poisoned
44 //! It also rewrites `return x` and `yield y` as setting a new generator state and returning
45 //! GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
46 //! MIR locals which are live across a suspension point are moved to the generator struct
47 //! with references to them being updated with references to the generator struct.
49 //! The pass creates two functions which have a switch on the generator state giving
50 //! the action to take.
52 //! One of them is the implementation of Generator::resume.
53 //! For generators with state 0 (unresumed) it starts the execution of the generator.
54 //! For generators with state 1 (returned) and state 2 (poisoned) it panics.
55 //! Otherwise it continues the execution from the last suspension point.
57 //! The other function is the drop glue for the generator.
58 //! For generators with state 0 (unresumed) it drops the upvars of the generator.
59 //! For generators with state 1 (returned) and state 2 (poisoned) it does nothing.
60 //! Otherwise it drops all the values in scope at the last suspension point.
63 use rustc::hir::def_id::DefId;
64 use rustc::middle::const_val::ConstVal;
66 use rustc::mir::visit::{LvalueContext, Visitor, MutVisitor};
67 use rustc::ty::{self, TyCtxt, AdtDef, Ty, GeneratorInterior};
68 use rustc::ty::subst::{Kind, Substs};
70 use util::liveness::{self, LivenessMode};
71 use rustc_const_math::ConstInt;
72 use rustc_data_structures::indexed_vec::Idx;
73 use rustc_data_structures::indexed_set::IdxSetBuf;
74 use std::collections::HashMap;
78 use transform::{MirPass, MirSource};
79 use transform::simplify;
80 use transform::no_landing_pads::no_landing_pads;
81 use dataflow::{self, MaybeStorageLive, state_for_location};
83 pub struct StateTransform;
85 struct RenameLocalVisitor {
90 impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor {
91 fn visit_local(&mut self,
93 _: LvalueContext<'tcx>,
95 if *local == self.from {
101 struct DerefArgVisitor;
103 impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor {
104 fn visit_local(&mut self,
106 _: LvalueContext<'tcx>,
108 assert_ne!(*local, self_arg());
111 fn visit_lvalue(&mut self,
112 lvalue: &mut Lvalue<'tcx>,
113 context: LvalueContext<'tcx>,
114 location: Location) {
115 if *lvalue == Lvalue::Local(self_arg()) {
116 *lvalue = Lvalue::Projection(Box::new(Projection {
117 base: lvalue.clone(),
118 elem: ProjectionElem::Deref,
121 self.super_lvalue(lvalue, context, location);
126 fn self_arg() -> Local {
130 struct SuspensionPoint {
133 drop: Option<BasicBlock>,
134 storage_liveness: liveness::LocalSet,
137 struct TransformVisitor<'a, 'tcx: 'a> {
138 tcx: TyCtxt<'a, 'tcx, 'tcx>,
139 state_adt_ref: &'tcx AdtDef,
140 state_substs: &'tcx Substs<'tcx>,
142 // The index of the generator state in the generator struct
145 // Mapping from Local to (type of local, generator struct index)
146 remap: HashMap<Local, (Ty<'tcx>, usize)>,
148 // A map from a suspension point in a block to the locals which have live storage at that point
149 storage_liveness: HashMap<BasicBlock, liveness::LocalSet>,
151 // A list of suspension points, generated during the transform
152 suspension_points: Vec<SuspensionPoint>,
154 // The original RETURN_POINTER local
155 new_ret_local: Local,
158 impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
159 // Make a GeneratorState rvalue
160 fn make_state(&self, idx: usize, val: Operand<'tcx>) -> Rvalue<'tcx> {
161 let adt = AggregateKind::Adt(self.state_adt_ref, idx, self.state_substs, None);
162 Rvalue::Aggregate(box adt, vec![val])
165 // Create a Lvalue referencing a generator struct field
166 fn make_field(&self, idx: usize, ty: Ty<'tcx>) -> Lvalue<'tcx> {
167 let base = Lvalue::Local(self_arg());
168 let field = Projection {
170 elem: ProjectionElem::Field(Field::new(idx), ty),
172 Lvalue::Projection(Box::new(field))
175 // Create a statement which changes the generator state
176 fn set_state(&self, state_disc: u32, source_info: SourceInfo) -> Statement<'tcx> {
177 let state = self.make_field(self.state_field, self.tcx.types.u32);
178 let val = Operand::Constant(box Constant {
179 span: source_info.span,
180 ty: self.tcx.types.u32,
181 literal: Literal::Value {
182 value: self.tcx.mk_const(ty::Const {
183 val: ConstVal::Integral(ConstInt::U32(state_disc)),
184 ty: self.tcx.types.u32
190 kind: StatementKind::Assign(state, Rvalue::Use(val)),
195 impl<'a, 'tcx> MutVisitor<'tcx> for TransformVisitor<'a, 'tcx> {
196 fn visit_local(&mut self,
198 _: LvalueContext<'tcx>,
200 assert_eq!(self.remap.get(local), None);
203 fn visit_lvalue(&mut self,
204 lvalue: &mut Lvalue<'tcx>,
205 context: LvalueContext<'tcx>,
206 location: Location) {
207 if let Lvalue::Local(l) = *lvalue {
208 // Replace an Local in the remap with a generator struct access
209 if let Some(&(ty, idx)) = self.remap.get(&l) {
210 *lvalue = self.make_field(idx, ty);
213 self.super_lvalue(lvalue, context, location);
217 fn visit_basic_block_data(&mut self,
219 data: &mut BasicBlockData<'tcx>) {
220 // Remove StorageLive and StorageDead statements for remapped locals
221 data.retain_statements(|s| {
223 StatementKind::StorageLive(l) | StatementKind::StorageDead(l) => {
224 !self.remap.contains_key(&l)
230 let ret_val = match data.terminator().kind {
231 TerminatorKind::Return => Some((1,
233 Operand::Move(Lvalue::Local(self.new_ret_local)),
235 TerminatorKind::Yield { ref value, resume, drop } => Some((0,
242 if let Some((state_idx, resume, v, drop)) = ret_val {
243 let source_info = data.terminator().source_info;
244 // We must assign the value first in case it gets declared dead below
245 data.statements.push(Statement {
247 kind: StatementKind::Assign(Lvalue::Local(RETURN_POINTER),
248 self.make_state(state_idx, v)),
250 let state = if let Some(resume) = resume { // Yield
251 let state = 3 + self.suspension_points.len() as u32;
253 self.suspension_points.push(SuspensionPoint {
257 storage_liveness: self.storage_liveness.get(&block).unwrap().clone(),
262 1 // state for returned
264 data.statements.push(self.set_state(state, source_info));
265 data.terminator.as_mut().unwrap().kind = TerminatorKind::Return;
268 self.super_basic_block_data(block, data);
272 fn make_generator_state_argument_indirect<'a, 'tcx>(
273 tcx: TyCtxt<'a, 'tcx, 'tcx>,
275 mir: &mut Mir<'tcx>) {
276 let gen_ty = mir.local_decls.raw[1].ty;
278 let region = ty::ReFree(ty::FreeRegion {
280 bound_region: ty::BoundRegion::BrEnv,
283 let region = tcx.mk_region(region);
285 let ref_gen_ty = tcx.mk_ref(region, ty::TypeAndMut {
287 mutbl: hir::MutMutable
290 // Replace the by value generator argument
291 mir.local_decls.raw[1].ty = ref_gen_ty;
293 // Add a deref to accesses of the generator state
294 DerefArgVisitor.visit_mir(mir);
297 fn replace_result_variable<'tcx>(ret_ty: Ty<'tcx>,
298 mir: &mut Mir<'tcx>) -> Local {
299 let new_ret = LocalDecl {
300 mutability: Mutability::Mut,
303 source_info: source_info(mir),
304 lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
306 is_user_variable: false,
308 let new_ret_local = Local::new(mir.local_decls.len());
309 mir.local_decls.push(new_ret);
310 mir.local_decls.swap(0, new_ret_local.index());
313 from: RETURN_POINTER,
320 struct StorageIgnored(liveness::LocalSet);
322 impl<'tcx> Visitor<'tcx> for StorageIgnored {
323 fn visit_statement(&mut self,
325 statement: &Statement<'tcx>,
326 _location: Location) {
327 match statement.kind {
328 StatementKind::StorageLive(l) |
329 StatementKind::StorageDead(l) => { self.0.remove(&l); }
335 fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
337 source: MirSource) ->
339 HashMap<BasicBlock, liveness::LocalSet>) {
340 let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
341 let node_id = tcx.hir.as_local_node_id(source.def_id).unwrap();
342 let analysis = MaybeStorageLive::new(mir);
344 dataflow::do_dataflow(tcx, mir, node_id, &[], &dead_unwinds, analysis,
345 |bd, p| &bd.mir().local_decls[p]);
347 let mut ignored = StorageIgnored(IdxSetBuf::new_filled(mir.local_decls.len()));
348 ignored.visit_mir(mir);
350 let mut set = liveness::LocalSet::new_empty(mir.local_decls.len());
351 let liveness = liveness::liveness_of_locals(mir, LivenessMode {
352 include_regular_use: true,
355 liveness::dump_mir(tcx, "generator_liveness", source, mir, &liveness);
357 let mut storage_liveness_map = HashMap::new();
359 for (block, data) in mir.basic_blocks().iter_enumerated() {
360 if let TerminatorKind::Yield { .. } = data.terminator().kind {
363 statement_index: data.statements.len(),
366 let storage_liveness = state_for_location(loc, &analysis, &storage_live);
368 storage_liveness_map.insert(block, storage_liveness.clone());
370 let mut live_locals = storage_liveness;
372 // Mark locals without storage statements as always having live storage
373 live_locals.union(&ignored.0);
375 // Locals live are live at this point only if they are used across suspension points
376 // and their storage is live
377 live_locals.intersect(&liveness.outs[block]);
379 // Add the locals life at this suspension point to the set of locals which live across
380 // any suspension points
381 set.union(&live_locals);
385 // The generator argument is ignored
386 set.remove(&self_arg());
388 (set, storage_liveness_map)
391 fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
393 interior: GeneratorInterior<'tcx>,
395 -> (HashMap<Local, (Ty<'tcx>, usize)>,
396 GeneratorLayout<'tcx>,
397 HashMap<BasicBlock, liveness::LocalSet>)
399 // Use a liveness analysis to compute locals which are live across a suspension point
400 let (live_locals, storage_liveness) = locals_live_across_suspend_points(tcx, mir, source);
402 // Erase regions from the types passed in from typeck so we can compare them with
404 let allowed = tcx.erase_regions(&interior.as_slice());
406 for (local, decl) in mir.local_decls.iter_enumerated() {
407 // Ignore locals which are internal or not live
408 if !live_locals.contains(&local) || decl.internal {
412 // Sanity check that typeck knows about the type of locals which are
413 // live across a suspension point
414 if !allowed.contains(&decl.ty) {
416 "Broken MIR: generator contains type {} in MIR, \
417 but typeck only knows about {}",
423 let upvar_len = mir.upvar_decls.len();
424 let dummy_local = LocalDecl::new_internal(tcx.mk_nil(), mir.span);
426 // Gather live locals and their indices replacing values in mir.local_decls with a dummy
427 // to avoid changing local indices
428 let live_decls = live_locals.iter().map(|local| {
429 let var = mem::replace(&mut mir.local_decls[local], dummy_local.clone());
433 // Create a map from local indices to generator struct indices.
434 // These are offset by (upvar_len + 1) because of fields which comes before locals.
435 // We also create a vector of the LocalDecls of these locals.
436 let (remap, vars) = live_decls.enumerate().map(|(idx, (local, var))| {
437 ((local, (var.ty, upvar_len + 1 + idx)), var)
440 let layout = GeneratorLayout {
444 (remap, layout, storage_liveness)
447 fn insert_switch<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
449 cases: Vec<(u32, BasicBlock)>,
450 transform: &TransformVisitor<'a, 'tcx>,
451 default: TerminatorKind<'tcx>) {
452 let default_block = insert_term_block(mir, default);
454 let switch = TerminatorKind::SwitchInt {
455 discr: Operand::Copy(transform.make_field(transform.state_field, tcx.types.u32)),
456 switch_ty: tcx.types.u32,
457 values: Cow::from(cases.iter().map(|&(i, _)| ConstInt::U32(i)).collect::<Vec<_>>()),
458 targets: cases.iter().map(|&(_, d)| d).chain(once(default_block)).collect(),
461 let source_info = source_info(mir);
462 mir.basic_blocks_mut().raw.insert(0, BasicBlockData {
463 statements: Vec::new(),
464 terminator: Some(Terminator {
471 let blocks = mir.basic_blocks_mut().iter_mut();
473 for target in blocks.flat_map(|b| b.terminator_mut().successors_mut()) {
474 *target = BasicBlock::new(target.index() + 1);
478 fn elaborate_generator_drops<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
480 mir: &mut Mir<'tcx>) {
481 use util::elaborate_drops::{elaborate_drop, Unwind};
482 use util::patch::MirPatch;
483 use shim::DropShimElaborator;
485 // Note that `elaborate_drops` only drops the upvars of a generator, and
486 // this is ok because `open_drop` can only be reached within that own
487 // generator's resume function.
489 let param_env = tcx.param_env(def_id);
490 let gen = self_arg();
492 for block in mir.basic_blocks().indices() {
493 let (target, unwind, source_info) = match mir.basic_blocks()[block].terminator() {
496 kind: TerminatorKind::Drop {
497 location: Lvalue::Local(local),
501 } if local == gen => (target, unwind, source_info),
504 let unwind = if let Some(unwind) = unwind {
510 let mut elaborator = DropShimElaborator {
512 patch: MirPatch::new(mir),
531 fn create_generator_drop_shim<'a, 'tcx>(
532 tcx: TyCtxt<'a, 'tcx, 'tcx>,
533 transform: &TransformVisitor<'a, 'tcx>,
538 drop_clean: BasicBlock) -> Mir<'tcx> {
539 let mut mir = mir.clone();
541 let source_info = source_info(&mir);
543 let mut cases = create_cases(&mut mir, transform, |point| point.drop);
545 cases.insert(0, (0, drop_clean));
547 // The returned state (1) and the poisoned state (2) falls through to
548 // the default case which is just to return
550 insert_switch(tcx, &mut mir, cases, &transform, TerminatorKind::Return);
552 for block in mir.basic_blocks_mut() {
553 let kind = &mut block.terminator_mut().kind;
554 if let TerminatorKind::GeneratorDrop = *kind {
555 *kind = TerminatorKind::Return;
559 // Replace the return variable
560 mir.local_decls[RETURN_POINTER] = LocalDecl {
561 mutability: Mutability::Mut,
565 lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
567 is_user_variable: false,
570 make_generator_state_argument_indirect(tcx, def_id, &mut mir);
572 // Change the generator argument from &mut to *mut
573 mir.local_decls[self_arg()] = LocalDecl {
574 mutability: Mutability::Mut,
575 ty: tcx.mk_ptr(ty::TypeAndMut {
577 mutbl: hir::Mutability::MutMutable,
581 lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
583 is_user_variable: false,
586 no_landing_pads(tcx, &mut mir);
588 // Make sure we remove dead blocks to remove
589 // unrelated code from the resume part of the function
590 simplify::remove_dead_blocks(&mut mir);
592 dump_mir(tcx, None, "generator_drop", &0, source, &mut mir, |_, _| Ok(()) );
597 fn insert_term_block<'tcx>(mir: &mut Mir<'tcx>, kind: TerminatorKind<'tcx>) -> BasicBlock {
598 let term_block = BasicBlock::new(mir.basic_blocks().len());
599 let source_info = source_info(mir);
600 mir.basic_blocks_mut().push(BasicBlockData {
601 statements: Vec::new(),
602 terminator: Some(Terminator {
611 fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
613 message: AssertMessage<'tcx>) -> BasicBlock {
614 let assert_block = BasicBlock::new(mir.basic_blocks().len());
615 let term = TerminatorKind::Assert {
616 cond: Operand::Constant(box Constant {
619 literal: Literal::Value {
620 value: tcx.mk_const(ty::Const {
621 val: ConstVal::Bool(false),
628 target: assert_block,
632 let source_info = source_info(mir);
633 mir.basic_blocks_mut().push(BasicBlockData {
634 statements: Vec::new(),
635 terminator: Some(Terminator {
645 fn create_generator_resume_function<'a, 'tcx>(
646 tcx: TyCtxt<'a, 'tcx, 'tcx>,
647 transform: TransformVisitor<'a, 'tcx>,
650 mir: &mut Mir<'tcx>) {
651 // Poison the generator when it unwinds
652 for block in mir.basic_blocks_mut() {
653 let source_info = block.terminator().source_info;
654 if let &TerminatorKind::Resume = &block.terminator().kind {
655 block.statements.push(transform.set_state(1, source_info));
659 let mut cases = create_cases(mir, &transform, |point| Some(point.resume));
661 // Jump to the entry point on the 0 state
662 cases.insert(0, (0, BasicBlock::new(0)));
663 // Panic when resumed on the returned (1) state
664 cases.insert(1, (1, insert_panic_block(tcx, mir, AssertMessage::GeneratorResumedAfterReturn)));
665 // Panic when resumed on the poisoned (2) state
666 cases.insert(2, (2, insert_panic_block(tcx, mir, AssertMessage::GeneratorResumedAfterPanic)));
668 insert_switch(tcx, mir, cases, &transform, TerminatorKind::Unreachable);
670 make_generator_state_argument_indirect(tcx, def_id, mir);
672 no_landing_pads(tcx, mir);
674 // Make sure we remove dead blocks to remove
675 // unrelated code from the drop part of the function
676 simplify::remove_dead_blocks(mir);
678 dump_mir(tcx, None, "generator_resume", &0, source, mir, |_, _| Ok(()) );
681 fn source_info<'a, 'tcx>(mir: &Mir<'tcx>) -> SourceInfo {
684 scope: ARGUMENT_VISIBILITY_SCOPE,
688 fn insert_clean_drop<'a, 'tcx>(mir: &mut Mir<'tcx>) -> BasicBlock {
689 let return_block = insert_term_block(mir, TerminatorKind::Return);
691 // Create a block to destroy an unresumed generators. This can only destroy upvars.
692 let drop_clean = BasicBlock::new(mir.basic_blocks().len());
693 let term = TerminatorKind::Drop {
694 location: Lvalue::Local(self_arg()),
695 target: return_block,
698 let source_info = source_info(mir);
699 mir.basic_blocks_mut().push(BasicBlockData {
700 statements: Vec::new(),
701 terminator: Some(Terminator {
711 fn create_cases<'a, 'tcx, F>(mir: &mut Mir<'tcx>,
712 transform: &TransformVisitor<'a, 'tcx>,
713 target: F) -> Vec<(u32, BasicBlock)>
714 where F: Fn(&SuspensionPoint) -> Option<BasicBlock> {
715 let source_info = source_info(mir);
717 transform.suspension_points.iter().filter_map(|point| {
718 // Find the target for this suspension point, if applicable
719 target(point).map(|target| {
720 let block = BasicBlock::new(mir.basic_blocks().len());
721 let mut statements = Vec::new();
723 // Create StorageLive instructions for locals with live storage
724 for i in 0..(mir.local_decls.len()) {
725 let l = Local::new(i);
726 if point.storage_liveness.contains(&l) && !transform.remap.contains_key(&l) {
727 statements.push(Statement {
729 kind: StatementKind::StorageLive(l),
734 // Then jump to the real target
735 mir.basic_blocks_mut().push(BasicBlockData {
737 terminator: Some(Terminator {
739 kind: TerminatorKind::Goto {
751 impl MirPass for StateTransform {
752 fn run_pass<'a, 'tcx>(&self,
753 tcx: TyCtxt<'a, 'tcx, 'tcx>,
755 mir: &mut Mir<'tcx>) {
756 let yield_ty = if let Some(yield_ty) = mir.yield_ty {
759 // This only applies to generators
763 assert!(mir.generator_drop.is_none());
765 let def_id = source.def_id;
766 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
767 let hir_id = tcx.hir.node_to_hir_id(node_id);
769 // Get the interior types which typeck computed
770 let tables = tcx.typeck_tables_of(def_id);
771 let interior = match tables.node_id_to_type(hir_id).sty {
772 ty::TyGenerator(_, _, interior) => interior,
773 ref t => bug!("type of generator not a generator: {:?}", t),
776 // The first argument is the generator type passed by value
777 let gen_ty = mir.local_decls.raw[1].ty;
779 // Compute GeneratorState<yield_ty, return_ty>
780 let state_did = tcx.lang_items().gen_state().unwrap();
781 let state_adt_ref = tcx.adt_def(state_did);
782 let state_substs = tcx.mk_substs([Kind::from(yield_ty),
783 Kind::from(mir.return_ty())].iter());
784 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
786 // We rename RETURN_POINTER which has type mir.return_ty to new_ret_local
787 // RETURN_POINTER then is a fresh unused local with type ret_ty.
788 let new_ret_local = replace_result_variable(ret_ty, mir);
790 // Extract locals which are live across suspension point into `layout`
791 // `remap` gives a mapping from local indices onto generator struct indices
792 // `storage_liveness` tells us which locals have live storage at suspension points
793 let (remap, layout, storage_liveness) = compute_layout(tcx, source, interior, mir);
795 let state_field = mir.upvar_decls.len();
797 // Run the transformation which converts Lvalues from Local to generator struct
798 // accesses for locals in `remap`.
799 // It also rewrites `return x` and `yield y` as writing a new generator state and returning
800 // GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
801 let mut transform = TransformVisitor {
807 suspension_points: Vec::new(),
811 transform.visit_mir(mir);
813 // Update our MIR struct to reflect the changed we've made
816 mir.spread_arg = None;
817 mir.generator_layout = Some(layout);
819 // Insert `drop(generator_struct)` which is used to drop upvars for generators in
820 // the unresumed (0) state.
821 // This is expanded to a drop ladder in `elaborate_generator_drops`.
822 let drop_clean = insert_clean_drop(mir);
824 dump_mir(tcx, None, "generator_pre-elab", &0, source, mir, |_, _| Ok(()) );
826 // Expand `drop(generator_struct)` to a drop ladder which destroys upvars.
827 // If any upvars are moved out of, drop elaboration will handle upvar destruction.
828 // However we need to also elaborate the code generated by `insert_clean_drop`.
829 elaborate_generator_drops(tcx, def_id, mir);
831 dump_mir(tcx, None, "generator_post-transform", &0, source, mir, |_, _| Ok(()) );
833 // Create a copy of our MIR and use it to create the drop shim for the generator
834 let drop_shim = create_generator_drop_shim(tcx,
842 mir.generator_drop = Some(box drop_shim);
844 // Create the Generator::resume function
845 create_generator_resume_function(tcx, transform, def_id, source, mir);