2017-08-25 00:57:08 +02:00
|
|
|
//! This is the implementation of the pass which transforms generators into state machines.
|
|
|
|
//!
|
|
|
|
//! MIR generation for generators creates a function which has a self argument which
|
|
|
|
//! passes by value. This argument is effectively a generator type which only contains upvars and
|
|
|
|
//! is only used for this argument inside the MIR for the generator.
|
|
|
|
//! It is passed by value to enable upvars to be moved out of it. Drop elaboration runs on that
|
|
|
|
//! MIR before this pass and creates drop flags for MIR locals.
|
|
|
|
//! It will also drop the generator argument (which only consists of upvars) if any of the upvars
|
|
|
|
//! are moved out of. This pass elaborates the drops of upvars / generator argument in the case
|
|
|
|
//! that none of the upvars were moved out of. This is because we cannot have any drops of this
|
|
|
|
//! generator in the MIR, since it is used to create the drop glue for the generator. We'd get
|
|
|
|
//! infinite recursion otherwise.
|
|
|
|
//!
|
|
|
|
//! This pass creates the implementation for the Generator::resume function and the drop shim
|
|
|
|
//! for the generator based on the MIR input. It converts the generator argument from Self to
|
|
|
|
//! &mut Self adding derefs in the MIR as needed. It computes the final layout of the generator
|
|
|
|
//! struct which looks like this:
|
|
|
|
//! First upvars are stored
|
|
|
|
//! It is followed by the generator state field.
|
|
|
|
//! Then finally the MIR locals which are live across a suspension point are stored.
|
|
|
|
//!
|
|
|
|
//! struct Generator {
|
|
|
|
//! upvars...,
|
|
|
|
//! state: u32,
|
|
|
|
//! mir_locals...,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! This pass computes the meaning of the state field and the MIR locals which are live
|
2019-03-03 19:28:05 +00:00
|
|
|
//! across a suspension point. There are however three hardcoded generator states:
|
2017-08-25 00:57:08 +02:00
|
|
|
//! 0 - Generator have not been resumed yet
|
2017-09-10 22:34:56 +02:00
|
|
|
//! 1 - Generator has returned / is completed
|
|
|
|
//! 2 - Generator has been poisoned
|
2017-08-25 00:57:08 +02:00
|
|
|
//!
|
|
|
|
//! It also rewrites `return x` and `yield y` as setting a new generator state and returning
|
|
|
|
//! GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
|
|
|
|
//! MIR locals which are live across a suspension point are moved to the generator struct
|
|
|
|
//! with references to them being updated with references to the generator struct.
|
|
|
|
//!
|
|
|
|
//! The pass creates two functions which have a switch on the generator state giving
|
|
|
|
//! the action to take.
|
|
|
|
//!
|
|
|
|
//! One of them is the implementation of Generator::resume.
|
|
|
|
//! For generators with state 0 (unresumed) it starts the execution of the generator.
|
2017-09-10 22:34:56 +02:00
|
|
|
//! For generators with state 1 (returned) and state 2 (poisoned) it panics.
|
2017-08-25 00:57:08 +02:00
|
|
|
//! Otherwise it continues the execution from the last suspension point.
|
|
|
|
//!
|
|
|
|
//! The other function is the drop glue for the generator.
|
|
|
|
//! For generators with state 0 (unresumed) it drops the upvars of the generator.
|
2017-09-10 22:34:56 +02:00
|
|
|
//! For generators with state 1 (returned) and state 2 (poisoned) it does nothing.
|
2017-08-25 00:57:08 +02:00
|
|
|
//! Otherwise it drops all the values in scope at the last suspension point.
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-02-28 22:02:20 -08:00
|
|
|
use crate::dataflow::{self, Analysis};
|
2020-04-10 11:13:31 -07:00
|
|
|
use crate::dataflow::{
|
|
|
|
MaybeBorrowedLocals, MaybeLiveLocals, MaybeRequiresStorage, MaybeStorageLive,
|
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::transform::no_landing_pads::no_landing_pads;
|
|
|
|
use crate::transform::simplify;
|
|
|
|
use crate::transform::{MirPass, MirSource};
|
|
|
|
use crate::util::dump_mir;
|
2020-03-29 14:17:53 -07:00
|
|
|
use crate::util::storage;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_index::bit_set::{BitMatrix, BitSet};
|
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2020-03-29 14:17:53 -07:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
|
|
|
use rustc_middle::ty::GeneratorSubsts;
|
|
|
|
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2020-03-31 23:15:39 +01:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2016-12-26 14:34:03 +01:00
|
|
|
use std::borrow::Cow;
|
2019-04-02 16:04:51 -07:00
|
|
|
use std::iter;
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
pub struct StateTransform;
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
struct RenameLocalVisitor<'tcx> {
|
2016-12-26 14:34:03 +01:00
|
|
|
from: Local,
|
|
|
|
to: Local,
|
2019-10-20 16:11:04 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
|
2016-12-26 14:34:03 +01:00
|
|
|
if *local == self.from {
|
|
|
|
*local = self.to;
|
|
|
|
}
|
2019-10-07 19:14:35 -03:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
struct DerefArgVisitor<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
|
2020-03-10 13:51:07 -07:00
|
|
|
assert_ne!(*local, SELF_ARG);
|
2017-09-03 19:14:31 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
|
2020-03-10 13:51:07 -07:00
|
|
|
if place.local == SELF_ARG {
|
2019-12-22 17:42:04 -05:00
|
|
|
replace_base(
|
|
|
|
place,
|
|
|
|
Place {
|
2020-03-10 13:51:07 -07:00
|
|
|
local: SELF_ARG,
|
2020-01-22 17:19:26 +01:00
|
|
|
projection: self.tcx().intern_place_elems(&[ProjectionElem::Deref]),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
self.tcx,
|
|
|
|
);
|
2016-12-26 14:34:03 +01:00
|
|
|
} else {
|
2020-04-21 17:11:00 -03:00
|
|
|
self.visit_local(&mut place.local, context, location);
|
2019-10-07 19:14:35 -03:00
|
|
|
|
|
|
|
for elem in place.projection.iter() {
|
|
|
|
if let PlaceElem::Index(local) = elem {
|
2020-03-10 13:51:07 -07:00
|
|
|
assert_ne!(*local, SELF_ARG);
|
2019-10-07 19:14:35 -03:00
|
|
|
}
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:11:58 +01:00
|
|
|
struct PinArgVisitor<'tcx> {
|
|
|
|
ref_gen_ty: Ty<'tcx>,
|
2019-10-20 16:11:04 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-11-07 00:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
|
2020-03-10 13:51:07 -07:00
|
|
|
assert_ne!(*local, SELF_ARG);
|
2018-11-07 00:11:58 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
|
2020-03-10 13:51:07 -07:00
|
|
|
if place.local == SELF_ARG {
|
2019-10-20 16:11:04 -04:00
|
|
|
replace_base(
|
|
|
|
place,
|
|
|
|
Place {
|
2020-03-10 13:51:07 -07:00
|
|
|
local: SELF_ARG,
|
2020-01-22 17:19:26 +01:00
|
|
|
projection: self.tcx().intern_place_elems(&[ProjectionElem::Field(
|
2019-12-22 17:42:04 -05:00
|
|
|
Field::new(0),
|
|
|
|
self.ref_gen_ty,
|
2019-10-20 16:11:04 -04:00
|
|
|
)]),
|
|
|
|
},
|
|
|
|
self.tcx,
|
|
|
|
);
|
2018-11-07 00:11:58 +01:00
|
|
|
} else {
|
2020-04-21 17:11:00 -03:00
|
|
|
self.visit_local(&mut place.local, context, location);
|
2019-10-07 19:14:35 -03:00
|
|
|
|
|
|
|
for elem in place.projection.iter() {
|
|
|
|
if let PlaceElem::Index(local) = elem {
|
2020-03-10 13:51:07 -07:00
|
|
|
assert_ne!(*local, SELF_ARG);
|
2019-10-07 19:14:35 -03:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 00:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
fn replace_base<'tcx>(place: &mut Place<'tcx>, new_base: Place<'tcx>, tcx: TyCtxt<'tcx>) {
|
2019-12-11 16:50:03 -03:00
|
|
|
place.local = new_base.local;
|
2019-07-30 00:07:28 +02:00
|
|
|
|
|
|
|
let mut new_projection = new_base.projection.to_vec();
|
|
|
|
new_projection.append(&mut place.projection.to_vec());
|
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
place.projection = tcx.intern_place_elems(&new_projection);
|
2019-06-08 10:24:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:51:07 -07:00
|
|
|
const SELF_ARG: Local = Local::from_u32(1);
|
2017-08-25 07:19:40 -07:00
|
|
|
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Generator has not been resumed yet.
|
2019-04-12 17:03:03 -07:00
|
|
|
const UNRESUMED: usize = GeneratorSubsts::UNRESUMED;
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Generator has returned / is completed.
|
2019-04-12 17:03:03 -07:00
|
|
|
const RETURNED: usize = GeneratorSubsts::RETURNED;
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Generator has panicked and is poisoned.
|
2019-04-12 17:03:03 -07:00
|
|
|
const POISONED: usize = GeneratorSubsts::POISONED;
|
2019-03-03 19:28:05 +00:00
|
|
|
|
2020-02-19 23:39:00 +01:00
|
|
|
/// A `yield` point in the generator.
|
2020-01-25 02:33:52 +01:00
|
|
|
struct SuspensionPoint<'tcx> {
|
2020-02-19 23:39:00 +01:00
|
|
|
/// State discriminant used when suspending or resuming at this point.
|
2019-04-02 16:04:51 -07:00
|
|
|
state: usize,
|
2020-02-19 23:39:00 +01:00
|
|
|
/// The block to jump to after resumption.
|
2017-09-10 22:34:56 +02:00
|
|
|
resume: BasicBlock,
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Where to move the resume argument after resumption.
|
2020-01-25 02:33:52 +01:00
|
|
|
resume_arg: Place<'tcx>,
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Which block to jump to if the generator is dropped in this state.
|
2017-09-10 22:34:56 +02:00
|
|
|
drop: Option<BasicBlock>,
|
2020-02-19 23:39:00 +01:00
|
|
|
/// Set of locals that have live storage while at this suspension point.
|
2020-04-10 11:13:31 -07:00
|
|
|
storage_liveness: BitSet<Local>,
|
2017-09-10 22:34:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
struct TransformVisitor<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-12-26 14:34:03 +01:00
|
|
|
state_adt_ref: &'tcx AdtDef,
|
2019-02-09 22:11:53 +08:00
|
|
|
state_substs: SubstsRef<'tcx>,
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2019-04-02 16:04:51 -07:00
|
|
|
// The type of the discriminant in the generator struct
|
2019-04-08 17:44:00 -07:00
|
|
|
discr_ty: Ty<'tcx>,
|
2017-08-11 06:20:28 +02:00
|
|
|
|
|
|
|
// Mapping from Local to (type of local, generator struct index)
|
2018-08-18 13:55:43 +03:00
|
|
|
// FIXME(eddyb) This should use `IndexVec<Local, Option<_>>`.
|
2019-04-02 16:04:51 -07:00
|
|
|
remap: FxHashMap<Local, (Ty<'tcx>, VariantIdx, usize)>,
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2017-09-10 22:34:56 +02:00
|
|
|
// A map from a suspension point in a block to the locals which have live storage at that point
|
2020-04-10 11:13:31 -07:00
|
|
|
storage_liveness: IndexVec<BasicBlock, Option<BitSet<Local>>>,
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2017-09-10 22:34:56 +02:00
|
|
|
// A list of suspension points, generated during the transform
|
2020-01-25 02:33:52 +01:00
|
|
|
suspension_points: Vec<SuspensionPoint<'tcx>>,
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2020-03-29 14:17:53 -07:00
|
|
|
// The set of locals that have no `StorageLive`/`StorageDead` annotations.
|
|
|
|
always_live_locals: storage::AlwaysLiveLocals,
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// The original RETURN_PLACE local
|
2016-12-26 14:34:03 +01:00
|
|
|
new_ret_local: Local,
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl TransformVisitor<'tcx> {
|
2017-08-11 06:20:28 +02:00
|
|
|
// Make a GeneratorState rvalue
|
2018-11-01 19:03:38 +01:00
|
|
|
fn make_state(&self, idx: VariantIdx, val: Operand<'tcx>) -> Rvalue<'tcx> {
|
2018-08-09 11:56:53 -04:00
|
|
|
let adt = AggregateKind::Adt(self.state_adt_ref, idx, self.state_substs, None, None);
|
2016-12-26 14:34:03 +01:00
|
|
|
Rvalue::Aggregate(box adt, vec![val])
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:31:47 +02:00
|
|
|
// Create a Place referencing a generator struct field
|
2019-04-02 16:04:51 -07:00
|
|
|
fn make_field(&self, variant_index: VariantIdx, idx: usize, ty: Ty<'tcx>) -> Place<'tcx> {
|
2020-03-10 13:51:07 -07:00
|
|
|
let self_place = Place::from(SELF_ARG);
|
2019-10-20 21:04:59 -04:00
|
|
|
let base = self.tcx.mk_place_downcast_unnamed(self_place, variant_index);
|
2019-07-30 00:07:28 +02:00
|
|
|
let mut projection = base.projection.to_vec();
|
|
|
|
projection.push(ProjectionElem::Field(Field::new(idx), ty));
|
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
Place { local: base.local, projection: self.tcx.intern_place_elems(&projection) }
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 16:04:51 -07:00
|
|
|
// Create a statement which changes the discriminant
|
|
|
|
fn set_discr(&self, state_disc: VariantIdx, source_info: SourceInfo) -> Statement<'tcx> {
|
2020-03-10 13:51:07 -07:00
|
|
|
let self_place = Place::from(SELF_ARG);
|
2016-12-26 14:34:03 +01:00
|
|
|
Statement {
|
|
|
|
source_info,
|
2019-09-11 16:05:45 -03:00
|
|
|
kind: StatementKind::SetDiscriminant {
|
|
|
|
place: box self_place,
|
|
|
|
variant_index: state_disc,
|
|
|
|
},
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 16:04:51 -07:00
|
|
|
|
|
|
|
// Create a statement which reads the discriminant into a temporary
|
2019-06-03 18:26:48 -04:00
|
|
|
fn get_discr(&self, body: &mut Body<'tcx>) -> (Statement<'tcx>, Place<'tcx>) {
|
|
|
|
let temp_decl = LocalDecl::new_internal(self.tcx.types.isize, body.span);
|
|
|
|
let local_decls_len = body.local_decls.push(temp_decl);
|
2019-06-24 17:46:09 +02:00
|
|
|
let temp = Place::from(local_decls_len);
|
2019-04-02 16:04:51 -07:00
|
|
|
|
2020-03-10 13:51:07 -07:00
|
|
|
let self_place = Place::from(SELF_ARG);
|
2019-04-02 16:04:51 -07:00
|
|
|
let assign = Statement {
|
2020-05-06 10:30:11 +10:00
|
|
|
source_info: SourceInfo::outermost(body.span),
|
2020-01-22 16:30:15 +01:00
|
|
|
kind: StatementKind::Assign(box (temp, Rvalue::Discriminant(self_place))),
|
2019-04-02 16:04:51 -07:00
|
|
|
};
|
|
|
|
(assign, temp)
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl MutVisitor<'tcx> for TransformVisitor<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
|
2017-09-03 19:14:31 +03:00
|
|
|
assert_eq!(self.remap.get(local), None);
|
|
|
|
}
|
|
|
|
|
2019-12-11 10:39:24 -03:00
|
|
|
fn visit_place(
|
|
|
|
&mut self,
|
|
|
|
place: &mut Place<'tcx>,
|
|
|
|
_context: PlaceContext,
|
|
|
|
_location: Location,
|
|
|
|
) {
|
2019-12-11 16:50:03 -03:00
|
|
|
// Replace an Local in the remap with a generator struct access
|
|
|
|
if let Some(&(ty, variant_index, idx)) = self.remap.get(&place.local) {
|
|
|
|
replace_base(place, self.make_field(variant_index, idx, ty), self.tcx);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
|
2017-08-11 06:20:28 +02:00
|
|
|
// Remove StorageLive and StorageDead statements for remapped locals
|
2019-12-22 17:42:04 -05:00
|
|
|
data.retain_statements(|s| match s.kind {
|
|
|
|
StatementKind::StorageLive(l) | StatementKind::StorageDead(l) => {
|
|
|
|
!self.remap.contains_key(&l)
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => true,
|
2016-12-26 14:34:03 +01:00
|
|
|
});
|
|
|
|
|
2017-08-25 07:17:37 -07:00
|
|
|
let ret_val = match data.terminator().kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
TerminatorKind::Return => Some((
|
|
|
|
VariantIdx::new(1),
|
2017-09-10 22:34:56 +02:00
|
|
|
None,
|
2019-06-24 17:46:09 +02:00
|
|
|
Operand::Move(Place::from(self.new_ret_local)),
|
2019-12-22 17:42:04 -05:00
|
|
|
None,
|
|
|
|
)),
|
2020-01-25 02:33:52 +01:00
|
|
|
TerminatorKind::Yield { ref value, resume, resume_arg, drop } => {
|
|
|
|
Some((VariantIdx::new(0), Some((resume, resume_arg)), value.clone(), drop))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
_ => None,
|
2017-08-25 07:17:37 -07:00
|
|
|
};
|
|
|
|
|
2017-08-09 13:56:19 -07:00
|
|
|
if let Some((state_idx, resume, v, drop)) = ret_val {
|
2016-12-26 14:34:03 +01:00
|
|
|
let source_info = data.terminator().source_info;
|
2017-09-11 12:15:35 +02:00
|
|
|
// We must assign the value first in case it gets declared dead below
|
|
|
|
data.statements.push(Statement {
|
|
|
|
source_info,
|
2019-12-22 17:42:04 -05:00
|
|
|
kind: StatementKind::Assign(box (
|
|
|
|
Place::return_place(),
|
|
|
|
self.make_state(state_idx, v),
|
|
|
|
)),
|
2017-09-11 12:15:35 +02:00
|
|
|
});
|
2020-01-25 02:33:52 +01:00
|
|
|
let state = if let Some((resume, resume_arg)) = resume {
|
2019-12-22 17:42:04 -05:00
|
|
|
// Yield
|
2019-04-02 16:04:51 -07:00
|
|
|
let state = 3 + self.suspension_points.len();
|
2017-09-10 22:34:56 +02:00
|
|
|
|
2020-02-19 23:38:31 +01:00
|
|
|
// 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)
|
|
|
|
} else {
|
|
|
|
resume_arg
|
|
|
|
};
|
|
|
|
|
2017-09-10 22:34:56 +02:00
|
|
|
self.suspension_points.push(SuspensionPoint {
|
|
|
|
state,
|
|
|
|
resume,
|
2020-01-25 02:33:52 +01:00
|
|
|
resume_arg,
|
2017-09-10 22:34:56 +02:00
|
|
|
drop,
|
2020-04-25 13:46:37 +02:00
|
|
|
storage_liveness: self.storage_liveness[block].clone().unwrap(),
|
2017-09-10 22:34:56 +02:00
|
|
|
});
|
|
|
|
|
2019-04-02 16:04:51 -07:00
|
|
|
VariantIdx::new(state)
|
2019-12-22 17:42:04 -05:00
|
|
|
} else {
|
|
|
|
// Return
|
2019-04-02 16:04:51 -07:00
|
|
|
VariantIdx::new(RETURNED) // state for returned
|
2017-09-10 22:34:56 +02:00
|
|
|
};
|
2019-04-02 16:04:51 -07:00
|
|
|
data.statements.push(self.set_discr(state, source_info));
|
2019-10-04 00:55:28 -04:00
|
|
|
data.terminator_mut().kind = TerminatorKind::Return;
|
2017-08-09 13:56:19 -07:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
self.super_basic_block_data(block, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn make_generator_state_argument_indirect<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2019-11-06 12:00:46 -05:00
|
|
|
let gen_ty = body.local_decls.raw[1].ty;
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2020-03-19 11:40:38 +00:00
|
|
|
let ref_gen_ty =
|
|
|
|
tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty: gen_ty, mutbl: Mutability::Mut });
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
// Replace the by value generator argument
|
2019-11-06 12:00:46 -05:00
|
|
|
body.local_decls.raw[1].ty = ref_gen_ty;
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-07-10 20:04:15 +02:00
|
|
|
// Add a deref to accesses of the generator state
|
2019-11-06 12:00:46 -05:00
|
|
|
DerefArgVisitor { tcx }.visit_body(body);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn make_generator_state_argument_pinned<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2019-11-06 12:00:46 -05:00
|
|
|
let ref_gen_ty = body.local_decls.raw[1].ty;
|
2018-11-07 00:11:58 +01:00
|
|
|
|
|
|
|
let pin_did = tcx.lang_items().pin_type().unwrap();
|
|
|
|
let pin_adt_ref = tcx.adt_def(pin_did);
|
|
|
|
let substs = tcx.intern_substs(&[ref_gen_ty.into()]);
|
|
|
|
let pin_ref_gen_ty = tcx.mk_adt(pin_adt_ref, substs);
|
|
|
|
|
|
|
|
// Replace the by ref generator argument
|
2019-11-06 12:00:46 -05:00
|
|
|
body.local_decls.raw[1].ty = pin_ref_gen_ty;
|
2018-11-07 00:11:58 +01:00
|
|
|
|
|
|
|
// Add the Pin field access to accesses of the generator state
|
2019-11-06 12:00:46 -05:00
|
|
|
PinArgVisitor { ref_gen_ty, tcx }.visit_body(body);
|
2018-11-07 00:11:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 22:46:06 +01:00
|
|
|
/// Allocates a new local and replaces all references of `local` with it. Returns the new local.
|
|
|
|
///
|
|
|
|
/// `local` will be changed to a new local decl with type `ty`.
|
|
|
|
///
|
|
|
|
/// Note that the new local will be uninitialized. It is the caller's responsibility to assign some
|
|
|
|
/// valid value to it before its first use.
|
|
|
|
fn replace_local<'tcx>(
|
|
|
|
local: Local,
|
|
|
|
ty: Ty<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-10-20 16:11:04 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-07-22 19:23:39 +03:00
|
|
|
) -> Local {
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2020-02-02 22:46:06 +01:00
|
|
|
let new_decl = LocalDecl {
|
2016-12-26 14:34:03 +01:00
|
|
|
mutability: Mutability::Mut,
|
2020-02-02 22:46:06 +01:00
|
|
|
ty,
|
2018-10-22 14:23:44 +02:00
|
|
|
user_ty: UserTypeProjections::none(),
|
2018-05-29 21:31:33 +03:00
|
|
|
source_info,
|
2017-07-15 22:41:33 +02:00
|
|
|
internal: false,
|
2018-09-22 00:51:48 +02:00
|
|
|
is_block_tail: None,
|
2019-12-22 17:42:04 -05:00
|
|
|
local_info: LocalInfo::Other,
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
2020-04-25 13:46:37 +02:00
|
|
|
let new_local = body.local_decls.push(new_decl);
|
2020-02-02 22:46:06 +01:00
|
|
|
body.local_decls.swap(local, new_local);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-02-02 22:46:06 +01:00
|
|
|
RenameLocalVisitor { from: local, to: new_local, tcx }.visit_body(body);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-02-02 22:46:06 +01:00
|
|
|
new_local
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 16:36:36 -07:00
|
|
|
struct LivenessInfo {
|
|
|
|
/// Which locals are live across any suspension point.
|
|
|
|
///
|
|
|
|
/// GeneratorSavedLocal is indexed in terms of the elements in this set;
|
|
|
|
/// i.e. GeneratorSavedLocal::new(1) corresponds to the second local
|
|
|
|
/// included in this set.
|
2020-04-10 11:13:31 -07:00
|
|
|
live_locals: BitSet<Local>,
|
2019-05-22 16:36:36 -07:00
|
|
|
|
|
|
|
/// The set of saved locals live at each suspension point.
|
|
|
|
live_locals_at_suspension_points: Vec<BitSet<GeneratorSavedLocal>>,
|
|
|
|
|
|
|
|
/// For every saved local, the set of other saved locals that are
|
|
|
|
/// storage-live at the same time as this local. We cannot overlap locals in
|
|
|
|
/// the layout which have conflicting storage.
|
2019-05-31 16:53:27 -07:00
|
|
|
storage_conflicts: BitMatrix<GeneratorSavedLocal, GeneratorSavedLocal>,
|
2019-05-22 16:36:36 -07:00
|
|
|
|
|
|
|
/// For every suspending block, the locals which are storage-live across
|
|
|
|
/// that suspension point.
|
2020-04-10 11:13:31 -07:00
|
|
|
storage_liveness: IndexVec<BasicBlock, Option<BitSet<Local>>>,
|
2019-05-22 16:36:36 -07:00
|
|
|
}
|
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
fn locals_live_across_suspend_points(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &Body<'tcx>,
|
2019-02-09 12:19:04 +01:00
|
|
|
source: MirSource<'tcx>,
|
2020-03-29 14:17:53 -07:00
|
|
|
always_live_locals: &storage::AlwaysLiveLocals,
|
2018-08-18 13:55:43 +03:00
|
|
|
movable: bool,
|
2019-05-22 16:36:36 -07:00
|
|
|
) -> LivenessInfo {
|
2019-03-17 10:36:10 +00:00
|
|
|
let def_id = source.def_id();
|
2019-11-06 12:00:46 -05:00
|
|
|
let body_ref: &Body<'_> = &body;
|
2018-01-29 08:48:56 +01:00
|
|
|
|
|
|
|
// Calculate when MIR locals have live storage. This gives us an upper bound of their
|
|
|
|
// lifetimes.
|
2020-03-29 14:17:53 -07:00
|
|
|
let mut storage_live = MaybeStorageLive::new(always_live_locals.clone())
|
2020-02-18 10:03:00 -08:00
|
|
|
.into_engine(tcx, body_ref, def_id)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(body_ref);
|
2017-09-10 22:34:56 +02:00
|
|
|
|
2018-01-29 08:48:56 +01:00
|
|
|
// Calculate the MIR locals which have been previously
|
|
|
|
// borrowed (even if they are still active).
|
2020-02-12 13:41:26 -08:00
|
|
|
let borrowed_locals_results =
|
2020-02-13 13:56:19 -08:00
|
|
|
MaybeBorrowedLocals::all_borrows().into_engine(tcx, body_ref, def_id).iterate_to_fixpoint();
|
2020-02-12 13:41:26 -08:00
|
|
|
|
2020-02-18 10:31:01 -08:00
|
|
|
let mut borrowed_locals_cursor =
|
|
|
|
dataflow::ResultsCursor::new(body_ref, &borrowed_locals_results);
|
2019-06-17 19:08:12 -07:00
|
|
|
|
|
|
|
// Calculate the MIR locals that we actually need to keep storage around
|
|
|
|
// for.
|
2020-02-18 10:35:16 -08:00
|
|
|
let requires_storage_results = MaybeRequiresStorage::new(body, &borrowed_locals_results)
|
2020-02-18 10:31:01 -08:00
|
|
|
.into_engine(tcx, body_ref, def_id)
|
|
|
|
.iterate_to_fixpoint();
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut requires_storage_cursor =
|
2020-02-18 10:31:01 -08:00
|
|
|
dataflow::ResultsCursor::new(body_ref, &requires_storage_results);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
|
|
|
// Calculate the liveness of MIR locals ignoring borrows.
|
2020-04-10 11:13:31 -07:00
|
|
|
let mut liveness = MaybeLiveLocals
|
|
|
|
.into_engine(tcx, body_ref, def_id)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(body_ref);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-04-25 13:46:37 +02:00
|
|
|
let mut storage_liveness_map = IndexVec::from_elem(None, body.basic_blocks());
|
2019-05-09 18:07:30 -07:00
|
|
|
let mut live_locals_at_suspension_points = Vec::new();
|
2020-04-10 11:13:31 -07:00
|
|
|
let mut live_locals_at_any_suspension_point = BitSet::new_empty(body.local_decls.len());
|
2019-04-02 16:04:51 -07:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
for (block, data) in body.basic_blocks().iter_enumerated() {
|
2017-07-10 21:11:31 +02:00
|
|
|
if let TerminatorKind::Yield { .. } = data.terminator().kind {
|
2020-03-06 19:28:44 +01:00
|
|
|
let loc = Location { block, statement_index: data.statements.len() };
|
2017-09-10 22:34:56 +02:00
|
|
|
|
2020-04-10 11:13:31 -07:00
|
|
|
liveness.seek_to_block_end(block);
|
|
|
|
let mut live_locals = liveness.get().clone();
|
|
|
|
|
2019-06-17 19:08:12 -07:00
|
|
|
if !movable {
|
2018-01-29 08:48:56 +01:00
|
|
|
// The `liveness` variable contains the liveness of MIR locals ignoring borrows.
|
|
|
|
// This is correct for movable generators since borrows cannot live across
|
|
|
|
// suspension points. However for immovable generators we need to account for
|
2018-07-31 21:54:30 +02:00
|
|
|
// borrows, so we conseratively assume that all borrowed locals are live until
|
|
|
|
// we find a StorageDead statement referencing the locals.
|
2018-01-29 08:48:56 +01:00
|
|
|
// To do this we just union our `liveness` result with `borrowed_locals`, which
|
|
|
|
// contains all the locals which has been borrowed before this suspension point.
|
|
|
|
// If a borrow is converted to a raw reference, we must also assume that it lives
|
|
|
|
// forever. Note that the final liveness is still bounded by the storage liveness
|
|
|
|
// of the local, which happens using the `intersect` operation below.
|
2020-03-22 12:09:40 -07:00
|
|
|
borrowed_locals_cursor.seek_before_primary_effect(loc);
|
2020-04-10 11:13:31 -07:00
|
|
|
live_locals.union(borrowed_locals_cursor.get());
|
2018-01-29 08:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the storage liveness for later use so we can restore the state
|
|
|
|
// after a suspension point
|
2020-03-22 12:09:40 -07:00
|
|
|
storage_live.seek_before_primary_effect(loc);
|
|
|
|
storage_liveness_map[block] = Some(storage_live.get().clone());
|
2017-09-10 22:34:56 +02:00
|
|
|
|
2018-01-29 08:48:56 +01:00
|
|
|
// Locals live are live at this point only if they are used across
|
|
|
|
// suspension points (the `liveness` variable)
|
2019-06-17 19:08:12 -07:00
|
|
|
// and their storage is required (the `storage_required` variable)
|
2020-04-10 11:13:31 -07:00
|
|
|
requires_storage_cursor.seek_before_primary_effect(loc);
|
|
|
|
live_locals.intersect(requires_storage_cursor.get());
|
2017-10-07 16:36:28 +02:00
|
|
|
|
2020-03-06 00:32:06 +01:00
|
|
|
// The generator argument is ignored.
|
2020-04-10 11:19:24 -07:00
|
|
|
live_locals.remove(SELF_ARG);
|
2017-09-10 22:34:56 +02:00
|
|
|
|
2020-04-10 11:19:24 -07:00
|
|
|
debug!("loc = {:?}, live_locals = {:?}", loc, live_locals);
|
2019-06-17 19:08:12 -07:00
|
|
|
|
2019-05-09 18:07:30 -07:00
|
|
|
// Add the locals live at this suspension point to the set of locals which live across
|
2017-09-10 22:34:56 +02:00
|
|
|
// any suspension points
|
2020-04-10 11:19:24 -07:00
|
|
|
live_locals_at_any_suspension_point.union(&live_locals);
|
2019-05-09 18:07:30 -07:00
|
|
|
|
2020-04-10 11:19:24 -07:00
|
|
|
live_locals_at_suspension_points.push(live_locals);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 11:19:24 -07:00
|
|
|
debug!("live_locals_anywhere = {:?}", live_locals_at_any_suspension_point);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-05-09 18:07:30 -07:00
|
|
|
// Renumber our liveness_map bitsets to include only the locals we are
|
|
|
|
// saving.
|
|
|
|
let live_locals_at_suspension_points = live_locals_at_suspension_points
|
|
|
|
.iter()
|
2020-04-10 11:19:24 -07:00
|
|
|
.map(|live_here| renumber_bitset(&live_here, &live_locals_at_any_suspension_point))
|
2019-05-09 18:07:30 -07:00
|
|
|
.collect();
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-03-29 14:17:53 -07:00
|
|
|
let storage_conflicts = compute_storage_conflicts(
|
|
|
|
body_ref,
|
2020-04-10 11:19:24 -07:00
|
|
|
&live_locals_at_any_suspension_point,
|
2020-03-29 14:17:53 -07:00
|
|
|
always_live_locals.clone(),
|
|
|
|
requires_storage_results,
|
|
|
|
);
|
2019-05-30 14:27:12 -07:00
|
|
|
|
2019-05-22 16:36:36 -07:00
|
|
|
LivenessInfo {
|
2020-04-10 11:19:24 -07:00
|
|
|
live_locals: live_locals_at_any_suspension_point,
|
2019-05-22 16:36:36 -07:00
|
|
|
live_locals_at_suspension_points,
|
|
|
|
storage_conflicts,
|
|
|
|
storage_liveness: storage_liveness_map,
|
|
|
|
}
|
2019-05-30 14:27:12 -07:00
|
|
|
}
|
|
|
|
|
2019-05-31 20:51:07 -07:00
|
|
|
/// Renumbers the items present in `stored_locals` and applies the renumbering
|
|
|
|
/// to 'input`.
|
|
|
|
///
|
|
|
|
/// For example, if `stored_locals = [1, 3, 5]`, this would be renumbered to
|
|
|
|
/// `[0, 1, 2]`. Thus, if `input = [3, 5]` we would return `[1, 2]`.
|
2019-12-22 17:42:04 -05:00
|
|
|
fn renumber_bitset(
|
|
|
|
input: &BitSet<Local>,
|
2020-04-10 11:13:31 -07:00
|
|
|
stored_locals: &BitSet<Local>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) -> BitSet<GeneratorSavedLocal> {
|
2019-05-31 20:51:07 -07:00
|
|
|
assert!(stored_locals.superset(&input), "{:?} not a superset of {:?}", stored_locals, input);
|
|
|
|
let mut out = BitSet::new_empty(stored_locals.count());
|
|
|
|
for (idx, local) in stored_locals.iter().enumerate() {
|
|
|
|
let saved_local = GeneratorSavedLocal::from(idx);
|
|
|
|
if input.contains(local) {
|
|
|
|
out.insert(saved_local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("renumber_bitset({:?}, {:?}) => {:?}", input, stored_locals, out);
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:27:12 -07:00
|
|
|
/// For every saved local, looks for which locals are StorageLive at the same
|
|
|
|
/// time. Generates a bitset for every local of all the other locals that may be
|
|
|
|
/// StorageLive simultaneously with that local. This is used in the layout
|
|
|
|
/// computation; see `GeneratorLayout` for more.
|
|
|
|
fn compute_storage_conflicts(
|
|
|
|
body: &'mir Body<'tcx>,
|
2020-04-10 11:13:31 -07:00
|
|
|
stored_locals: &BitSet<Local>,
|
2020-03-29 14:17:53 -07:00
|
|
|
always_live_locals: storage::AlwaysLiveLocals,
|
2020-02-18 10:35:16 -08:00
|
|
|
requires_storage: dataflow::Results<'tcx, MaybeRequiresStorage<'mir, 'tcx>>,
|
2019-05-31 16:53:27 -07:00
|
|
|
) -> BitMatrix<GeneratorSavedLocal, GeneratorSavedLocal> {
|
|
|
|
assert_eq!(body.local_decls.len(), stored_locals.domain_size());
|
2020-03-29 14:17:53 -07:00
|
|
|
|
2019-05-30 14:27:12 -07:00
|
|
|
debug!("compute_storage_conflicts({:?})", body.span);
|
2020-03-29 14:17:53 -07:00
|
|
|
debug!("always_live = {:?}", always_live_locals);
|
2019-05-30 14:27:12 -07:00
|
|
|
|
2020-03-29 14:17:53 -07:00
|
|
|
// Locals that are always live or ones that need to be stored across
|
|
|
|
// suspension points are not eligible for overlap.
|
|
|
|
let mut ineligible_locals = always_live_locals.into_inner();
|
|
|
|
ineligible_locals.intersect(stored_locals);
|
2019-05-30 14:27:12 -07:00
|
|
|
|
2019-05-31 20:51:07 -07:00
|
|
|
// Compute the storage conflicts for all eligible locals.
|
|
|
|
let mut visitor = StorageConflictVisitor {
|
|
|
|
body,
|
|
|
|
stored_locals: &stored_locals,
|
2019-06-28 18:41:53 -07:00
|
|
|
local_conflicts: BitMatrix::from_row_n(&ineligible_locals, body.local_decls.len()),
|
2019-05-31 20:51:07 -07:00
|
|
|
};
|
2020-02-18 10:31:01 -08:00
|
|
|
|
2020-02-28 20:57:36 -08:00
|
|
|
// Visit only reachable basic blocks. The exact order is not important.
|
|
|
|
let reachable_blocks = traversal::preorder(body).map(|(bb, _)| bb);
|
|
|
|
requires_storage.visit_with(body, reachable_blocks, &mut visitor);
|
2020-02-18 10:31:01 -08:00
|
|
|
|
2019-05-31 20:51:07 -07:00
|
|
|
let local_conflicts = visitor.local_conflicts;
|
2019-05-30 14:27:12 -07:00
|
|
|
|
2019-05-31 20:51:07 -07:00
|
|
|
// Compress the matrix using only stored locals (Local -> GeneratorSavedLocal).
|
|
|
|
//
|
2019-05-30 14:27:12 -07:00
|
|
|
// NOTE: Today we store a full conflict bitset for every local. Technically
|
|
|
|
// this is twice as many bits as we need, since the relation is symmetric.
|
|
|
|
// However, in practice these bitsets are not usually large. The layout code
|
|
|
|
// also needs to keep track of how many conflicts each local has, so it's
|
|
|
|
// simpler to keep it this way for now.
|
2019-05-31 16:53:27 -07:00
|
|
|
let mut storage_conflicts = BitMatrix::new(stored_locals.count(), stored_locals.count());
|
|
|
|
for (idx_a, local_a) in stored_locals.iter().enumerate() {
|
|
|
|
let saved_local_a = GeneratorSavedLocal::new(idx_a);
|
|
|
|
if ineligible_locals.contains(local_a) {
|
|
|
|
// Conflicts with everything.
|
|
|
|
storage_conflicts.insert_all_into_row(saved_local_a);
|
|
|
|
} else {
|
|
|
|
// Keep overlap information only for stored locals.
|
|
|
|
for (idx_b, local_b) in stored_locals.iter().enumerate() {
|
|
|
|
let saved_local_b = GeneratorSavedLocal::new(idx_b);
|
|
|
|
if local_conflicts.contains(local_a, local_b) {
|
|
|
|
storage_conflicts.insert(saved_local_a, saved_local_b);
|
|
|
|
}
|
2019-05-30 14:27:12 -07:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 14:27:12 -07:00
|
|
|
storage_conflicts
|
|
|
|
}
|
|
|
|
|
2020-02-18 10:31:01 -08:00
|
|
|
struct StorageConflictVisitor<'mir, 'tcx, 's> {
|
|
|
|
body: &'mir Body<'tcx>,
|
2020-04-10 11:13:31 -07:00
|
|
|
stored_locals: &'s BitSet<Local>,
|
2019-05-31 20:51:07 -07:00
|
|
|
// FIXME(tmandry): Consider using sparse bitsets here once we have good
|
|
|
|
// benchmarks for generators.
|
|
|
|
local_conflicts: BitMatrix<Local, Local>,
|
|
|
|
}
|
|
|
|
|
2020-02-18 10:31:01 -08:00
|
|
|
impl dataflow::ResultsVisitor<'mir, 'tcx> for StorageConflictVisitor<'mir, 'tcx, '_> {
|
|
|
|
type FlowState = BitSet<Local>;
|
2019-05-31 20:51:07 -07:00
|
|
|
|
2020-04-10 11:13:31 -07:00
|
|
|
fn visit_statement_before_primary_effect(
|
2019-12-22 17:42:04 -05:00
|
|
|
&mut self,
|
2020-02-18 10:31:01 -08:00
|
|
|
state: &Self::FlowState,
|
|
|
|
_statement: &'mir Statement<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
loc: Location,
|
|
|
|
) {
|
2020-02-18 10:31:01 -08:00
|
|
|
self.apply_state(state, loc);
|
2019-05-31 20:51:07 -07:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:13:31 -07:00
|
|
|
fn visit_terminator_before_primary_effect(
|
2019-12-22 17:42:04 -05:00
|
|
|
&mut self,
|
2020-02-18 10:31:01 -08:00
|
|
|
state: &Self::FlowState,
|
|
|
|
_terminator: &'mir Terminator<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
loc: Location,
|
|
|
|
) {
|
2020-02-18 10:31:01 -08:00
|
|
|
self.apply_state(state, loc);
|
2019-05-31 20:51:07 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'body, 'tcx, 's> StorageConflictVisitor<'body, 'tcx, 's> {
|
2020-02-18 10:31:01 -08:00
|
|
|
fn apply_state(&mut self, flow_state: &BitSet<Local>, loc: Location) {
|
2019-05-31 20:51:07 -07:00
|
|
|
// Ignore unreachable blocks.
|
2020-03-22 13:36:56 +01:00
|
|
|
if self.body.basic_blocks()[loc.block].terminator().kind == TerminatorKind::Unreachable {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-02-18 10:31:01 -08:00
|
|
|
let mut eligible_storage_live = flow_state.clone();
|
2019-05-31 20:51:07 -07:00
|
|
|
eligible_storage_live.intersect(&self.stored_locals);
|
|
|
|
|
|
|
|
for local in eligible_storage_live.iter() {
|
|
|
|
self.local_conflicts.union_row_with(&eligible_storage_live, local);
|
|
|
|
}
|
|
|
|
|
|
|
|
if eligible_storage_live.count() > 1 {
|
|
|
|
trace!("at {:?}, eligible_storage_live={:?}", loc, eligible_storage_live);
|
2019-05-30 14:27:12 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
fn compute_layout<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
source: MirSource<'tcx>,
|
|
|
|
upvars: &Vec<Ty<'tcx>>,
|
|
|
|
interior: Ty<'tcx>,
|
2020-03-29 14:17:53 -07:00
|
|
|
always_live_locals: &storage::AlwaysLiveLocals,
|
2019-06-12 00:11:55 +03:00
|
|
|
movable: bool,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
) -> (
|
|
|
|
FxHashMap<Local, (Ty<'tcx>, VariantIdx, usize)>,
|
|
|
|
GeneratorLayout<'tcx>,
|
2020-04-10 11:13:31 -07:00
|
|
|
IndexVec<BasicBlock, Option<BitSet<Local>>>,
|
2019-06-12 00:11:55 +03:00
|
|
|
) {
|
2017-08-11 06:20:28 +02:00
|
|
|
// Use a liveness analysis to compute locals which are live across a suspension point
|
2019-05-22 16:36:36 -07:00
|
|
|
let LivenessInfo {
|
2019-12-22 17:42:04 -05:00
|
|
|
live_locals,
|
|
|
|
live_locals_at_suspension_points,
|
|
|
|
storage_conflicts,
|
|
|
|
storage_liveness,
|
2020-04-12 10:31:00 -07:00
|
|
|
} = locals_live_across_suspend_points(tcx, body, source, always_live_locals, movable);
|
2019-04-02 16:04:51 -07:00
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Erase regions from the types passed in from typeck so we can compare them with
|
|
|
|
// MIR types
|
2018-05-16 15:48:11 +03:00
|
|
|
let allowed_upvars = tcx.erase_regions(upvars);
|
2019-09-16 19:08:35 +01:00
|
|
|
let allowed = match interior.kind {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::GeneratorWitness(s) => tcx.erase_late_bound_regions(&s),
|
2017-10-07 16:36:28 +02:00
|
|
|
_ => bug!(),
|
|
|
|
};
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2020-04-09 16:48:36 +02:00
|
|
|
let param_env = tcx.param_env(source.def_id());
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
for (local, decl) in body.local_decls.iter_enumerated() {
|
2017-08-11 06:20:28 +02:00
|
|
|
// Ignore locals which are internal or not live
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
if !live_locals.contains(local) || decl.internal {
|
2016-12-26 14:34:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-09 16:48:36 +02:00
|
|
|
let decl_ty = tcx.normalize_erasing_regions(param_env, decl.ty);
|
2017-08-11 06:20:28 +02:00
|
|
|
|
|
|
|
// Sanity check that typeck knows about the type of locals which are
|
|
|
|
// live across a suspension point
|
2020-04-09 16:48:36 +02:00
|
|
|
if !allowed.contains(&decl_ty) && !allowed_upvars.contains(&decl_ty) {
|
2019-12-22 17:42:04 -05:00
|
|
|
span_bug!(
|
|
|
|
body.span,
|
|
|
|
"Broken MIR: generator contains type {} in MIR, \
|
2017-08-09 13:56:19 -07:00
|
|
|
but typeck only knows about {}",
|
2019-12-22 17:42:04 -05:00
|
|
|
decl.ty,
|
|
|
|
interior
|
|
|
|
);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
// Gather live local types and their indices.
|
2019-05-09 18:07:30 -07:00
|
|
|
let mut locals = IndexVec::<GeneratorSavedLocal, _>::new();
|
|
|
|
let mut tys = IndexVec::<GeneratorSavedLocal, _>::new();
|
|
|
|
for (idx, local) in live_locals.iter().enumerate() {
|
|
|
|
locals.push(local);
|
2019-11-06 12:00:46 -05:00
|
|
|
tys.push(body.local_decls[local].ty);
|
2019-05-09 18:07:30 -07:00
|
|
|
debug!("generator saved local {:?} => {:?}", GeneratorSavedLocal::from(idx), local);
|
|
|
|
}
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2019-05-09 18:07:30 -07:00
|
|
|
// Leave empty variants for the UNRESUMED, RETURNED, and POISONED states.
|
|
|
|
const RESERVED_VARIANTS: usize = 3;
|
2019-04-02 16:04:51 -07:00
|
|
|
|
2019-05-09 18:07:30 -07:00
|
|
|
// Build the generator variant field list.
|
2017-08-11 06:20:28 +02:00
|
|
|
// Create a map from local indices to generator struct indices.
|
2019-05-09 18:07:30 -07:00
|
|
|
let mut variant_fields: IndexVec<VariantIdx, IndexVec<Field, GeneratorSavedLocal>> =
|
|
|
|
iter::repeat(IndexVec::new()).take(RESERVED_VARIANTS).collect();
|
2019-05-03 16:03:05 -07:00
|
|
|
let mut remap = FxHashMap::default();
|
2019-05-09 18:07:30 -07:00
|
|
|
for (suspension_point_idx, live_locals) in live_locals_at_suspension_points.iter().enumerate() {
|
|
|
|
let variant_index = VariantIdx::from(RESERVED_VARIANTS + suspension_point_idx);
|
|
|
|
let mut fields = IndexVec::new();
|
|
|
|
for (idx, saved_local) in live_locals.iter().enumerate() {
|
|
|
|
fields.push(saved_local);
|
2019-05-22 16:36:36 -07:00
|
|
|
// Note that if a field is included in multiple variants, we will
|
|
|
|
// just use the first one here. That's fine; fields do not move
|
|
|
|
// around inside generators, so it doesn't matter which variant
|
|
|
|
// index we access them by.
|
|
|
|
remap.entry(locals[saved_local]).or_insert((tys[saved_local], variant_index, idx));
|
2019-05-09 18:07:30 -07:00
|
|
|
}
|
|
|
|
variant_fields.push(fields);
|
2019-05-03 16:03:05 -07:00
|
|
|
}
|
2019-05-09 18:07:30 -07:00
|
|
|
debug!("generator variant_fields = {:?}", variant_fields);
|
2019-05-31 16:53:27 -07:00
|
|
|
debug!("generator storage_conflicts = {:#?}", storage_conflicts);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let layout = GeneratorLayout { field_tys: tys, variant_fields, storage_conflicts };
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2017-09-10 22:34:56 +02:00
|
|
|
(remap, layout, storage_liveness)
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 22:46:06 +01:00
|
|
|
/// Replaces the entry point of `body` with a block that switches on the generator discriminant and
|
|
|
|
/// dispatches to blocks according to `cases`.
|
|
|
|
///
|
|
|
|
/// After this function, the former entry point of the function will be bb1.
|
2019-06-12 00:11:55 +03:00
|
|
|
fn insert_switch<'tcx>(
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
cases: Vec<(usize, BasicBlock)>,
|
|
|
|
transform: &TransformVisitor<'tcx>,
|
|
|
|
default: TerminatorKind<'tcx>,
|
|
|
|
) {
|
2019-11-06 12:00:46 -05:00
|
|
|
let default_block = insert_term_block(body, default);
|
|
|
|
let (assign, discr) = transform.get_discr(body);
|
2017-09-10 22:34:56 +02:00
|
|
|
let switch = TerminatorKind::SwitchInt {
|
2019-04-02 16:04:51 -07:00
|
|
|
discr: Operand::Move(discr),
|
|
|
|
switch_ty: transform.discr_ty,
|
|
|
|
values: Cow::from(cases.iter().map(|&(i, _)| i as u128).collect::<Vec<_>>()),
|
|
|
|
targets: cases.iter().map(|&(_, d)| d).chain(iter::once(default_block)).collect(),
|
2017-09-10 22:34:56 +02:00
|
|
|
};
|
|
|
|
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2019-12-22 17:42:04 -05:00
|
|
|
body.basic_blocks_mut().raw.insert(
|
|
|
|
0,
|
|
|
|
BasicBlockData {
|
|
|
|
statements: vec![assign],
|
|
|
|
terminator: Some(Terminator { source_info, kind: switch }),
|
|
|
|
is_cleanup: false,
|
|
|
|
},
|
|
|
|
);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let blocks = body.basic_blocks_mut().iter_mut();
|
2019-10-04 00:55:28 -04:00
|
|
|
|
|
|
|
for target in blocks.flat_map(|b| b.terminator_mut().successors_mut()) {
|
|
|
|
*target = BasicBlock::new(target.index() + 1);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn elaborate_generator_drops<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, body: &mut Body<'tcx>) {
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::shim::DropShimElaborator;
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::util::elaborate_drops::{elaborate_drop, Unwind};
|
|
|
|
use crate::util::patch::MirPatch;
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-25 07:38:57 -07:00
|
|
|
// Note that `elaborate_drops` only drops the upvars of a generator, and
|
|
|
|
// this is ok because `open_drop` can only be reached within that own
|
|
|
|
// generator's resume function.
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut elaborator = DropShimElaborator { body, patch: MirPatch::new(body), tcx, param_env };
|
2019-03-03 19:27:41 +00:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
for (block, block_data) in body.basic_blocks().iter_enumerated() {
|
2019-03-03 19:27:41 +00:00
|
|
|
let (target, unwind, source_info) = match block_data.terminator() {
|
2019-12-22 17:42:04 -05:00
|
|
|
Terminator { source_info, kind: TerminatorKind::Drop { location, target, unwind } } => {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(local) = location.as_local() {
|
2020-03-10 13:51:07 -07:00
|
|
|
if local == SELF_ARG {
|
2019-10-20 16:09:36 -04:00
|
|
|
(target, unwind, source_info)
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
_ => continue,
|
|
|
|
};
|
2019-03-03 19:27:41 +00:00
|
|
|
let unwind = if block_data.is_cleanup {
|
2016-12-26 14:34:03 +01:00
|
|
|
Unwind::InCleanup
|
2019-03-03 19:27:41 +00:00
|
|
|
} else {
|
|
|
|
Unwind::To(unwind.unwrap_or_else(|| elaborator.patch.resume_block()))
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
2019-03-03 19:27:41 +00:00
|
|
|
elaborate_drop(
|
|
|
|
&mut elaborator,
|
2019-10-20 16:09:36 -04:00
|
|
|
*source_info,
|
2020-03-30 20:25:43 -03:00
|
|
|
Place::from(SELF_ARG),
|
2019-03-03 19:27:41 +00:00
|
|
|
(),
|
2019-10-20 16:09:36 -04:00
|
|
|
*target,
|
2019-03-03 19:27:41 +00:00
|
|
|
unwind,
|
|
|
|
block,
|
|
|
|
);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
2019-11-06 12:00:46 -05:00
|
|
|
elaborator.patch.apply(body);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
fn create_generator_drop_shim<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
transform: &TransformVisitor<'tcx>,
|
|
|
|
source: MirSource<'tcx>,
|
|
|
|
gen_ty: Ty<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
drop_clean: BasicBlock,
|
2020-04-12 10:31:00 -07:00
|
|
|
) -> Body<'tcx> {
|
2019-11-06 12:00:46 -05:00
|
|
|
let mut body = body.clone();
|
2020-01-25 20:03:10 +01:00
|
|
|
body.arg_count = 1; // make sure the resume argument is not included here
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-01-27 22:32:57 +01:00
|
|
|
let mut cases = create_cases(&mut body, transform, Operation::Drop);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-03-03 19:28:05 +00:00
|
|
|
cases.insert(0, (UNRESUMED, drop_clean));
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2019-03-03 19:28:05 +00:00
|
|
|
// The returned state and the poisoned state fall through to the default
|
|
|
|
// case which is just to return
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
insert_switch(&mut body, cases, &transform, TerminatorKind::Return);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
for block in body.basic_blocks_mut() {
|
2019-10-04 00:55:28 -04:00
|
|
|
let kind = &mut block.terminator_mut().kind;
|
2016-12-26 14:34:03 +01:00
|
|
|
if let TerminatorKind::GeneratorDrop = *kind {
|
|
|
|
*kind = TerminatorKind::Return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the return variable
|
2019-11-06 12:00:46 -05:00
|
|
|
body.local_decls[RETURN_PLACE] = LocalDecl {
|
2016-12-26 14:34:03 +01:00
|
|
|
mutability: Mutability::Mut,
|
2018-09-10 11:07:13 +09:00
|
|
|
ty: tcx.mk_unit(),
|
2018-10-22 14:23:44 +02:00
|
|
|
user_ty: UserTypeProjections::none(),
|
2018-05-29 21:31:33 +03:00
|
|
|
source_info,
|
2017-07-15 22:41:33 +02:00
|
|
|
internal: false,
|
2018-09-22 00:51:48 +02:00
|
|
|
is_block_tail: None,
|
2019-12-22 17:42:04 -05:00
|
|
|
local_info: LocalInfo::Other,
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
|
|
|
|
2020-03-19 11:40:38 +00:00
|
|
|
make_generator_state_argument_indirect(tcx, &mut body);
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
// Change the generator argument from &mut to *mut
|
2020-03-10 13:51:07 -07:00
|
|
|
body.local_decls[SELF_ARG] = LocalDecl {
|
2016-12-26 14:34:03 +01:00
|
|
|
mutability: Mutability::Mut,
|
2019-12-22 17:42:04 -05:00
|
|
|
ty: tcx.mk_ptr(ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut }),
|
2018-10-22 14:23:44 +02:00
|
|
|
user_ty: UserTypeProjections::none(),
|
2018-05-29 21:31:33 +03:00
|
|
|
source_info,
|
2017-07-15 22:41:33 +02:00
|
|
|
internal: false,
|
2018-09-22 00:51:48 +02:00
|
|
|
is_block_tail: None,
|
2019-12-22 17:42:04 -05:00
|
|
|
local_info: LocalInfo::Other,
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
2018-11-07 12:21:28 +01:00
|
|
|
if tcx.sess.opts.debugging_opts.mir_emit_retag {
|
|
|
|
// Alias tracking must know we changed the type
|
2019-12-22 17:42:04 -05:00
|
|
|
body.basic_blocks_mut()[START_BLOCK].statements.insert(
|
|
|
|
0,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
2020-03-10 13:51:07 -07:00
|
|
|
kind: StatementKind::Retag(RetagKind::Raw, box Place::from(SELF_ARG)),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
)
|
2018-11-07 12:21:28 +01:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
no_landing_pads(tcx, &mut body);
|
2017-08-12 07:06:34 +02:00
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
// Make sure we remove dead blocks to remove
|
|
|
|
// unrelated code from the resume part of the function
|
2019-11-06 12:00:46 -05:00
|
|
|
simplify::remove_dead_blocks(&mut body);
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2020-03-07 00:19:25 +01:00
|
|
|
dump_mir(tcx, None, "generator_drop", &0, source, &body, |_, _| Ok(()));
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
body
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn insert_term_block<'tcx>(body: &mut Body<'tcx>, kind: TerminatorKind<'tcx>) -> BasicBlock {
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2019-11-06 12:00:46 -05:00
|
|
|
body.basic_blocks_mut().push(BasicBlockData {
|
2017-09-10 22:34:56 +02:00
|
|
|
statements: Vec::new(),
|
2019-12-22 17:42:04 -05:00
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
2017-09-10 22:34:56 +02:00
|
|
|
is_cleanup: false,
|
2020-04-25 13:46:37 +02:00
|
|
|
})
|
2017-09-10 22:34:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
fn insert_panic_block<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
message: AssertMessage<'tcx>,
|
|
|
|
) -> BasicBlock {
|
2019-11-06 12:00:46 -05:00
|
|
|
let assert_block = BasicBlock::new(body.basic_blocks().len());
|
2016-12-26 14:34:03 +01:00
|
|
|
let term = TerminatorKind::Assert {
|
|
|
|
cond: Operand::Constant(box Constant {
|
2019-11-06 12:00:46 -05:00
|
|
|
span: body.span,
|
2018-08-09 06:18:00 -04:00
|
|
|
user_ty: None,
|
2019-04-03 15:29:31 +02:00
|
|
|
literal: ty::Const::from_bool(tcx, false),
|
2016-12-26 14:34:03 +01:00
|
|
|
}),
|
|
|
|
expected: true,
|
2017-09-10 22:34:56 +02:00
|
|
|
msg: message,
|
2017-07-10 20:04:15 +02:00
|
|
|
target: assert_block,
|
2017-08-11 04:36:39 +02:00
|
|
|
cleanup: None,
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
|
|
|
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2019-11-06 12:00:46 -05:00
|
|
|
body.basic_blocks_mut().push(BasicBlockData {
|
2016-12-26 14:34:03 +01:00
|
|
|
statements: Vec::new(),
|
2019-12-22 17:42:04 -05:00
|
|
|
terminator: Some(Terminator { source_info, kind: term }),
|
2016-12-26 14:34:03 +01:00
|
|
|
is_cleanup: false,
|
|
|
|
});
|
2017-09-10 22:34:56 +02:00
|
|
|
|
|
|
|
assert_block
|
2017-07-10 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 21:29:09 +01:00
|
|
|
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
|
|
|
|
// Returning from a function with an uninhabited return type is undefined behavior.
|
|
|
|
if body.return_ty().conservative_is_privately_uninhabited(tcx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:51:26 +01:00
|
|
|
// If there's a return terminator the function may return.
|
2020-03-07 21:29:09 +01:00
|
|
|
for block in body.basic_blocks() {
|
|
|
|
if let TerminatorKind::Return = block.terminator().kind {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:51:26 +01:00
|
|
|
// Otherwise the function can't return.
|
2020-03-07 21:29:09 +01:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-03-07 21:51:34 +01:00
|
|
|
fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
|
|
|
|
// Nothing can unwind when landing pads are off.
|
2020-03-31 23:15:39 +01:00
|
|
|
if tcx.sess.panic_strategy() == PanicStrategy::Abort {
|
2020-03-07 21:51:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwinds can only start at certain terminators.
|
|
|
|
for block in body.basic_blocks() {
|
|
|
|
match block.terminator().kind {
|
|
|
|
// These never unwind.
|
|
|
|
TerminatorKind::Goto { .. }
|
|
|
|
| TerminatorKind::SwitchInt { .. }
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
| TerminatorKind::Return
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::GeneratorDrop
|
|
|
|
| TerminatorKind::FalseEdges { .. }
|
|
|
|
| TerminatorKind::FalseUnwind { .. } => {}
|
|
|
|
|
|
|
|
// Resume will *continue* unwinding, but if there's no other unwinding terminator it
|
|
|
|
// will never be reached.
|
|
|
|
TerminatorKind::Resume => {}
|
|
|
|
|
|
|
|
TerminatorKind::Yield { .. } => {
|
|
|
|
unreachable!("`can_unwind` called before generator transform")
|
|
|
|
}
|
|
|
|
|
|
|
|
// These may unwind.
|
|
|
|
TerminatorKind::Drop { .. }
|
|
|
|
| TerminatorKind::DropAndReplace { .. }
|
|
|
|
| TerminatorKind::Call { .. }
|
|
|
|
| TerminatorKind::Assert { .. } => return true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find an unwinding terminator, the function cannot unwind.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
fn create_generator_resume_function<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
transform: TransformVisitor<'tcx>,
|
|
|
|
source: MirSource<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2020-03-07 21:29:09 +01:00
|
|
|
can_return: bool,
|
2019-06-12 00:11:55 +03:00
|
|
|
) {
|
2020-03-08 00:55:29 +01:00
|
|
|
let can_unwind = can_unwind(tcx, body);
|
|
|
|
|
2017-07-10 20:04:15 +02:00
|
|
|
// Poison the generator when it unwinds
|
2020-03-08 00:55:29 +01:00
|
|
|
if can_unwind {
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2020-04-25 13:46:37 +02:00
|
|
|
let poison_block = body.basic_blocks_mut().push(BasicBlockData {
|
2020-03-08 00:55:29 +01:00
|
|
|
statements: vec![transform.set_discr(VariantIdx::new(POISONED), source_info)],
|
|
|
|
terminator: Some(Terminator { source_info, kind: TerminatorKind::Resume }),
|
|
|
|
is_cleanup: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (idx, block) in body.basic_blocks_mut().iter_enumerated_mut() {
|
|
|
|
let source_info = block.terminator().source_info;
|
|
|
|
|
|
|
|
if let TerminatorKind::Resume = block.terminator().kind {
|
|
|
|
// An existing `Resume` terminator is redirected to jump to our dedicated
|
|
|
|
// "poisoning block" above.
|
|
|
|
if idx != poison_block {
|
|
|
|
*block.terminator_mut() = Terminator {
|
|
|
|
source_info,
|
|
|
|
kind: TerminatorKind::Goto { target: poison_block },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if !block.is_cleanup {
|
|
|
|
// Any terminators that *can* unwind but don't have an unwind target set are also
|
|
|
|
// pointed at our poisoning block (unless they're part of the cleanup path).
|
|
|
|
if let Some(unwind @ None) = block.terminator_mut().unwind_mut() {
|
|
|
|
*unwind = Some(poison_block);
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 20:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:32:57 +01:00
|
|
|
let mut cases = create_cases(body, &transform, Operation::Resume);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::AssertKind::{ResumedAfterPanic, ResumedAfterReturn};
|
2018-04-27 15:21:31 +02:00
|
|
|
|
2019-03-03 19:28:05 +00:00
|
|
|
// Jump to the entry point on the unresumed
|
|
|
|
cases.insert(0, (UNRESUMED, BasicBlock::new(0)));
|
2019-11-25 12:58:40 +00:00
|
|
|
|
|
|
|
// Panic when resumed on the returned or poisoned state
|
2019-11-26 12:45:19 +00:00
|
|
|
let generator_kind = body.generator_kind.unwrap();
|
2020-03-07 21:29:09 +01:00
|
|
|
|
2020-03-14 15:46:57 +01:00
|
|
|
if can_unwind {
|
2020-03-07 21:29:09 +01:00
|
|
|
cases.insert(
|
|
|
|
1,
|
2020-03-14 15:46:57 +01:00
|
|
|
(POISONED, insert_panic_block(tcx, body, ResumedAfterPanic(generator_kind))),
|
2020-03-07 21:29:09 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-14 15:46:57 +01:00
|
|
|
if can_return {
|
2020-03-07 21:51:34 +01:00
|
|
|
cases.insert(
|
2020-03-08 01:23:00 +01:00
|
|
|
1,
|
2020-03-14 15:46:57 +01:00
|
|
|
(RETURNED, insert_panic_block(tcx, body, ResumedAfterReturn(generator_kind))),
|
2020-03-07 21:51:34 +01:00
|
|
|
);
|
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
insert_switch(body, cases, &transform, TerminatorKind::Unreachable);
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2020-03-19 11:40:38 +00:00
|
|
|
make_generator_state_argument_indirect(tcx, body);
|
2019-11-06 12:00:46 -05:00
|
|
|
make_generator_state_argument_pinned(tcx, body);
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
no_landing_pads(tcx, body);
|
2017-08-12 07:06:34 +02:00
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
// Make sure we remove dead blocks to remove
|
|
|
|
// unrelated code from the drop part of the function
|
2019-11-06 12:00:46 -05:00
|
|
|
simplify::remove_dead_blocks(body);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
dump_mir(tcx, None, "generator_resume", &0, source, body, |_, _| Ok(()));
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
|
2019-11-06 12:00:46 -05:00
|
|
|
let return_block = insert_term_block(body, TerminatorKind::Return);
|
2017-07-10 20:04:15 +02:00
|
|
|
|
|
|
|
let term = TerminatorKind::Drop {
|
2020-03-10 13:51:07 -07:00
|
|
|
location: Place::from(SELF_ARG),
|
2017-07-10 20:04:15 +02:00
|
|
|
target: return_block,
|
|
|
|
unwind: None,
|
|
|
|
};
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2020-04-25 13:46:37 +02:00
|
|
|
|
|
|
|
// Create a block to destroy an unresumed generators. This can only destroy upvars.
|
2019-11-06 12:00:46 -05:00
|
|
|
body.basic_blocks_mut().push(BasicBlockData {
|
2017-07-10 20:04:15 +02:00
|
|
|
statements: Vec::new(),
|
2019-12-22 17:42:04 -05:00
|
|
|
terminator: Some(Terminator { source_info, kind: term }),
|
2017-07-10 20:04:15 +02:00
|
|
|
is_cleanup: false,
|
2020-04-25 13:46:37 +02:00
|
|
|
})
|
2017-07-10 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 22:32:57 +01:00
|
|
|
/// An operation that can be performed on a generator.
|
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
enum Operation {
|
|
|
|
Resume,
|
|
|
|
Drop,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Operation {
|
|
|
|
fn target_block(self, point: &SuspensionPoint<'_>) -> Option<BasicBlock> {
|
|
|
|
match self {
|
|
|
|
Operation::Resume => Some(point.resume),
|
|
|
|
Operation::Drop => point.drop,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_cases<'tcx>(
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
transform: &TransformVisitor<'tcx>,
|
2020-01-27 22:32:57 +01:00
|
|
|
operation: Operation,
|
|
|
|
) -> Vec<(usize, BasicBlock)> {
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2017-09-11 18:11:21 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
transform
|
|
|
|
.suspension_points
|
|
|
|
.iter()
|
|
|
|
.filter_map(|point| {
|
|
|
|
// Find the target for this suspension point, if applicable
|
2020-01-27 22:32:57 +01:00
|
|
|
operation.target_block(point).map(|target| {
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut statements = Vec::new();
|
|
|
|
|
|
|
|
// Create StorageLive instructions for locals with live storage
|
|
|
|
for i in 0..(body.local_decls.len()) {
|
2020-02-02 22:46:06 +01:00
|
|
|
if i == 2 {
|
|
|
|
// The resume argument is live on function entry. Don't insert a
|
|
|
|
// `StorageLive`, or the following `Assign` will read from uninitialized
|
|
|
|
// memory.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let l = Local::new(i);
|
2020-03-29 14:17:53 -07:00
|
|
|
let needs_storage_live = point.storage_liveness.contains(l)
|
|
|
|
&& !transform.remap.contains_key(&l)
|
|
|
|
&& !transform.always_live_locals.contains(l);
|
|
|
|
if needs_storage_live {
|
2019-12-22 17:42:04 -05:00
|
|
|
statements
|
|
|
|
.push(Statement { source_info, kind: StatementKind::StorageLive(l) });
|
|
|
|
}
|
2017-09-11 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 22:32:57 +01:00
|
|
|
if operation == Operation::Resume {
|
|
|
|
// Move the resume argument to the destination place of the `Yield` terminator
|
|
|
|
let resume_arg = Local::new(2); // 0 = return, 1 = self
|
|
|
|
statements.push(Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::Assign(box (
|
|
|
|
point.resume_arg,
|
|
|
|
Rvalue::Use(Operand::Move(resume_arg.into())),
|
|
|
|
)),
|
|
|
|
});
|
|
|
|
}
|
2020-01-25 02:33:52 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
// Then jump to the real target
|
2020-04-25 13:46:37 +02:00
|
|
|
let block = body.basic_blocks_mut().push(BasicBlockData {
|
2019-12-22 17:42:04 -05:00
|
|
|
statements,
|
|
|
|
terminator: Some(Terminator {
|
|
|
|
source_info,
|
|
|
|
kind: TerminatorKind::Goto { target },
|
|
|
|
}),
|
|
|
|
is_cleanup: false,
|
|
|
|
});
|
2017-09-11 18:11:21 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
(point.state, block)
|
|
|
|
})
|
2017-09-11 18:11:21 +02:00
|
|
|
})
|
2019-12-22 17:42:04 -05:00
|
|
|
.collect()
|
2017-09-11 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 16:20:00 -04:00
|
|
|
impl<'tcx> MirPass<'tcx> for StateTransform {
|
2020-04-12 10:31:00 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
|
2019-11-06 12:00:46 -05:00
|
|
|
let yield_ty = if let Some(yield_ty) = body.yield_ty {
|
2017-07-10 21:11:31 +02:00
|
|
|
yield_ty
|
2016-12-26 14:34:03 +01:00
|
|
|
} else {
|
|
|
|
// This only applies to generators
|
2019-12-22 17:42:04 -05:00
|
|
|
return;
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
assert!(body.generator_drop.is_none());
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-02-03 11:51:07 +01:00
|
|
|
let def_id = source.def_id();
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// The first argument is the generator type passed by value
|
2019-11-06 12:00:46 -05:00
|
|
|
let gen_ty = body.local_decls.raw[1].ty;
|
2017-08-11 06:20:28 +02:00
|
|
|
|
2017-10-07 16:36:28 +02:00
|
|
|
// Get the interior types and substs which typeck computed
|
2019-09-16 19:08:35 +01:00
|
|
|
let (upvars, interior, discr_ty, movable) = match gen_ty.kind {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Generator(_, substs, movability) => {
|
2019-10-03 21:51:30 +08:00
|
|
|
let substs = substs.as_generator();
|
2019-12-22 17:42:04 -05:00
|
|
|
(
|
2020-03-13 03:23:38 +02:00
|
|
|
substs.upvar_tys().collect(),
|
|
|
|
substs.witness(),
|
2019-12-22 17:42:04 -05:00
|
|
|
substs.discr_ty(tcx),
|
|
|
|
movability == hir::Movability::Movable,
|
|
|
|
)
|
2017-10-07 16:36:28 +02:00
|
|
|
}
|
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Compute GeneratorState<yield_ty, return_ty>
|
2017-08-31 08:57:41 -07:00
|
|
|
let state_did = tcx.lang_items().gen_state().unwrap();
|
2016-12-26 14:34:03 +01:00
|
|
|
let state_adt_ref = tcx.adt_def(state_did);
|
2019-12-22 17:42:04 -05:00
|
|
|
let state_substs = tcx.intern_substs(&[yield_ty.into(), body.return_ty().into()]);
|
2016-12-26 14:34:03 +01:00
|
|
|
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// We rename RETURN_PLACE which has type mir.return_ty to new_ret_local
|
|
|
|
// RETURN_PLACE then is a fresh unused local with type ret_ty.
|
2020-02-02 22:46:06 +01:00
|
|
|
let new_ret_local = replace_local(RETURN_PLACE, ret_ty, body, tcx);
|
|
|
|
|
|
|
|
// We also replace the resume argument and insert an `Assign`.
|
2020-02-06 12:34:31 +01:00
|
|
|
// This is needed because the resume argument `_2` might be live across a `yield`, in which
|
|
|
|
// case there is no `Assign` to it that the transform can turn into a store to the generator
|
|
|
|
// state. After the yield the slot in the generator state would then be uninitialized.
|
2020-02-02 22:46:06 +01:00
|
|
|
let resume_local = Local::new(2);
|
|
|
|
let new_resume_local =
|
|
|
|
replace_local(resume_local, body.local_decls[resume_local].ty, body, tcx);
|
|
|
|
|
|
|
|
// When first entering the generator, move the resume argument into its new local.
|
2020-05-06 10:30:11 +10:00
|
|
|
let source_info = SourceInfo::outermost(body.span);
|
2020-02-02 22:46:06 +01:00
|
|
|
let stmts = &mut body.basic_blocks_mut()[BasicBlock::new(0)].statements;
|
|
|
|
stmts.insert(
|
|
|
|
0,
|
|
|
|
Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::Assign(box (
|
|
|
|
new_resume_local.into(),
|
|
|
|
Rvalue::Use(Operand::Move(resume_local.into())),
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-03-29 14:17:53 -07:00
|
|
|
let always_live_locals = storage::AlwaysLiveLocals::new(&body);
|
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Extract locals which are live across suspension point into `layout`
|
|
|
|
// `remap` gives a mapping from local indices onto generator struct indices
|
2017-09-10 22:34:56 +02:00
|
|
|
// `storage_liveness` tells us which locals have live storage at suspension points
|
2019-12-22 17:42:04 -05:00
|
|
|
let (remap, layout, storage_liveness) =
|
2020-03-29 14:17:53 -07:00
|
|
|
compute_layout(tcx, source, &upvars, interior, &always_live_locals, movable, body);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-03-07 21:29:09 +01:00
|
|
|
let can_return = can_return(tcx, body);
|
|
|
|
|
2017-12-01 14:31:47 +02:00
|
|
|
// Run the transformation which converts Places from Local to generator struct
|
2017-08-11 06:20:28 +02:00
|
|
|
// accesses for locals in `remap`.
|
|
|
|
// It also rewrites `return x` and `yield y` as writing a new generator state and returning
|
|
|
|
// GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
|
2016-12-26 14:34:03 +01:00
|
|
|
let mut transform = TransformVisitor {
|
|
|
|
tcx,
|
|
|
|
state_adt_ref,
|
|
|
|
state_substs,
|
|
|
|
remap,
|
2017-09-10 22:34:56 +02:00
|
|
|
storage_liveness,
|
2020-03-29 14:17:53 -07:00
|
|
|
always_live_locals,
|
2017-09-10 22:34:56 +02:00
|
|
|
suspension_points: Vec::new(),
|
2016-12-26 14:34:03 +01:00
|
|
|
new_ret_local,
|
2019-04-08 17:44:00 -07:00
|
|
|
discr_ty,
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
2019-11-06 12:00:46 -05:00
|
|
|
transform.visit_body(body);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-01-25 02:33:52 +01:00
|
|
|
// Update our MIR struct to reflect the changes we've made
|
2019-11-06 12:00:46 -05:00
|
|
|
body.yield_ty = None;
|
2020-01-25 20:03:10 +01:00
|
|
|
body.arg_count = 2; // self, resume arg
|
2019-11-06 12:00:46 -05:00
|
|
|
body.spread_arg = None;
|
|
|
|
body.generator_layout = Some(layout);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Insert `drop(generator_struct)` which is used to drop upvars for generators in
|
2019-03-03 19:28:05 +00:00
|
|
|
// the unresumed state.
|
2017-08-11 06:20:28 +02:00
|
|
|
// This is expanded to a drop ladder in `elaborate_generator_drops`.
|
2019-11-06 12:00:46 -05:00
|
|
|
let drop_clean = insert_clean_drop(body);
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
dump_mir(tcx, None, "generator_pre-elab", &0, source, body, |_, _| Ok(()));
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Expand `drop(generator_struct)` to a drop ladder which destroys upvars.
|
|
|
|
// If any upvars are moved out of, drop elaboration will handle upvar destruction.
|
|
|
|
// However we need to also elaborate the code generated by `insert_clean_drop`.
|
2019-11-06 12:00:46 -05:00
|
|
|
elaborate_generator_drops(tcx, def_id, body);
|
2017-07-10 20:04:15 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
dump_mir(tcx, None, "generator_post-transform", &0, source, body, |_, _| Ok(()));
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-11 06:20:28 +02:00
|
|
|
// Create a copy of our MIR and use it to create the drop shim for the generator
|
2019-12-22 17:42:04 -05:00
|
|
|
let drop_shim =
|
2020-03-19 11:40:38 +00:00
|
|
|
create_generator_drop_shim(tcx, &transform, source, gen_ty, body, drop_clean);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
body.generator_drop = Some(box drop_shim);
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-08-11 06:23:22 +02:00
|
|
|
// Create the Generator::resume function
|
2020-03-19 11:40:38 +00:00
|
|
|
create_generator_resume_function(tcx, transform, source, body, can_return);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
}
|