E0657, // `impl Trait` can only capture lifetimes bound at the fn level
E0687, // in-band lifetimes cannot be used in `fn`/`Fn` syntax
E0688, // in-band lifetimes cannot be mixed with explicit lifetime binders
+
+ E0906, // closures cannot be static
}
arms.iter().map(|x| self.lower_arm(x)).collect(),
hir::MatchSource::Normal)
}
- ExprKind::Closure(capture_clause, ref decl, ref body, fn_decl_span) => {
+ ExprKind::Closure(capture_clause, movability, ref decl, ref body, fn_decl_span) => {
self.with_new_scopes(|this| {
this.with_parent_def(e.id, |this| {
let mut is_generator = false;
is_generator = this.is_generator;
e
});
- if is_generator && !decl.inputs.is_empty() {
- span_err!(this.sess, fn_decl_span, E0628,
- "generators cannot have explicit arguments");
- this.sess.abort_if_errors();
- }
+ let generator_option = if is_generator {
+ if !decl.inputs.is_empty() {
+ span_err!(this.sess, fn_decl_span, E0628,
+ "generators cannot have explicit arguments");
+ this.sess.abort_if_errors();
+ }
+ Some(match movability {
+ Movability::Movable => hir::GeneratorMovability::Movable,
+ Movability::Static => hir::GeneratorMovability::Static,
+ })
+ } else {
+ if movability == Movability::Static {
+ span_err!(this.sess, fn_decl_span, E0906,
+ "closures cannot be static");
+ }
+ None
+ };
hir::ExprClosure(this.lower_capture_clause(capture_clause),
this.lower_fn_decl(decl, None, false),
body_id,
fn_decl_span,
- is_generator)
+ generator_option)
})
})
}
///
/// This may also be a generator literal, indicated by the final boolean,
/// in that case there is an GeneratorClause.
- ExprClosure(CaptureClause, P<FnDecl>, BodyId, Span, bool),
+ ExprClosure(CaptureClause, P<FnDecl>, BodyId, Span, Option<GeneratorMovability>),
/// A block (`{ ... }`)
ExprBlock(P<Block>),
pub target_id: ScopeTarget,
}
+#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
+pub enum GeneratorMovability {
+ Static,
+ Movable,
+}
+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum CaptureClause {
CaptureByValue,
}
}
+impl_stable_hash_for!(enum hir::GeneratorMovability {
+ Static,
+ Movable
+});
+
impl_stable_hash_for!(enum hir::CaptureClause {
CaptureByValue,
CaptureByRef
impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
-impl_stable_hash_for!(struct ty::GeneratorInterior<'tcx> { witness });
+impl_stable_hash_for!(struct ty::GeneratorInterior<'tcx> { witness, movable });
impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
parent,
closure_substs.hash_stable(hcx, hasher);
interior.hash_stable(hcx, hasher);
}
+ TyGeneratorWitness(types) => {
+ types.hash_stable(hcx, hasher)
+ }
TyTuple(inner_tys, from_diverging_type_var) => {
inner_tys.hash_stable(hcx, hasher);
from_diverging_type_var.hash_stable(hcx, hasher);
if let Some(node_id) = hir.as_local_node_id(free_region.scope) {
match hir.get(node_id) {
NodeExpr(Expr {
- node: ExprClosure(_, _, _, closure_span, false),
+ node: ExprClosure(_, _, _, closure_span, None),
..
}) => {
let sup_sp = sup_origin.span();
ty::TyForeign(..) |
ty::TyParam(..) |
ty::TyClosure(..) |
+ ty::TyGeneratorWitness(..) |
ty::TyAnon(..) => {
t.super_fold_with(self)
}
terminating_scopes: FxHashSet<hir::ItemLocalId>,
}
+struct ExprLocatorVisitor {
+ id: ast::NodeId,
+ result: Option<usize>,
+ expr_and_pat_count: usize,
+}
+
+// This visitor has to have the same visit_expr calls as RegionResolutionVisitor
+// since `expr_count` is compared against the results there.
+impl<'tcx> Visitor<'tcx> for ExprLocatorVisitor {
+ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+ NestedVisitorMap::None
+ }
+
+ fn visit_pat(&mut self, pat: &'tcx Pat) {
+ self.expr_and_pat_count += 1;
+
+ intravisit::walk_pat(self, pat);
+ }
+
+ fn visit_expr(&mut self, expr: &'tcx Expr) {
+ debug!("ExprLocatorVisitor - pre-increment {} expr = {:?}",
+ self.expr_and_pat_count,
+ expr);
+
+ intravisit::walk_expr(self, expr);
+
+ self.expr_and_pat_count += 1;
+
+ debug!("ExprLocatorVisitor - post-increment {} expr = {:?}",
+ self.expr_and_pat_count,
+ expr);
+
+ if expr.id == self.id {
+ self.result = Some(self.expr_and_pat_count);
+ }
+ }
+}
impl<'tcx> ScopeTree {
pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
return true;
}
+ /// Returns the id of the innermost containing body
+ pub fn containing_body(&self, mut scope: Scope)-> Option<hir::ItemLocalId> {
+ loop {
+ if let ScopeData::CallSite(id) = scope.data() {
+ return Some(id);
+ }
+
+ match self.opt_encl_scope(scope) {
+ None => return None,
+ Some(parent) => scope = parent,
+ }
+ }
+ }
+
/// Finds the nearest common ancestor (if any) of two scopes. That is, finds the smallest
/// scope which is greater than or equal to both `scope_a` and `scope_b`.
pub fn nearest_common_ancestor(&self,
self.yield_in_scope.get(&scope).cloned()
}
+ /// Checks whether the given scope contains a `yield` and if that yield could execute
+ /// after `expr`. If so, it returns the span of that `yield`.
+ /// `scope` must be inside the body.
+ pub fn yield_in_scope_for_expr(&self,
+ scope: Scope,
+ expr: ast::NodeId,
+ body: &'tcx hir::Body) -> Option<Span> {
+ self.yield_in_scope(scope).and_then(|(span, count)| {
+ let mut visitor = ExprLocatorVisitor {
+ id: expr,
+ result: None,
+ expr_and_pat_count: 0,
+ };
+ visitor.visit_body(body);
+ if count >= visitor.result.unwrap() {
+ Some(span)
+ } else {
+ None
+ }
+ })
+ }
+
/// Gives the number of expressions visited in a body.
/// Used to sanity check visit_expr call count when
/// calculating generator interiors.
record_var_lifetime(visitor, pat.hir_id.local_id, pat.span);
}
+ debug!("resolve_pat - pre-increment {} pat = {:?}", visitor.expr_and_pat_count, pat);
+
intravisit::walk_pat(visitor, pat);
visitor.expr_and_pat_count += 1;
+
+ debug!("resolve_pat - post-increment {} pat = {:?}", visitor.expr_and_pat_count, pat);
}
fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, stmt: &'tcx hir::Stmt) {
}
fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr: &'tcx hir::Expr) {
- debug!("resolve_expr(expr.id={:?})", expr.id);
+ debug!("resolve_expr - pre-increment {} expr = {:?}", visitor.expr_and_pat_count, expr);
let prev_cx = visitor.cx;
visitor.enter_node_scope_with_dtor(expr.hir_id.local_id);
visitor.expr_and_pat_count += 1;
+ debug!("resolve_expr post-increment {}, expr = {:?}", visitor.expr_and_pat_count, expr);
+
if let hir::ExprYield(..) = expr.node {
// Mark this expr's scope and all parent scopes as containing `yield`.
let mut scope = Scope::Node(expr.hir_id.local_id);
}
}
- if let Some(pat) = pat {
- visitor.visit_pat(pat);
- }
+ // Make sure we visit the initializer first, so expr_and_pat_count remains correct
if let Some(expr) = init {
visitor.visit_expr(expr);
}
+ if let Some(pat) = pat {
+ visitor.visit_pat(pat);
+ }
/// True if `pat` match the `P&` nonterminal:
///
true
}
- ty::TyClosure(..) | ty::TyGenerator(..) | ty::TyAnon(..) => {
+ ty::TyClosure(..) |
+ ty::TyGenerator(..) |
+ ty::TyGeneratorWitness(..) |
+ ty::TyAnon(..) => {
bug!("ty_is_local invoked on unexpected type: {:?}", ty)
}
}
},
ty::TyGenerator(..) => Some(18),
ty::TyForeign(..) => Some(19),
+ ty::TyGeneratorWitness(..) => Some(20),
ty::TyInfer(..) | ty::TyError => None
}
}
ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyRawPtr(..) |
ty::TyChar | ty::TyRef(..) | ty::TyGenerator(..) |
- ty::TyArray(..) | ty::TyClosure(..) | ty::TyNever |
- ty::TyError => {
+ ty::TyGeneratorWitness(..) | ty::TyArray(..) | ty::TyClosure(..) |
+ ty::TyNever | ty::TyError => {
// safe for everything
Where(ty::Binder(Vec::new()))
}
}
ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
- ty::TyGenerator(..) | ty::TyForeign(..) |
+ ty::TyGenerator(..) | ty::TyGeneratorWitness(..) | ty::TyForeign(..) |
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
Never
}
}
ty::TyGenerator(def_id, ref substs, interior) => {
- let witness = iter::once(interior.witness);
- substs.upvar_tys(def_id, self.tcx()).chain(witness).collect()
+ substs.upvar_tys(def_id, self.tcx()).chain(iter::once(interior.witness)).collect()
+ }
+
+ ty::TyGeneratorWitness(types) => {
+ // This is sound because no regions in the witness can refer to
+ // the binder outside the witness. So we'll effectivly reuse
+ // the implicit binder around the witness.
+ types.skip_binder().to_vec()
}
// for `PhantomData<T>`, we pass `T`
pub fn print_debug_stats(self) {
sty_debug_print!(
self,
- TyAdt, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr, TyGenerator, TyForeign,
- TyDynamic, TyClosure, TyTuple, TyParam, TyInfer, TyProjection, TyAnon);
+ TyAdt, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
+ TyGenerator, TyGeneratorWitness, TyDynamic, TyClosure, TyTuple,
+ TyParam, TyInfer, TyProjection, TyAnon, TyForeign);
println!("Substs interner: #{}", self.interners.substs.borrow().len());
println!("Region interner: #{}", self.interners.region.borrow().len());
self.mk_ty(TyGenerator(id, closure_substs, interior))
}
+ pub fn mk_generator_witness(self, types: ty::Binder<&'tcx Slice<Ty<'tcx>>>) -> Ty<'tcx> {
+ self.mk_ty(TyGeneratorWitness(types))
+ }
+
pub fn mk_var(self, v: TyVid) -> Ty<'tcx> {
self.mk_infer(TyVar(v))
}
}
ty::TyClosure(..) => "closure".to_string(),
ty::TyGenerator(..) => "generator".to_string(),
+ ty::TyGeneratorWitness(..) => "generator witness".to_string(),
ty::TyTuple(..) => "tuple".to_string(),
ty::TyInfer(ty::TyVar(_)) => "inferred type".to_string(),
ty::TyInfer(ty::IntVar(_)) => "integral variable".to_string(),
TraitSimplifiedType(D),
ClosureSimplifiedType(D),
GeneratorSimplifiedType(D),
+ GeneratorWitnessSimplifiedType(usize),
AnonSimplifiedType(D),
FunctionSimplifiedType(usize),
ParameterSimplifiedType,
ty::TyGenerator(def_id, _, _) => {
Some(GeneratorSimplifiedType(def_id))
}
+ ty::TyGeneratorWitness(ref tys) => {
+ Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
+ }
ty::TyNever => Some(NeverSimplifiedType),
ty::TyTuple(ref tys, _) => {
Some(TupleSimplifiedType(tys.len()))
TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
+ GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
AnonSimplifiedType(d) => AnonSimplifiedType(map(d)),
FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
ParameterSimplifiedType => ParameterSimplifiedType,
TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
+ GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
AnonSimplifiedType(d) => d.hash_stable(hcx, hasher),
FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),
self.add_ty(interior.witness);
}
+ &ty::TyGeneratorWitness(ref ts) => {
+ let mut computation = FlagComputation::new();
+ computation.add_tys(&ts.skip_binder()[..]);
+ self.add_bound_computation(&computation);
+ }
+
&ty::TyClosure(_, ref substs) => {
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
ty::TyAnon(..) |
ty::TyInfer(_) |
ty::TyError |
+ ty::TyGeneratorWitness(..) |
ty::TyNever |
ty::TyFloat(_) => None,
}
ty::TyParam(_) => {
return Err(LayoutError::Unknown(ty));
}
- ty::TyInfer(_) | ty::TyError => {
+ ty::TyGeneratorWitness(..) | ty::TyInfer(_) | ty::TyError => {
bug!("LayoutDetails::compute: unexpected type `{}`", ty)
}
})
ty::TyFnPtr(_) |
ty::TyNever |
ty::TyFnDef(..) |
- ty::TyDynamic(..) |
- ty::TyForeign(..) => {
+ ty::TyGeneratorWitness(..) |
+ ty::TyForeign(..) |
+ ty::TyDynamic(..) => {
bug!("TyLayout::field_type({:?}): not applicable", self)
}
vec![]
}
- TyStr | TyDynamic(..) | TySlice(_) | TyForeign(..) | TyError => {
+ TyStr |
+ TyDynamic(..) |
+ TySlice(_) |
+ TyForeign(..) |
+ TyError |
+ TyGeneratorWitness(..) => {
// these are never sized - return the target type
vec![ty]
}
}
}
- ty::TyGenerator(def_id, ref substs, ref interior) => {
+ ty::TyGenerator(def_id, ref substs, _) => {
// Same as the closure case
for upvar_ty in substs.upvar_tys(def_id, *self) {
self.compute_components(upvar_ty, out);
}
- // But generators can have additional interior types
- self.compute_components(interior.witness, out);
+ // We ignore regions in the generator interior as we don't
+ // want these to affect region inference
}
+ // All regions are bound inside a witness
+ ty::TyGeneratorWitness(..) => (),
+
// OutlivesTypeParameterEnv -- the actual checking that `X:'a`
// is implied by the environment is done in regionck.
ty::TyParam(p) => {
use traits::Reveal;
use ty::subst::{Kind, Substs};
use ty::{self, Ty, TyCtxt, TypeFoldable};
+use ty::fold::{TypeVisitor, TypeFolder};
use ty::error::{ExpectedFound, TypeError};
use util::common::ErrorReported;
use std::rc::Rc;
}
}
+#[derive(Debug, Clone)]
+struct GeneratorWitness<'tcx>(&'tcx ty::Slice<Ty<'tcx>>);
+
+impl<'tcx> TypeFoldable<'tcx> for GeneratorWitness<'tcx> {
+ fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
+ GeneratorWitness(self.0.fold_with(folder))
+ }
+
+ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
+ self.0.visit_with(visitor)
+ }
+}
+
+impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
+ fn relate<'a, 'gcx, R>(relation: &mut R,
+ a: &GeneratorWitness<'tcx>,
+ b: &GeneratorWitness<'tcx>)
+ -> RelateResult<'tcx, GeneratorWitness<'tcx>>
+ where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
+ {
+ assert!(a.0.len() == b.0.len());
+ let tcx = relation.tcx();
+ let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
+ Ok(GeneratorWitness(types))
+ }
+}
+
impl<'tcx> Relate<'tcx> for Ty<'tcx> {
fn relate<'a, 'gcx, R>(relation: &mut R,
a: &Ty<'tcx>,
Ok(tcx.mk_generator(a_id, substs, interior))
}
+ (&ty::TyGeneratorWitness(a_types), &ty::TyGeneratorWitness(b_types)) =>
+ {
+ // Wrap our types with a temporary GeneratorWitness struct
+ // inside the binder so we can related them
+ let a_types = ty::Binder(GeneratorWitness(*a_types.skip_binder()));
+ let b_types = ty::Binder(GeneratorWitness(*b_types.skip_binder()));
+ // Then remove the GeneratorWitness for the result
+ let types = ty::Binder(relation.relate(&a_types, &b_types)?.skip_binder().0);
+ Ok(tcx.mk_generator_witness(types))
+ }
+
(&ty::TyClosure(a_id, a_substs),
&ty::TyClosure(b_id, b_substs))
if a_id == b_id =>
-> RelateResult<'tcx, ty::GeneratorInterior<'tcx>>
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
{
- let interior = relation.relate(&a.witness, &b.witness)?;
- Ok(ty::GeneratorInterior::new(interior))
+ assert_eq!(a.movable, b.movable);
+ let witness = relation.relate(&a.witness, &b.witness)?;
+ Ok(ty::GeneratorInterior { witness, movable: a.movable })
}
}
type Lifted = ty::GeneratorInterior<'tcx>;
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self.witness).map(|witness| {
- ty::GeneratorInterior { witness }
+ ty::GeneratorInterior { witness, movable: self.movable }
})
}
}
ty::TyGenerator(did, substs, interior) => {
ty::TyGenerator(did, substs.fold_with(folder), interior.fold_with(folder))
}
+ ty::TyGeneratorWitness(types) => ty::TyGeneratorWitness(types.fold_with(folder)),
ty::TyClosure(did, substs) => ty::TyClosure(did, substs.fold_with(folder)),
ty::TyProjection(ref data) => ty::TyProjection(data.fold_with(folder)),
ty::TyAnon(did, substs) => ty::TyAnon(did, substs.fold_with(folder)),
ty::TyGenerator(_did, ref substs, ref interior) => {
substs.visit_with(visitor) || interior.visit_with(visitor)
}
+ ty::TyGeneratorWitness(ref types) => types.visit_with(visitor),
ty::TyClosure(_did, ref substs) => substs.visit_with(visitor),
ty::TyProjection(ref data) => data.visit_with(visitor),
ty::TyAnon(_, ref substs) => substs.visit_with(visitor),
impl<'tcx> TypeFoldable<'tcx> for ty::GeneratorInterior<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
- ty::GeneratorInterior::new(self.witness.fold_with(folder))
+ ty::GeneratorInterior {
+ witness: self.witness.fold_with(folder),
+ movable: self.movable,
+ }
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
/// `|a| yield a`.
TyGenerator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
+ /// A type representin the types stored inside a generator.
+ /// This should only appear in GeneratorInteriors.
+ TyGeneratorWitness(Binder<&'tcx Slice<Ty<'tcx>>>),
+
/// The never type `!`
TyNever,
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
pub struct GeneratorInterior<'tcx> {
pub witness: Ty<'tcx>,
-}
-
-impl<'tcx> GeneratorInterior<'tcx> {
- pub fn new(witness: Ty<'tcx>) -> GeneratorInterior<'tcx> {
- GeneratorInterior { witness }
- }
-
- pub fn as_slice(&self) -> &'tcx Slice<Ty<'tcx>> {
- match self.witness.sty {
- ty::TyTuple(s, _) => s,
- _ => bug!(),
- }
- }
+ pub movable: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
}
TyFnDef(..) |
TyFnPtr(_) |
+ TyGeneratorWitness(..) |
TyBool |
TyChar |
TyInt(_) |
HashStable};
use rustc_data_structures::fx::FxHashMap;
use std::cmp;
-use std::iter;
use std::hash::Hash;
use std::intrinsics;
use syntax::ast::{self, Name};
let result = match ty.sty {
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) |
ty::TyFloat(_) | ty::TyStr | ty::TyNever | ty::TyForeign(..) |
- ty::TyRawPtr(..) | ty::TyRef(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) => {
+ ty::TyRawPtr(..) | ty::TyRef(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) |
+ ty::TyGeneratorWitness(..) => {
// these types never have a destructor
Ok(ty::DtorckConstraint::empty())
}
}).collect()
}
- ty::TyGenerator(def_id, substs, interior) => {
- substs.upvar_tys(def_id, self).chain(iter::once(interior.witness)).map(|ty| {
+ ty::TyGenerator(def_id, substs, _) => {
+ // Note that the interior types are ignored here.
+ // Any type reachable inside the interior must also be reachable
+ // through the upvars.
+ substs.upvar_tys(def_id, self).map(|ty| {
self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty)
}).collect()
}
self.def_id(d);
}
}
+ TyGeneratorWitness(tys) => {
+ self.hash(tys.skip_binder().len());
+ }
TyTuple(tys, defaulted) => {
self.hash(tys.len());
self.hash(defaulted);
// Fast-path for primitive types
ty::TyInfer(ty::FreshIntTy(_)) | ty::TyInfer(ty::FreshFloatTy(_)) |
ty::TyBool | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) | ty::TyNever |
- ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar |
+ ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar | ty::TyGeneratorWitness(..) |
ty::TyRawPtr(_) | ty::TyRef(..) | ty::TyStr => false,
// Foreign types can never have destructors
stack.extend(substs.substs.types().rev());
}
ty::TyGenerator(_, ref substs, ref interior) => {
- stack.extend(substs.substs.types().rev());
stack.push(interior.witness);
+ stack.extend(substs.substs.types().rev());
+ }
+ ty::TyGeneratorWitness(ts) => {
+ stack.extend(ts.skip_binder().iter().cloned().rev());
}
ty::TyTuple(ts, _) => {
stack.extend(ts.iter().cloned().rev());
ty::TyFloat(..) |
ty::TyError |
ty::TyStr |
+ ty::TyGeneratorWitness(..) |
ty::TyNever |
ty::TyParam(_) |
ty::TyForeign(..) => {
use ty::{TyBool, TyChar, TyAdt};
use ty::{TyError, TyStr, TyArray, TySlice, TyFloat, TyFnDef, TyFnPtr};
use ty::{TyParam, TyRawPtr, TyRef, TyNever, TyTuple};
-use ty::{TyClosure, TyGenerator, TyForeign, TyProjection, TyAnon};
+use ty::{TyClosure, TyGenerator, TyGeneratorWitness, TyForeign, TyProjection, TyAnon};
use ty::{TyDynamic, TyInt, TyUint, TyInfer};
use ty::{self, Ty, TyCtxt, TypeFoldable};
use util::nodemap::FxHashSet;
}
}
+define_print! {
+ ('tcx) &'tcx ty::Slice<Ty<'tcx>>, (self, f, cx) {
+ display {
+ write!(f, "{{")?;
+ let mut tys = self.iter();
+ if let Some(&ty) = tys.next() {
+ print!(f, cx, print(ty))?;
+ for &ty in tys {
+ print!(f, cx, write(", "), print(ty))?;
+ }
+ }
+ write!(f, "}}")
+ }
+ }
+}
+
define_print! {
('tcx) ty::TypeAndMut<'tcx>, (self, f, cx) {
display {
TyStr => write!(f, "str"),
TyGenerator(did, substs, interior) => ty::tls::with(|tcx| {
let upvar_tys = substs.upvar_tys(did, tcx);
- write!(f, "[generator")?;
+ if interior.movable {
+ write!(f, "[generator")?;
+ } else {
+ write!(f, "[static generator")?;
+ }
if let Some(node_id) = tcx.hir.as_local_node_id(did) {
write!(f, "@{:?}", tcx.hir.span(node_id))?;
print!(f, cx, write(" "), print(interior), write("]"))
}),
+ TyGeneratorWitness(types) => {
+ ty::tls::with(|tcx| cx.in_binder(f, tcx, &types, tcx.lift(&types)))
+ }
TyClosure(did, substs) => ty::tls::with(|tcx| {
let upvar_tys = substs.upvar_tys(did, tcx);
write!(f, "[closure")?;
use rustc::middle::mem_categorization as mc;
use rustc::middle::mem_categorization::Categorization;
use rustc::middle::region;
-use rustc::ty::{self, TyCtxt};
+use rustc::ty::{self, TyCtxt, RegionKind};
use syntax::ast;
use syntax_pos::Span;
use rustc::hir;
move_data: &'a move_data::FlowedMoveData<'a, 'tcx>,
all_loans: &'a [Loan<'tcx>],
param_env: ty::ParamEnv<'tcx>,
+ movable_generator: bool,
}
impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
}
self.check_for_conflicting_loans(hir_id.local_id);
+
+ self.check_for_loans_across_yields(cmt, loan_region, borrow_span);
}
fn mutate(&mut self,
debug!("check_loans(body id={})", body.value.id);
let def_id = bccx.tcx.hir.body_owner_def_id(body.id());
+
+ let node_id = bccx.tcx.hir.as_local_node_id(def_id).unwrap();
+ let movable_generator = !match bccx.tcx.hir.get(node_id) {
+ hir::map::Node::NodeExpr(&hir::Expr {
+ node: hir::ExprClosure(.., Some(hir::GeneratorMovability::Static)),
+ ..
+ }) => true,
+ _ => false,
+ };
+
let param_env = bccx.tcx.param_env(def_id);
let mut clcx = CheckLoanCtxt {
bccx,
move_data,
all_loans,
param_env,
+ movable_generator,
};
let rvalue_promotable_map = bccx.tcx.rvalue_promotable_map(def_id);
euv::ExprUseVisitor::new(&mut clcx,
return result;
}
+ pub fn check_for_loans_across_yields(&self,
+ cmt: mc::cmt<'tcx>,
+ loan_region: ty::Region<'tcx>,
+ borrow_span: Span) {
+ pub fn borrow_of_local_data<'tcx>(cmt: &mc::cmt<'tcx>) -> bool {
+ match cmt.cat {
+ // Borrows of static items is allowed
+ Categorization::StaticItem => false,
+ // Reborrow of already borrowed data is ignored
+ // Any errors will be caught on the initial borrow
+ Categorization::Deref(..) => false,
+
+ // By-ref upvars has Derefs so they will get ignored.
+ // Generators counts as FnOnce so this leaves only
+ // by-move upvars, which is local data for generators
+ Categorization::Upvar(..) => true,
+
+ Categorization::Rvalue(region) => {
+ // Rvalues promoted to 'static are no longer local
+ if let RegionKind::ReStatic = *region {
+ false
+ } else {
+ true
+ }
+ }
+
+ // Borrow of local data must be checked
+ Categorization::Local(..) => true,
+
+ // For interior references and downcasts, find out if the base is local
+ Categorization::Downcast(ref cmt_base, _) |
+ Categorization::Interior(ref cmt_base, _) => borrow_of_local_data(&cmt_base),
+ }
+ }
+
+ if !self.movable_generator {
+ return;
+ }
+
+ if !borrow_of_local_data(&cmt) {
+ return;
+ }
+
+ let scope = match *loan_region {
+ // A concrete region in which we will look for a yield expression
+ RegionKind::ReScope(scope) => scope,
+
+ // There cannot be yields inside an empty region
+ RegionKind::ReEmpty => return,
+
+ // Local data cannot have these lifetimes
+ RegionKind::ReEarlyBound(..) |
+ RegionKind::ReLateBound(..) |
+ RegionKind::ReFree(..) |
+ RegionKind::ReStatic => return,
+
+ // These cannot exist in borrowck
+ RegionKind::ReVar(..) |
+ RegionKind::ReSkolemized(..) |
+ RegionKind::ReClosureBound(..) |
+ RegionKind::ReErased => span_bug!(borrow_span,
+ "unexpected region in borrowck {:?}",
+ loan_region),
+ };
+
+ let body_id = self.bccx.body.value.hir_id.local_id;
+
+ if self.bccx.region_scope_tree.containing_body(scope) != Some(body_id) {
+ // We are borrowing a local data longer than it's storage.
+ // This should result in other borrowck errors.
+ // FIXME: Ensure an error is generated
+ return;
+ }
+
+ if let Some(yield_span) = self.bccx
+ .region_scope_tree
+ .yield_in_scope_for_expr(scope,
+ cmt.id,
+ self.bccx.body) {
+ self.bccx.cannot_borrow_across_generator_yield(borrow_span,
+ yield_span,
+ Origin::Ast).emit();
+ }
+ }
+
pub fn check_for_conflicting_loans(&self, node: hir::ItemLocalId) {
//! Checks to see whether any of the loans that are issued
//! on entrance to `node` conflict with loans that have already been
}
};
- // When you have a borrow that lives across a yield,
- // that reference winds up captured in the generator
- // type. Regionck then constraints it to live as long
- // as the generator itself. If that borrow is borrowing
- // data owned by the generator, this winds up resulting in
- // an `err_out_of_scope` error:
- //
- // ```
- // {
- // let g = || {
- // let a = &3; // this borrow is forced to ... -+
- // yield (); // |
- // println!("{}", a); // |
- // }; // |
- // } <----------------------... live until here --------+
- // ```
- //
- // To detect this case, we look for cases where the
- // `super_scope` (lifetime of the value) is within the
- // body, but the `sub_scope` is not.
- debug!("err_out_of_scope: self.body.is_generator = {:?}",
- self.body.is_generator);
- let maybe_borrow_across_yield = if self.body.is_generator {
- let body_scope = region::Scope::Node(self.body.value.hir_id.local_id);
- debug!("err_out_of_scope: body_scope = {:?}", body_scope);
- debug!("err_out_of_scope: super_scope = {:?}", super_scope);
- debug!("err_out_of_scope: sub_scope = {:?}", sub_scope);
- match (super_scope, sub_scope) {
- (&ty::RegionKind::ReScope(value_scope),
- &ty::RegionKind::ReScope(loan_scope)) => {
- if {
- // value_scope <= body_scope &&
- self.region_scope_tree.is_subscope_of(value_scope, body_scope) &&
- // body_scope <= loan_scope
- self.region_scope_tree.is_subscope_of(body_scope, loan_scope)
- } {
- // We now know that this is a case
- // that fits the bill described above:
- // a borrow of something whose scope
- // is within the generator, but the
- // borrow is for a scope outside the
- // generator.
- //
- // Now look within the scope of the of
- // the value being borrowed (in the
- // example above, that would be the
- // block remainder that starts with
- // `let a`) for a yield. We can cite
- // that for the user.
- self.region_scope_tree.yield_in_scope(value_scope)
- } else {
- None
- }
- }
- _ => None,
- }
- } else {
- None
- };
-
- if let Some((yield_span, _)) = maybe_borrow_across_yield {
- debug!("err_out_of_scope: opt_yield_span = {:?}", yield_span);
- self.cannot_borrow_across_generator_yield(error_span, yield_span, Origin::Ast)
- .emit();
- return;
- }
-
let mut db = self.path_does_not_live_long_enough(error_span, &msg, Origin::Ast);
let value_kind = match err.cmt.cat {
mc::Categorization::Rvalue(..) => "temporary value",
ty::TyError |
ty::TyClosure(..) |
ty::TyGenerator(..) |
+ ty::TyGeneratorWitness(..) |
ty::TyProjection(..) |
ty::TyAnon(..) |
ty::TyFnDef(..) => bug!("Unexpected type in foreign function"),
TyDynamic(..) => bug!("miri produced a trait object"),
TyClosure(..) => bug!("miri produced a closure"),
TyGenerator(..) => bug!("miri produced a generator"),
+ TyGeneratorWitness(..) => bug!("miri produced a generator witness"),
TyNever => bug!("miri produced a value of the never type"),
TyProjection(_) => bug!("miri produced a projection"),
TyAnon(..) => bug!("miri produced an impl Trait type"),
ty::TyInfer(_) |
ty::TyProjection(..) |
ty::TyParam(_) |
+ ty::TyGeneratorWitness(_) |
ty::TyAnon(..) => {
bug!("DefPathBasedNames: Trying to create type name for \
unexpected type: {:?}", t);
}
}
+struct BorrowedLocals(liveness::LocalSet);
+
+fn mark_as_borrowed<'tcx>(place: &Place<'tcx>, locals: &mut BorrowedLocals) {
+ match *place {
+ Place::Local(l) => { locals.0.add(&l); },
+ Place::Static(..) => (),
+ Place::Projection(ref proj) => {
+ match proj.elem {
+ // For derefs we don't look any further.
+ // If it pointed to a Local, it would already be borrowed elsewhere
+ ProjectionElem::Deref => (),
+ _ => mark_as_borrowed(&proj.base, locals)
+ }
+ }
+ }
+}
+
+impl<'tcx> Visitor<'tcx> for BorrowedLocals {
+ fn visit_rvalue(&mut self,
+ rvalue: &Rvalue<'tcx>,
+ location: Location) {
+ if let Rvalue::Ref(_, _, ref place) = *rvalue {
+ mark_as_borrowed(place, self);
+ }
+
+ self.super_rvalue(rvalue, location)
+ }
+}
+
fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
mir: &Mir<'tcx>,
- source: MirSource) ->
+ source: MirSource,
+ movable: bool) ->
(liveness::LocalSet,
HashMap<BasicBlock, liveness::LocalSet>) {
let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
let mut ignored = StorageIgnored(IdxSetBuf::new_filled(mir.local_decls.len()));
ignored.visit_mir(mir);
+ let mut borrowed_locals = BorrowedLocals(IdxSetBuf::new_empty(mir.local_decls.len()));
+ borrowed_locals.visit_mir(mir);
+
let mut set = liveness::LocalSet::new_empty(mir.local_decls.len());
- let liveness = liveness::liveness_of_locals(mir, LivenessMode {
+ let mut liveness = liveness::liveness_of_locals(mir, LivenessMode {
include_regular_use: true,
include_drops: true,
});
// Mark locals without storage statements as always having live storage
live_locals.union(&ignored.0);
+ if !movable {
+ // For immovable generators we consider borrowed locals to always be live.
+ // This effectively makes those locals use just the storage liveness.
+ liveness.outs[block].union(&borrowed_locals.0);
+ }
+
// Locals live are live at this point only if they are used across suspension points
// and their storage is live
live_locals.intersect(&liveness.outs[block]);
fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
+ upvars: Vec<Ty<'tcx>>,
interior: GeneratorInterior<'tcx>,
mir: &mut Mir<'tcx>)
-> (HashMap<Local, (Ty<'tcx>, usize)>,
HashMap<BasicBlock, liveness::LocalSet>)
{
// Use a liveness analysis to compute locals which are live across a suspension point
- let (live_locals, storage_liveness) = locals_live_across_suspend_points(tcx, mir, source);
-
+ let (live_locals, storage_liveness) = locals_live_across_suspend_points(tcx,
+ mir,
+ source,
+ interior.movable);
// Erase regions from the types passed in from typeck so we can compare them with
// MIR types
- let allowed = tcx.erase_regions(&interior.as_slice());
+ let allowed_upvars = tcx.erase_regions(&upvars);
+ let allowed = match interior.witness.sty {
+ ty::TyGeneratorWitness(s) => tcx.erase_late_bound_regions(&s),
+ _ => bug!(),
+ };
for (local, decl) in mir.local_decls.iter_enumerated() {
// Ignore locals which are internal or not live
// Sanity check that typeck knows about the type of locals which are
// live across a suspension point
- if !allowed.contains(&decl.ty) {
+ if !allowed.contains(&decl.ty) && !allowed_upvars.contains(&decl.ty) {
span_bug!(mir.span,
"Broken MIR: generator contains type {} in MIR, \
but typeck only knows about {}",
assert!(mir.generator_drop.is_none());
let def_id = source.def_id;
- let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
- let hir_id = tcx.hir.node_to_hir_id(node_id);
-
- // Get the interior types which typeck computed
- let tables = tcx.typeck_tables_of(def_id);
- let interior = match tables.node_id_to_type(hir_id).sty {
- ty::TyGenerator(_, _, interior) => interior,
- ref t => bug!("type of generator not a generator: {:?}", t),
- };
// The first argument is the generator type passed by value
let gen_ty = mir.local_decls.raw[1].ty;
+ // Get the interior types and substs which typeck computed
+ let (upvars, interior) = match gen_ty.sty {
+ ty::TyGenerator(_, substs, interior) => {
+ (substs.upvar_tys(def_id, tcx).collect(), interior)
+ }
+ _ => bug!(),
+ };
+
// Compute GeneratorState<yield_ty, return_ty>
let state_did = tcx.lang_items().gen_state().unwrap();
let state_adt_ref = tcx.adt_def(state_did);
// Extract locals which are live across suspension point into `layout`
// `remap` gives a mapping from local indices onto generator struct indices
// `storage_liveness` tells us which locals have live storage at suspension points
- let (remap, layout, storage_liveness) = compute_layout(tcx, source, interior, mir);
+ let (remap, layout, storage_liveness) = compute_layout(tcx, source, upvars, interior, mir);
let state_field = mir.upvar_decls.len();
self.super_constant(constant, location);
let Constant { span, ty, literal } = constant;
self.push(&format!("mir::Constant"));
- self.push(&format!("└ span: {:?}", span));
- self.push(&format!("└ ty: {:?}", ty));
- self.push(&format!("└ literal: {:?}", literal));
+ self.push(&format!("+ span: {:?}", span));
+ self.push(&format!("+ ty: {:?}", ty));
+ self.push(&format!("+ literal: {:?}", literal));
}
fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) {
self.super_const(constant);
let ty::Const { ty, val } = constant;
self.push(&format!("ty::Const"));
- self.push(&format!("└ ty: {:?}", ty));
- self.push(&format!("└ val: {:?}", val));
+ self.push(&format!("+ ty: {:?}", ty));
+ self.push(&format!("+ val: {:?}", val));
}
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
Rvalue::Aggregate(kind, _) => match **kind {
AggregateKind::Closure(def_id, substs) => {
self.push(&format!("closure"));
- self.push(&format!("└ def_id: {:?}", def_id));
- self.push(&format!("└ substs: {:#?}", substs));
+ self.push(&format!("+ def_id: {:?}", def_id));
+ self.push(&format!("+ substs: {:#?}", substs));
}
AggregateKind::Generator(def_id, substs, interior) => {
self.push(&format!("generator"));
- self.push(&format!("└ def_id: {:?}", def_id));
- self.push(&format!("└ substs: {:#?}", substs));
- self.push(&format!("└ interior: {:?}", interior));
+ self.push(&format!("+ def_id: {:?}", def_id));
+ self.push(&format!("+ substs: {:#?}", substs));
+ self.push(&format!("+ interior: {:?}", interior));
}
_ => {}
_ => span_bug!(ex.span, "Expected struct or tuple type, found {:?}", ty),
}
}
- ast::ExprKind::Closure(_, ref decl, ref body, _fn_decl_span) => {
+ ast::ExprKind::Closure(_, _, ref decl, ref body, _fn_decl_span) => {
let mut id = String::from("$");
id.push_str(&ex.id.to_string());
ty::TyInfer(_) |
ty::TyProjection(..) |
ty::TyAnon(..) |
+ ty::TyGeneratorWitness(..) |
ty::TyParam(_) => {
bug!("debuginfo: Trying to create type name for \
unexpected type: {:?}", t);
ty::TyInfer(_) => None,
ty::TyBool | ty::TyChar | ty::TyInt(..) | ty::TyUint(..) |
- ty::TyFloat(_) | ty::TyArray(..) |
+ ty::TyFloat(_) | ty::TyArray(..) | ty::TyGeneratorWitness(..) |
ty::TyRawPtr(_) | ty::TyRef(..) | ty::TyFnDef(..) |
ty::TyFnPtr(..) | ty::TyClosure(..) | ty::TyGenerator(..) |
ty::TyAdt(..) | ty::TyNever | ty::TyError => {
_capture: hir::CaptureClause,
decl: &'gcx hir::FnDecl,
body_id: hir::BodyId,
+ gen: Option<hir::GeneratorMovability>,
expected: Expectation<'tcx>,
) -> Ty<'tcx> {
debug!(
None => (None, None),
};
let body = self.tcx.hir.body(body_id);
- self.check_closure(expr, expected_kind, decl, body, expected_sig)
+ self.check_closure(expr, expected_kind, decl, body, gen, expected_sig)
}
fn check_closure(
opt_kind: Option<ty::ClosureKind>,
decl: &'gcx hir::FnDecl,
body: &'gcx hir::Body,
+ gen: Option<hir::GeneratorMovability>,
expected_sig: Option<ty::FnSig<'tcx>>,
) -> Ty<'tcx> {
debug!(
decl,
expr.id,
body,
- true,
+ gen,
).1;
// Create type variables (for now) to represent the transformed
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use rustc::hir::{self, Pat, PatKind, Expr};
use rustc::middle::region;
-use rustc::ty::Ty;
+use rustc::ty::{self, Ty, GeneratorInterior};
use std::rc::Rc;
+use syntax_pos::Span;
use super::FnCtxt;
use util::nodemap::FxHashMap;
}
impl<'a, 'gcx, 'tcx> InteriorVisitor<'a, 'gcx, 'tcx> {
- fn record(&mut self, ty: Ty<'tcx>, scope: Option<region::Scope>, expr: Option<&'tcx Expr>) {
+ fn record(&mut self,
+ ty: Ty<'tcx>,
+ scope: Option<region::Scope>,
+ expr: Option<&'tcx Expr>,
+ source_span: Span) {
use syntax_pos::DUMMY_SP;
let live_across_yield = scope.map_or(Some(DUMMY_SP), |s| {
- self.region_scope_tree.yield_in_scope(s).and_then(|(span, expr_count)| {
+ self.region_scope_tree.yield_in_scope(s).and_then(|(yield_span, expr_count)| {
// If we are recording an expression that is the last yield
// in the scope, or that has a postorder CFG index larger
// than the one of all of the yields, then its value can't
// be storage-live (and therefore live) at any of the yields.
//
// See the mega-comment at `yield_in_scope` for a proof.
+
+ debug!("comparing counts yield: {} self: {}, source_span = {:?}",
+ expr_count, self.expr_count, source_span);
+
if expr_count >= self.expr_count {
- Some(span)
+ Some(yield_span)
} else {
None
}
})
});
- if let Some(span) = live_across_yield {
+ if let Some(yield_span) = live_across_yield {
let ty = self.fcx.resolve_type_vars_if_possible(&ty);
- debug!("type in expr = {:?}, scope = {:?}, type = {:?}, span = {:?}",
- expr, scope, ty, span);
-
- // Map the type to the number of types added before it
- let entries = self.types.len();
- self.types.entry(&ty).or_insert(entries);
+ debug!("type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
+ expr, scope, ty, self.expr_count, yield_span);
+
+ if self.fcx.any_unresolved_type_vars(&ty) {
+ let mut err = struct_span_err!(self.fcx.tcx.sess, source_span, E0907,
+ "type inside generator must be known in this context");
+ err.span_note(yield_span,
+ "the type is part of the generator because of this `yield`");
+ err.emit();
+ } else {
+ // Map the type to the number of types added before it
+ let entries = self.types.len();
+ self.types.entry(&ty).or_insert(entries);
+ }
} else {
- debug!("no type in expr = {:?}, span = {:?}", expr, expr.map(|e| e.span));
+ debug!("no type in expr = {:?}, count = {:?}, span = {:?}",
+ expr, self.expr_count, expr.map(|e| e.span));
}
}
}
pub fn resolve_interior<'a, 'gcx, 'tcx>(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
def_id: DefId,
body_id: hir::BodyId,
- witness: Ty<'tcx>) {
+ interior: GeneratorInterior<'tcx>) {
let body = fcx.tcx.hir.body(body_id);
let mut visitor = InteriorVisitor {
fcx,
types.sort_by_key(|t| t.1);
// Extract type components
- let types: Vec<_> = types.into_iter().map(|t| t.0).collect();
-
- let tuple = fcx.tcx.intern_tup(&types, false);
-
- debug!("Types in generator {:?}, span = {:?}", tuple, body.value.span);
-
- // Unify the tuple with the witness
- match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(witness, tuple) {
+ let type_list = fcx.tcx.mk_type_list(types.into_iter().map(|t| t.0));
+
+ // The types in the generator interior contain lifetimes local to the generator itself,
+ // which should not be exposed outside of the generator. Therefore, we replace these
+ // lifetimes with existentially-bound lifetimes, which reflect the exact value of the
+ // lifetimes not being known by users.
+ //
+ // These lifetimes are used in auto trait impl checking (for example,
+ // if a Sync generator contains an &'α T, we need to check whether &'α T: Sync),
+ // so knowledge of the exact relationships between them isn't particularly important.
+
+ debug!("Types in generator {:?}, span = {:?}", type_list, body.value.span);
+
+ // Replace all regions inside the generator interior with late bound regions
+ // Note that each region slot in the types gets a new fresh late bound region,
+ // which means that none of the regions inside relate to any other, even if
+ // typeck had previously found contraints that would cause them to be related.
+ let mut counter = 0;
+ let type_list = fcx.tcx.fold_regions(&type_list, &mut false, |_, current_depth| {
+ counter += 1;
+ fcx.tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(current_depth),
+ ty::BrAnon(counter)))
+ });
+
+ let witness = fcx.tcx.mk_generator_witness(ty::Binder(type_list));
+
+ debug!("Types in generator after region replacement {:?}, span = {:?}",
+ witness, body.value.span);
+
+ // Unify the type variable inside the generator with the new witness
+ match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior.witness, witness) {
Ok(ok) => fcx.register_infer_ok_obligations(ok),
_ => bug!(),
- }
+ }
}
// This visitor has to have the same visit_expr calls as RegionResolutionVisitor in
if let PatKind::Binding(..) = pat.node {
let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id);
let ty = self.fcx.tables.borrow().pat_ty(pat);
- self.record(ty, Some(scope), None);
+ self.record(ty, Some(scope), None, pat.span);
}
self.expr_count += 1;
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let ty = self.fcx.tables.borrow().expr_ty_adjusted(expr);
- self.record(ty, scope, Some(expr));
+ self.record(ty, scope, Some(expr), expr.span);
}
}
deferred_cast_checks: RefCell<Vec<cast::CastCheck<'tcx>>>,
- deferred_generator_interiors: RefCell<Vec<(hir::BodyId, Ty<'tcx>)>>,
+ deferred_generator_interiors: RefCell<Vec<(hir::BodyId, ty::GeneratorInterior<'tcx>)>>,
// Anonymized types found in explicit return types and their
// associated fresh inference variable. Writeback resolves these
param_env,
&fn_sig);
- let fcx = check_fn(&inh, param_env, fn_sig, decl, id, body, false).0;
+ let fcx = check_fn(&inh, param_env, fn_sig, decl, id, body, None).0;
fcx
} else {
let fcx = FnCtxt::new(&inh, param_env, body.value.id);
decl: &'gcx hir::FnDecl,
fn_id: ast::NodeId,
body: &'gcx hir::Body,
- can_be_generator: bool)
+ can_be_generator: Option<hir::GeneratorMovability>)
-> (FnCtxt<'a, 'gcx, 'tcx>, Option<GeneratorTypes<'tcx>>)
{
let mut fn_sig = fn_sig.clone();
let span = body.value.span;
- if body.is_generator && can_be_generator {
+ if body.is_generator && can_be_generator.is_some() {
fcx.yield_ty = Some(fcx.next_ty_var(TypeVariableOrigin::TypeInference(span)));
}
}
let fn_hir_id = fcx.tcx.hir.node_to_hir_id(fn_id);
- let gen_ty = if can_be_generator && body.is_generator {
+ inherited.tables.borrow_mut().liberated_fn_sigs_mut().insert(fn_hir_id, fn_sig);
+
+ fcx.check_return_expr(&body.value);
+
+ // We insert the deferred_generator_interiors entry after visiting the body.
+ // This ensures that all nested generators appear before the entry of this generator.
+ // resolve_generator_interiors relies on this property.
+ let gen_ty = if can_be_generator.is_some() && body.is_generator {
let witness = fcx.next_ty_var(TypeVariableOrigin::MiscVariable(span));
- fcx.deferred_generator_interiors.borrow_mut().push((body.id(), witness));
- let interior = ty::GeneratorInterior::new(witness);
+ let interior = ty::GeneratorInterior {
+ witness,
+ movable: can_be_generator.unwrap() == hir::GeneratorMovability::Movable,
+ };
+ fcx.deferred_generator_interiors.borrow_mut().push((body.id(), interior));
Some(GeneratorTypes { yield_ty: fcx.yield_ty.unwrap(), interior: interior })
} else {
None
};
- inherited.tables.borrow_mut().liberated_fn_sigs_mut().insert(fn_hir_id, fn_sig);
-
- fcx.check_return_expr(&body.value);
// Finalize the return check by taking the LUB of the return types
// we saw and assigning it to the expected return type. This isn't
}
fn resolve_generator_interiors(&self, def_id: DefId) {
- let mut deferred_generator_interiors = self.deferred_generator_interiors.borrow_mut();
- for (body_id, witness) in deferred_generator_interiors.drain(..) {
- generator_interior::resolve_interior(self, def_id, body_id, witness);
+ let mut generators = self.deferred_generator_interiors.borrow_mut();
+ for (body_id, interior) in generators.drain(..) {
+ self.select_obligations_where_possible();
+ generator_interior::resolve_interior(self, def_id, body_id, interior);
}
}
hir::ExprMatch(ref discrim, ref arms, match_src) => {
self.check_match(expr, &discrim, arms, expected, match_src)
}
- hir::ExprClosure(capture, ref decl, body_id, _, _) => {
- self.check_expr_closure(expr, capture, &decl, body_id, expected)
+ hir::ExprClosure(capture, ref decl, body_id, _, gen) => {
+ self.check_expr_closure(expr, capture, &decl, body_id, gen, expected)
}
hir::ExprBlock(ref body) => {
self.check_block_with_expected(&body, expected)
fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
match expr.node {
- hir::ExprClosure(cc, _, body_id, _, is_generator) => {
+ hir::ExprClosure(cc, _, body_id, _, gen) => {
let body = self.fcx.tcx.hir.body(body_id);
self.visit_body(body);
self.fcx
- .analyze_closure(expr.id, expr.hir_id, expr.span, body, cc, is_generator);
+ .analyze_closure(expr.id, expr.hir_id, expr.span, body, cc, gen);
}
_ => {}
span: Span,
body: &hir::Body,
capture_clause: hir::CaptureClause,
- is_generator: bool,
+ gen: Option<hir::GeneratorMovability>,
) {
/*!
* Analysis starting point.
}
};
- let infer_kind = if is_generator {
+ let infer_kind = if gen.is_some() {
false
} else {
self.closure_kind(closure_def_id, closure_substs).is_none()
NodeField(field) => icx.to_ty(&field.ty),
- NodeExpr(&hir::Expr { node: hir::ExprClosure(.., is_generator), .. }) => {
- if is_generator {
+ NodeExpr(&hir::Expr { node: hir::ExprClosure(.., gen), .. }) => {
+ if gen.is_some() {
let hir_id = tcx.hir.node_to_hir_id(node_id);
return tcx.typeck_tables_of(def_id).node_id_to_type(hir_id);
}
// argument position.
E0641, // cannot cast to/from a pointer with an unknown kind
E0645, // trait aliases not finished
+ E0907, // type inside generator must be known in this context
}
// types, where we use TyError as the Self type
}
+ ty::TyGeneratorWitness(..) |
ty::TyInfer(..) => {
bug!("unexpected type encountered in \
variance inference: {}",
ty::TyClosure(..) | ty::TyGenerator(..) => Tuple(vec![]), // FIXME(pcwalton)
+ ty::TyGeneratorWitness(..) => panic!("TyGeneratorWitness"),
ty::TyInfer(..) => panic!("TyInfer"),
ty::TyError => panic!("TyError"),
}
/// A closure (for example, `move |a, b, c| a + b + c`)
///
/// The final span is the span of the argument block `|...|`
- Closure(CaptureBy, P<FnDecl>, P<Expr>, Span),
+ Closure(CaptureBy, Movability, P<FnDecl>, P<Expr>, Span),
/// A block (`{ ... }`)
Block(P<Block>),
/// A catch block (`catch { ... }`)
Ref,
}
+/// The movability of a generator / closure literal
+#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
+pub enum Movability {
+ Static,
+ Movable,
+}
+
pub type Mac = Spanned<Mac_>;
/// Represents a macro invocation. The Path indicates which macro
fn_decl_span: Span) // span of the `|...|` part
-> P<ast::Expr> {
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
+ ast::Movability::Movable,
fn_decl,
body,
fn_decl_span))
// part of the lambda, but it probably (maybe?) corresponds to
// the entire lambda body. Probably we should extend the API
// here, but that's not entirely clear.
- self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, body, span))
+ self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
+ ast::Movability::Movable,
+ fn_decl,
+ body,
+ span))
}
fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
ExprKind::Match(folder.fold_expr(expr),
arms.move_map(|x| folder.fold_arm(x)))
}
- ExprKind::Closure(capture_clause, decl, body, span) => {
+ ExprKind::Closure(capture_clause, movability, decl, body, span) => {
ExprKind::Closure(capture_clause,
+ movability,
folder.fold_fn_decl(decl),
folder.fold_expr(body),
folder.new_span(span))
use ast::Unsafety;
use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind};
use ast::Block;
-use ast::{BlockCheckMode, CaptureBy};
+use ast::{BlockCheckMode, CaptureBy, Movability};
use ast::{Constness, Crate};
use ast::Defaultness;
use ast::EnumDef;
return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
}
token::BinOp(token::Or) | token::OrOr => {
- let lo = self.span;
- return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
+ return self.parse_lambda_expr(attrs);
}
token::OpenDelim(token::Bracket) => {
self.bump();
hi = path.span;
return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs));
}
- if self.eat_keyword(keywords::Move) {
- let lo = self.prev_span;
- return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
+ if self.check_keyword(keywords::Move) || self.check_keyword(keywords::Static) {
+ return self.parse_lambda_expr(attrs);
}
if self.eat_keyword(keywords::If) {
return self.parse_if_expr(attrs);
// `move |args| expr`
pub fn parse_lambda_expr(&mut self,
- lo: Span,
- capture_clause: CaptureBy,
attrs: ThinVec<Attribute>)
-> PResult<'a, P<Expr>>
{
+ let lo = self.span;
+ let movability = if self.eat_keyword(keywords::Static) {
+ Movability::Static
+ } else {
+ Movability::Movable
+ };
+ let capture_clause = if self.eat_keyword(keywords::Move) {
+ CaptureBy::Value
+ } else {
+ CaptureBy::Ref
+ };
let decl = self.parse_fn_block_decl()?;
let decl_hi = self.prev_span;
let body = match decl.output {
Ok(self.mk_expr(
lo.to(body.span),
- ExprKind::Closure(capture_clause, decl, body, lo.to(decl_hi)),
+ ExprKind::Closure(capture_clause, movability, decl, body, lo.to(decl_hi)),
attrs))
}
}
}
+ fn is_static_global(&mut self) -> bool {
+ if self.check_keyword(keywords::Static) {
+ // Check if this could be a closure
+ !self.look_ahead(1, |token| {
+ if token.is_keyword(keywords::Move) {
+ return true;
+ }
+ match *token {
+ token::BinOp(token::Or) | token::OrOr => true,
+ _ => false,
+ }
+ })
+ } else {
+ false
+ }
+ }
+
/// Parse one of the items allowed by the flags.
/// NB: this function no longer parses the items inside an
/// extern crate.
self.unexpected()?;
}
- if self.eat_keyword(keywords::Static) {
+ if self.is_static_global() {
+ self.bump();
// STATIC ITEM
let m = if self.eat_keyword(keywords::Mut) {
Mutability::Mutable
}
self.bclose_(expr.span, INDENT_UNIT)?;
}
- ast::ExprKind::Closure(capture_clause, ref decl, ref body, _) => {
+ ast::ExprKind::Closure(capture_clause, movability, ref decl, ref body, _) => {
+ self.print_movability(movability)?;
self.print_capture_clause(capture_clause)?;
self.print_fn_block_args(decl)?;
}
}
+ pub fn print_movability(&mut self, movability: ast::Movability)
+ -> io::Result<()> {
+ match movability {
+ ast::Movability::Static => self.word_space("static"),
+ ast::Movability::Movable => Ok(()),
+ }
+ }
+
pub fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy)
-> io::Result<()> {
match capture_clause {
visitor.visit_expr(subexpression);
walk_list!(visitor, visit_arm, arms);
}
- ExprKind::Closure(_, ref function_declaration, ref body, _decl_span) => {
+ ExprKind::Closure(_, _, ref function_declaration, ref body, _decl_span) => {
visitor.visit_fn(FnKind::Closure(body),
function_declaration,
expression.span,
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+ static || {};
+ //~^ ERROR closures cannot be static
+}
variadic: false,
});
iter_exprs(depth - 1, &mut |e| g(
- ExprKind::Closure(CaptureBy::Value, decl.clone(), e, DUMMY_SP)));
+ ExprKind::Closure(CaptureBy::Value,
+ Movability::Movable,
+ decl.clone(),
+ e,
+ DUMMY_SP)));
},
10 => {
iter_exprs(depth - 1, &mut |e| g(ExprKind::Assign(e, make_x())));
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(conservative_impl_trait, generators, generator_trait)]
+
+use std::ops::{ Generator, GeneratorState };
+
+fn foo(_: &str) -> String {
+ String::new()
+}
+
+fn bar(baz: String) -> impl Generator<Yield = String, Return = ()> {
+ move || {
+ yield foo(&baz);
+ }
+}
+
+fn foo2(_: &str) -> Result<String, ()> {
+ Err(())
+}
+
+fn bar2(baz: String) -> impl Generator<Yield = String, Return = ()> {
+ move || {
+ if let Ok(quux) = foo2(&baz) {
+ yield quux;
+ }
+ }
+}
+
+fn main() {
+ assert_eq!(bar(String::new()).resume(), GeneratorState::Yielded(String::new()));
+ assert_eq!(bar2(String::new()).resume(), GeneratorState::Complete(()));
+}
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(generators, generator_trait)]
+
+use std::ops::Generator;
+
+fn main() {
+ let b = |_| 3;
+ let mut a = || {
+ b(yield);
+ };
+ a.resume();
+}
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(generators)]
+#![feature(generator_trait)]
+
+use std::ops::Generator;
+use std::ops::GeneratorState;
+
+fn main() {
+ let _generator = || {
+ let mut sub_generator = || {
+ yield 2;
+ };
+
+ match sub_generator.resume() {
+ GeneratorState::Yielded(x) => {
+ yield x;
+ }
+ _ => panic!(),
+ };
+ };
+}
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(generators)]
+
+fn _run(bar: &mut i32) {
+ || {
+ {
+ let _baz = &*bar;
+ yield;
+ }
+
+ *bar = 2;
+ };
+}
+
+fn main() {}
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(generators, generator_trait)]
+
+use std::ops::{Generator, GeneratorState};
+
+fn main() {
+ let mut generator = static || {
+ let a = true;
+ let b = &a;
+ yield;
+ assert_eq!(b as *const _, &a as *const _);
+ };
+ assert_eq!(generator.resume(), GeneratorState::Yielded(()));
+ assert_eq!(generator.resume(), GeneratorState::Complete(()));
+}
\ No newline at end of file
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(generators)]
+#![feature(optin_builtin_traits)]
+
+auto trait Foo {}
+
+struct No;
+
+impl !Foo for No {}
+
+struct A<'a, 'b>(&'a mut bool, &'b mut bool, No);
+
+impl<'a, 'b: 'a> Foo for A<'a, 'b> {}
+
+struct OnlyFooIfStaticRef(No);
+impl Foo for &'static OnlyFooIfStaticRef {}
+
+struct OnlyFooIfRef(No);
+impl<'a> Foo for &'a OnlyFooIfRef {}
+
+fn assert_foo<T: Foo>(f: T) {}
+
+fn main() {
+ // Make sure 'static is erased for generator interiors so we can't match it in trait selection
+ let x: &'static _ = &OnlyFooIfStaticRef(No);
+ let gen = || {
+ let x = x;
+ yield;
+ assert_foo(x);
+ };
+ assert_foo(gen); //~ ERROR the trait bound `No: Foo` is not satisfied
+
+ // Allow impls which matches any lifetime
+ let x = &OnlyFooIfRef(No);
+ let gen = || {
+ let x = x;
+ yield;
+ assert_foo(x);
+ };
+ assert_foo(gen); // ok
+
+ // Disallow impls which relates lifetimes in the generator interior
+ let gen = || {
+ let a = A(&mut true, &mut true, No);
+ yield;
+ assert_foo(a);
+ };
+ assert_foo(gen); //~ ERROR the requirement `for<'r, 's> 'r : 's` is not satisfied
+}
--- /dev/null
+error[E0277]: the trait bound `No: Foo` is not satisfied in `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
+ --> $DIR/auto-trait-regions.rs:40:5
+ |
+40 | assert_foo(gen); //~ ERROR the trait bound `No: Foo` is not satisfied
+ | ^^^^^^^^^^ within `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`, the trait `Foo` is not implemented for `No`
+ |
+ = help: the following implementations were found:
+ <No as Foo>
+ = note: required because it appears within the type `OnlyFooIfStaticRef`
+ = note: required because it appears within the type `&OnlyFooIfStaticRef`
+ = note: required because it appears within the type `for<'r> {&'r OnlyFooIfStaticRef, ()}`
+ = note: required because it appears within the type `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
+note: required by `assert_foo`
+ --> $DIR/auto-trait-regions.rs:30:1
+ |
+30 | fn assert_foo<T: Foo>(f: T) {}
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error[E0279]: the requirement `for<'r, 's> 'r : 's` is not satisfied (`expected bound lifetime parameter, found concrete lifetime`)
+ --> $DIR/auto-trait-regions.rs:57:5
+ |
+57 | assert_foo(gen); //~ ERROR the requirement `for<'r, 's> 'r : 's` is not satisfied
+ | ^^^^^^^^^^
+ |
+ = note: required because of the requirements on the impl of `for<'r, 's> Foo` for `A<'_, '_>`
+ = note: required because it appears within the type `for<'r, 's> {A<'r, 's>, ()}`
+ = note: required because it appears within the type `[generator@$DIR/auto-trait-regions.rs:52:15: 56:6 for<'r, 's> {A<'r, 's>, ()}]`
+note: required by `assert_foo`
+ --> $DIR/auto-trait-regions.rs:30:1
+ |
+30 | fn assert_foo<T: Foo>(f: T) {}
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
17 | fn assert_send<T: Send>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied in `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`
+error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied in `[generator@$DIR/not-send-sync.rs:19:17: 23:6 {std::cell::Cell<i32>, ()}]`
--> $DIR/not-send-sync.rs:19:5
|
19 | assert_sync(|| {
| ^^^^^^^^^^^ `std::cell::Cell<i32>` cannot be shared between threads safely
|
- = help: within `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
- = note: required because it appears within the type `(std::cell::Cell<i32>, ())`
- = note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`
+ = help: within `[generator@$DIR/not-send-sync.rs:19:17: 23:6 {std::cell::Cell<i32>, ()}]`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
+ = note: required because it appears within the type `{std::cell::Cell<i32>, ()}`
+ = note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:19:17: 23:6 {std::cell::Cell<i32>, ()}]`
note: required by `main::assert_sync`
--> $DIR/not-send-sync.rs:16:5
|