Skip to content

Commit dba2ca8

Browse files
Zoxcarielb1
authored andcommitted
Sanity check the Expr visitation count
1 parent 3a511e0 commit dba2ca8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/librustc/middle/region.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ pub struct ScopeTree {
252252
/// stores the `Span` of the last one and the number of expressions
253253
/// which came before it in a generator body.
254254
yield_in_scope: FxHashMap<Scope, (Span, usize)>,
255+
256+
/// The number of visit_expr calls done in the body.
257+
/// Used to sanity check visit_expr call count when
258+
/// calculating geneartor interiors.
259+
body_expr_count: FxHashMap<hir::BodyId, usize>,
255260
}
256261

257262
#[derive(Debug, Copy, Clone)]
@@ -619,6 +624,13 @@ impl<'tcx> ScopeTree {
619624
pub fn yield_in_scope(&self, scope: Scope) -> Option<(Span, usize)> {
620625
self.yield_in_scope.get(&scope).cloned()
621626
}
627+
628+
/// Gives the number of expressions visited in a body.
629+
/// Used to sanity check visit_expr call count when
630+
/// calculating geneartor interiors.
631+
pub fn body_expr_count(&self, body_id: hir::BodyId) -> Option<usize> {
632+
self.body_expr_count.get(&body_id).map(|r| *r)
633+
}
622634
}
623635

624636
/// Records the lifetime of a local variable as `cx.var_parent`
@@ -1166,6 +1178,10 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
11661178
resolve_local(self, None, Some(&body.value));
11671179
}
11681180

1181+
if body.is_generator {
1182+
self.scope_tree.body_expr_count.insert(body_id, self.expr_count);
1183+
}
1184+
11691185
// Restore context we had at the start.
11701186
self.expr_count = outer_ec;
11711187
self.cx = outer_cx;

src/librustc_typeck/check/generator_interior.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ pub fn resolve_interior<'a, 'gcx, 'tcx>(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
7777
};
7878
intravisit::walk_body(&mut visitor, body);
7979

80+
// Check that we visited the same amount of expressions and the RegionResolutionVisitor
81+
let region_expr_count = visitor.region_scope_tree.body_expr_count(body_id).unwrap();
82+
assert_eq!(region_expr_count, visitor.expr_count);
83+
8084
let mut types: Vec<_> = visitor.types.drain().collect();
8185

8286
// Sort types by insertion order

0 commit comments

Comments
 (0)