wrap expr id into GeneratorInteriorTypeCause

This commit is contained in:
csmoe 2020-01-15 15:13:51 +08:00
parent 5ad8b9e394
commit 4eb47ded54
5 changed files with 23 additions and 18 deletions

View File

@ -2456,7 +2456,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let target_span = tables
.generator_interior_types
.iter()
.find(|(ty::GeneratorInteriorTypeCause { ty, .. }, _)| {
.find(|ty::GeneratorInteriorTypeCause { ty, .. }| {
// Careful: the regions for types that appear in the
// generator interior are not generally known, so we
// want to erase them when comparing (and anyway,
@ -2479,7 +2479,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
);
eq
})
.map(|(ty::GeneratorInteriorTypeCause { span, scope_span, .. }, expr)| {
.map(|ty::GeneratorInteriorTypeCause { span, scope_span, expr, .. }| {
(span, source_map.span_to_snippet(*span), scope_span, expr)
});
@ -2585,8 +2585,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
};
// Look at the last interior type to get a span for the `.await`.
let await_span =
tables.generator_interior_types.iter().map(|(i, _)| i.span).last().unwrap();
let await_span = tables.generator_interior_types.iter().map(|t| t.span).last().unwrap();
let mut span = MultiSpan::from_span(await_span);
span.push_span_label(
await_span,

View File

@ -315,8 +315,7 @@ pub struct ResolvedOpaqueTy<'tcx> {
///
/// Here, we would store the type `T`, the span of the value `x`, and the "scope-span" for
/// the scope that contains `x`.
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, Eq, Hash, PartialEq)]
#[derive(HashStable, TypeFoldable)]
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, Eq, Hash, PartialEq, HashStable)]
pub struct GeneratorInteriorTypeCause<'tcx> {
/// Type of the captured binding.
pub ty: Ty<'tcx>,
@ -324,6 +323,8 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
pub span: Span,
/// Span of the scope of the captured binding.
pub scope_span: Option<Span>,
/// Expr which the type evaluated from.
pub expr: Option<hir::HirId>,
}
#[derive(RustcEncodable, RustcDecodable, Debug)]
@ -438,7 +439,7 @@ pub struct TypeckTables<'tcx> {
/// Stores the type, expression, span and optional scope span of all types
/// that are live across the yield of this generator (if a generator).
pub generator_interior_types: Vec<(GeneratorInteriorTypeCause<'tcx>, Option<hir::HirId>)>,
pub generator_interior_types: Vec<GeneratorInteriorTypeCause<'tcx>>,
}
impl<'tcx> TypeckTables<'tcx> {

View File

@ -18,7 +18,6 @@ use rustc_span::Span;
struct InteriorVisitor<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
types: FxHashMap<ty::GeneratorInteriorTypeCause<'tcx>, usize>,
exprs: Vec<Option<hir::HirId>>,
region_scope_tree: &'tcx region::ScopeTree,
expr_count: usize,
kind: hir::GeneratorKind,
@ -98,9 +97,9 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
span: source_span,
ty: &ty,
scope_span,
expr: expr.map(|e| e.hir_id),
})
.or_insert(entries);
self.exprs.push(expr.map(|e| e.hir_id));
}
} else {
debug!(
@ -138,7 +137,6 @@ pub fn resolve_interior<'a, 'tcx>(
expr_count: 0,
kind,
prev_unresolved_span: None,
exprs: vec![],
};
intravisit::walk_body(&mut visitor, body);
@ -167,18 +165,25 @@ pub fn resolve_interior<'a, 'tcx>(
// which means that none of the regions inside relate to any other, even if
// typeck had previously found constraints that would cause them to be related.
let mut counter = 0;
let types = fcx.tcx.fold_regions(&types, &mut false, |_, current_depth| {
let fold_types: Vec<_> = types.iter().map(|(t, _)| t.ty).collect();
let folded_types = fcx.tcx.fold_regions(&fold_types, &mut false, |_, current_depth| {
counter += 1;
fcx.tcx.mk_region(ty::ReLateBound(current_depth, ty::BrAnon(counter)))
});
// Store the generator types and spans into the tables for this generator.
let interior_types =
types.iter().zip(visitor.exprs).map(|(t, e)| (t.0.clone(), e)).collect::<Vec<_>>();
visitor.fcx.inh.tables.borrow_mut().generator_interior_types = interior_types;
let types = types
.into_iter()
.zip(&folded_types)
.map(|((mut interior_cause, _), ty)| {
interior_cause.ty = ty;
interior_cause
})
.collect();
visitor.fcx.inh.tables.borrow_mut().generator_interior_types = types;
// Extract type components
let type_list = fcx.tcx.mk_type_list(types.into_iter().map(|t| (t.0).ty));
let type_list = fcx.tcx.mk_type_list(folded_types.iter());
let witness = fcx.tcx.mk_generator_witness(ty::Binder::bind(type_list));

View File

@ -20,7 +20,7 @@ LL | fn assert_sync<T: Sync>(_: T) {}
LL | assert_sync(|| {
| ^^^^^^^^^^^ future returned by `main` is not `Sync`
|
= help: within `[generator@$DIR/not-send-sync.rs:9:17: 13:6 {std::cell::Cell<i32>, ()}]`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
= help: within `[generator@$DIR/not-send-sync.rs:9:17: 13:6 {std::cell::Cell<i32>, (), ()}]`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
note: future is not `Sync` as this value is used across an yield
--> $DIR/not-send-sync.rs:12:9
|

View File

@ -76,7 +76,7 @@ error[E0720]: opaque type expands to a recursive type
LL | fn generator_capture() -> impl Sized {
| ^^^^^^^^^^ expands to a recursive type
|
= note: expanded type is `[generator@$DIR/recursive-impl-trait-type-indirect.rs:50:5: 50:26 x:impl Sized {()}]`
= note: expanded type is `[generator@$DIR/recursive-impl-trait-type-indirect.rs:50:5: 50:26 x:impl Sized {(), ()}]`
error[E0720]: opaque type expands to a recursive type
--> $DIR/recursive-impl-trait-type-indirect.rs:53:26
@ -92,7 +92,7 @@ error[E0720]: opaque type expands to a recursive type
LL | fn generator_hold() -> impl Sized {
| ^^^^^^^^^^ expands to a recursive type
|
= note: expanded type is `[generator@$DIR/recursive-impl-trait-type-indirect.rs:58:5: 62:6 {impl Sized, ()}]`
= note: expanded type is `[generator@$DIR/recursive-impl-trait-type-indirect.rs:58:5: 62:6 {impl Sized, (), ()}]`
error[E0720]: opaque type expands to a recursive type
--> $DIR/recursive-impl-trait-type-indirect.rs:69:26