Adds support for immovable generators. Move checking of invalid borrows across suspension points to borrowck. Fixes #44197, #45259 and #45093.

This commit is contained in:
John Kåre Alsaker 2017-10-07 16:36:28 +02:00
parent 47a8eb7c4e
commit ccf0d8399e
60 changed files with 863 additions and 213 deletions

View File

@ -2118,4 +2118,6 @@ register_diagnostics! {
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
}

View File

@ -2768,7 +2768,7 @@ impl<'a> LoweringContext<'a> {
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;
@ -2777,16 +2777,28 @@ impl<'a> LoweringContext<'a> {
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)
})
})
}

View File

@ -1290,7 +1290,7 @@ pub enum Expr_ {
///
/// 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>),
@ -1466,6 +1466,12 @@ pub struct Destination {
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,

View File

@ -606,6 +606,11 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::MatchSource {
}
}
impl_stable_hash_for!(enum hir::GeneratorMovability {
Static,
Movable
});
impl_stable_hash_for!(enum hir::CaptureClause {
CaptureByValue,
CaptureByRef

View File

@ -431,7 +431,7 @@ for ::middle::const_val::ErrKind<'gcx> {
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,
@ -656,6 +656,9 @@ for ty::TypeVariants<'gcx>
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);

View File

@ -60,7 +60,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
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();

View File

@ -192,6 +192,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::TyForeign(..) |
ty::TyParam(..) |
ty::TyClosure(..) |
ty::TyGeneratorWitness(..) |
ty::TyAnon(..) => {
t.super_fold_with(self)
}

View File

@ -453,6 +453,43 @@ struct RegionResolutionVisitor<'a, 'tcx: 'a> {
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>) {
@ -612,6 +649,20 @@ impl<'tcx> ScopeTree {
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,
@ -768,6 +819,28 @@ impl<'tcx> ScopeTree {
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.
@ -872,9 +945,13 @@ fn resolve_pat<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, pat: &
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) {
@ -897,7 +974,7 @@ fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, 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);
@ -982,6 +1059,8 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
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);
@ -1077,12 +1156,13 @@ fn resolve_local<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>,
}
}
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:
///

View File

@ -451,7 +451,10 @@ fn ty_is_local_constructor(ty: Ty, in_crate: InCrate) -> bool {
true
}
ty::TyClosure(..) | ty::TyGenerator(..) | ty::TyAnon(..) => {
ty::TyClosure(..) |
ty::TyGenerator(..) |
ty::TyGeneratorWitness(..) |
ty::TyAnon(..) => {
bug!("ty_is_local invoked on unexpected type: {:?}", ty)
}
}

View File

@ -262,6 +262,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
},
ty::TyGenerator(..) => Some(18),
ty::TyForeign(..) => Some(19),
ty::TyGeneratorWitness(..) => Some(20),
ty::TyInfer(..) | ty::TyError => None
}
}

View File

@ -2044,8 +2044,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
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()))
}
@ -2095,7 +2095,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
}
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
}
@ -2206,8 +2206,14 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
}
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`

View File

@ -1672,8 +1672,9 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
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());
@ -2079,6 +2080,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
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))
}

View File

@ -227,6 +227,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
}
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(),

View File

@ -46,6 +46,7 @@ pub enum SimplifiedTypeGen<D>
TraitSimplifiedType(D),
ClosureSimplifiedType(D),
GeneratorSimplifiedType(D),
GeneratorWitnessSimplifiedType(usize),
AnonSimplifiedType(D),
FunctionSimplifiedType(usize),
ParameterSimplifiedType,
@ -92,6 +93,9 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
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()))
@ -141,6 +145,7 @@ impl<D: Copy + Debug + Ord + Eq + Hash> SimplifiedTypeGen<D> {
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,
@ -175,6 +180,7 @@ impl<'gcx, D> HashStable<StableHashingContext<'gcx>> for SimplifiedTypeGen<D>
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),

View File

@ -94,6 +94,12 @@ impl FlagComputation {
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);

View File

@ -375,6 +375,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
ty::TyAnon(..) |
ty::TyInfer(_) |
ty::TyError |
ty::TyGeneratorWitness(..) |
ty::TyNever |
ty::TyFloat(_) => None,
}

View File

@ -1678,7 +1678,7 @@ impl<'a, 'tcx> LayoutDetails {
ty::TyParam(_) => {
return Err(LayoutError::Unknown(ty));
}
ty::TyInfer(_) | ty::TyError => {
ty::TyGeneratorWitness(..) | ty::TyInfer(_) | ty::TyError => {
bug!("LayoutDetails::compute: unexpected type `{}`", ty)
}
})
@ -2151,8 +2151,9 @@ impl<'a, 'tcx> TyLayout<'tcx> {
ty::TyFnPtr(_) |
ty::TyNever |
ty::TyFnDef(..) |
ty::TyDynamic(..) |
ty::TyForeign(..) => {
ty::TyGeneratorWitness(..) |
ty::TyForeign(..) |
ty::TyDynamic(..) => {
bug!("TyLayout::field_type({:?}): not applicable", self)
}

View File

@ -1880,7 +1880,12 @@ impl<'a, 'gcx, 'tcx> AdtDef {
vec![]
}
TyStr | TyDynamic(..) | TySlice(_) | TyForeign(..) | TyError => {
TyStr |
TyDynamic(..) |
TySlice(_) |
TyForeign(..) |
TyError |
TyGeneratorWitness(..) => {
// these are never sized - return the target type
vec![ty]
}

View File

@ -79,16 +79,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}
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) => {

View File

@ -18,6 +18,7 @@ use middle::const_val::ConstVal;
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;
@ -319,6 +320,33 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
}
}
#[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>,
@ -410,6 +438,17 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
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 =>
@ -575,8 +614,9 @@ impl<'tcx> Relate<'tcx> for ty::GeneratorInterior<'tcx> {
-> 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 })
}
}

View File

@ -438,7 +438,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorInterior<'a> {
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 }
})
}
}
@ -798,6 +798,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
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)),
@ -832,6 +833,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
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),
@ -928,7 +930,10 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
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 {

View File

@ -141,6 +141,10 @@ pub enum TypeVariants<'tcx> {
/// `|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,
@ -405,19 +409,7 @@ impl<'a, 'gcx, 'tcx> ClosureSubsts<'tcx> {
#[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)]
@ -1611,6 +1603,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
}
TyFnDef(..) |
TyFnPtr(_) |
TyGeneratorWitness(..) |
TyBool |
TyChar |
TyInt(_) |

View File

@ -29,7 +29,6 @@ use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
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};
@ -550,7 +549,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
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())
}
@ -572,8 +572,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}).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()
}
@ -783,6 +786,9 @@ impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
self.def_id(d);
}
}
TyGeneratorWitness(tys) => {
self.hash(tys.skip_binder().len());
}
TyTuple(tys, defaulted) => {
self.hash(tys.len());
self.hash(defaulted);
@ -1139,7 +1145,7 @@ fn needs_drop_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// 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

View File

@ -119,8 +119,11 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
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());

View File

@ -283,6 +283,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
ty::TyFloat(..) |
ty::TyError |
ty::TyStr |
ty::TyGeneratorWitness(..) |
ty::TyNever |
ty::TyParam(_) |
ty::TyForeign(..) => {

View File

@ -17,7 +17,7 @@ use ty::{BrAnon, BrEnv, BrFresh, BrNamed};
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;
@ -631,6 +631,22 @@ impl<'tcx> fmt::Debug for ty::UpvarBorrow<'tcx> {
}
}
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 {
@ -1066,7 +1082,11 @@ define_print! {
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))?;
@ -1097,6 +1117,9 @@ define_print! {
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")?;

View File

@ -25,7 +25,7 @@ use rustc::middle::expr_use_visitor::MutateMode;
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;
@ -92,6 +92,7 @@ struct CheckLoanCtxt<'a, 'tcx: 'a> {
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> {
@ -147,6 +148,8 @@ 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,
@ -198,6 +201,16 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
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,
@ -205,6 +218,7 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
move_data,
all_loans,
param_env,
movable_generator,
};
let rvalue_promotable_map = bccx.tcx.rvalue_promotable_map(def_id);
euv::ExprUseVisitor::new(&mut clcx,
@ -348,6 +362,91 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
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

View File

@ -906,73 +906,6 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}
};
// 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",

View File

@ -639,6 +639,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
ty::TyError |
ty::TyClosure(..) |
ty::TyGenerator(..) |
ty::TyGeneratorWitness(..) |
ty::TyProjection(..) |
ty::TyAnon(..) |
ty::TyFnDef(..) => bug!("Unexpected type in foreign function"),

View File

@ -514,6 +514,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
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"),

View File

@ -424,6 +424,7 @@ impl<'a, 'tcx> DefPathBasedNames<'a, 'tcx> {
ty::TyInfer(_) |
ty::TyProjection(..) |
ty::TyParam(_) |
ty::TyGeneratorWitness(_) |
ty::TyAnon(..) => {
bug!("DefPathBasedNames: Trying to create type name for \
unexpected type: {:?}", t);

View File

@ -332,9 +332,39 @@ impl<'tcx> Visitor<'tcx> for StorageIgnored {
}
}
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());
@ -347,8 +377,11 @@ fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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,
});
@ -372,6 +405,12 @@ fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// 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]);
@ -390,6 +429,7 @@ fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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)>,
@ -397,11 +437,17 @@ fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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
@ -411,7 +457,7 @@ fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// 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 {}",
@ -763,19 +809,18 @@ impl MirPass for StateTransform {
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);
@ -790,7 +835,7 @@ impl MirPass for StateTransform {
// 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();

View File

@ -397,17 +397,17 @@ impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
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) {
@ -416,15 +416,15 @@ impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
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));
}
_ => {}

View File

@ -1602,7 +1602,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
_ => 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());

View File

@ -173,6 +173,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
ty::TyInfer(_) |
ty::TyProjection(..) |
ty::TyAnon(..) |
ty::TyGeneratorWitness(..) |
ty::TyParam(_) => {
bug!("debuginfo: Trying to create type name for \
unexpected type: {:?}", t);

View File

@ -130,7 +130,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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 => {

View File

@ -37,6 +37,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
_capture: hir::CaptureClause,
decl: &'gcx hir::FnDecl,
body_id: hir::BodyId,
gen: Option<hir::GeneratorMovability>,
expected: Expectation<'tcx>,
) -> Ty<'tcx> {
debug!(
@ -53,7 +54,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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(
@ -62,6 +63,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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!(
@ -86,7 +88,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
decl,
expr.id,
body,
true,
gen,
).1;
// Create type variables (for now) to represent the transformed

View File

@ -17,8 +17,9 @@ use rustc::hir::def_id::DefId;
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;
@ -30,36 +31,53 @@ struct InteriorVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
}
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);
debug!("type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
expr, scope, ty, self.expr_count, yield_span);
// Map the type to the number of types added before it
let entries = self.types.len();
self.types.entry(&ty).or_insert(entries);
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));
}
}
}
@ -67,7 +85,7 @@ impl<'a, 'gcx, 'tcx> InteriorVisitor<'a, 'gcx, 'tcx> {
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,
@ -87,17 +105,40 @@ pub fn resolve_interior<'a, 'gcx, 'tcx>(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
types.sort_by_key(|t| t.1);
// Extract type components
let types: Vec<_> = types.into_iter().map(|t| t.0).collect();
let type_list = fcx.tcx.mk_type_list(types.into_iter().map(|t| t.0));
let tuple = fcx.tcx.intern_tup(&types, false);
// 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 = {:?}", tuple, body.value.span);
debug!("Types in generator {:?}, span = {:?}", type_list, body.value.span);
// Unify the tuple with the witness
match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(witness, tuple) {
// 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
@ -112,7 +153,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'gcx, 'tcx> {
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;
@ -128,6 +169,6 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'gcx, 'tcx> {
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);
}
}

View File

@ -209,7 +209,7 @@ pub struct Inherited<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
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
@ -838,7 +838,7 @@ fn typeck_tables_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
@ -973,7 +973,7 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
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();
@ -999,7 +999,7 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
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)));
}
@ -1023,17 +1023,24 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
}
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
@ -2113,9 +2120,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
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);
}
}
@ -3854,8 +3862,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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)

View File

@ -74,11 +74,11 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for InferBorrowKindVisitor<'a, 'gcx, 'tcx> {
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);
}
_ => {}
@ -96,7 +96,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
span: Span,
body: &hir::Body,
capture_clause: hir::CaptureClause,
is_generator: bool,
gen: Option<hir::GeneratorMovability>,
) {
/*!
* Analysis starting point.
@ -121,7 +121,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
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()

View File

@ -1141,8 +1141,8 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
}

View File

@ -4776,4 +4776,5 @@ register_diagnostics! {
// 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
}

View File

@ -377,6 +377,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
// types, where we use TyError as the Self type
}
ty::TyGeneratorWitness(..) |
ty::TyInfer(..) => {
bug!("unexpected type encountered in \
variance inference: {}",

View File

@ -2314,6 +2314,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
ty::TyClosure(..) | ty::TyGenerator(..) => Tuple(vec![]), // FIXME(pcwalton)
ty::TyGeneratorWitness(..) => panic!("TyGeneratorWitness"),
ty::TyInfer(..) => panic!("TyInfer"),
ty::TyError => panic!("TyError"),
}

View File

@ -1100,7 +1100,7 @@ pub enum ExprKind {
/// 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 { ... }`)
@ -1194,6 +1194,13 @@ pub enum CaptureBy {
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

View File

@ -912,6 +912,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
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))
@ -930,7 +931,11 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
// 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> {

View File

@ -1235,8 +1235,9 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
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))

View File

@ -14,7 +14,7 @@ use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
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;
@ -2258,8 +2258,7 @@ impl<'a> Parser<'a> {
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();
@ -2304,9 +2303,8 @@ impl<'a> Parser<'a> {
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);
@ -3247,11 +3245,20 @@ impl<'a> Parser<'a> {
// `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 {
@ -3269,7 +3276,7 @@ impl<'a> Parser<'a> {
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))
}
@ -6271,6 +6278,23 @@ impl<'a> Parser<'a> {
}
}
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.
@ -6329,7 +6353,8 @@ impl<'a> Parser<'a> {
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

View File

@ -2162,7 +2162,8 @@ impl<'a> State<'a> {
}
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)?;
@ -2777,6 +2778,14 @@ impl<'a> State<'a> {
}
}
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 {

View File

@ -739,7 +739,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
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,

View File

@ -0,0 +1,14 @@
// 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
}

View File

@ -142,7 +142,11 @@ fn iter_exprs(depth: usize, f: &mut FnMut(P<Expr>)) {
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())));

View File

@ -0,0 +1,40 @@
// 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(()));
}

View File

@ -0,0 +1,21 @@
// 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();
}

View File

@ -0,0 +1,30 @@
// 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!(),
};
};
}

View File

@ -0,0 +1,24 @@
// 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() {}

View File

@ -0,0 +1,24 @@
// 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(()));
}

View File

@ -0,0 +1,58 @@
// 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
}

View File

@ -0,0 +1,35 @@
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

View File

@ -13,15 +13,15 @@ note: required by `main::assert_send`
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
|