Skip to content

Commit 5c0feb8

Browse files
committed
add proofs and fix postorder traversal
I don't think the "quasi-postorder" travesal could cause any issues, but there's no reason for it to stay broken.
1 parent 0bb3dc1 commit 5c0feb8

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@
2727
//! for more details.
2828
//!
2929
//! Note: it is an important invariant that the default visitor walks
30-
//! the body of a function in "execution order" (more concretely,
31-
//! reverse post-order with respect to the CFG implied by the AST),
32-
//! meaning that if AST node A may execute before AST node B, then A
33-
//! is visited first. The borrow checker in particular relies on this
34-
//! property.
30+
//! the body of a function in "execution order" - more concretely, if
31+
//! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
32+
//! then a pre-order traversal of the HIR is consistent with the CFG RPO
33+
//! on the *initial CFG point* of each HIR node, while a post-order traversal
34+
//! of the HIR is consistent with the CFG RPO on each *final CFG point* of
35+
//! each CFG node.
36+
//!
37+
//! One thing that follows is that if HIR node A always starts/ends executing
38+
//! before HIR node B, then A appears in traversal pre/postorder before B,
39+
//! respectively. (This follows from RPO respecting CFG domination).
40+
//!
41+
//! This order consistency is required in a few places in rustc, for
42+
//! example generator inference, and possibly also HIR borrowck.
3543
3644
use syntax::abi::Abi;
3745
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};

src/librustc/middle/region.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,36 @@ pub struct ScopeTree {
249249
closure_tree: FxHashMap<hir::ItemLocalId, hir::ItemLocalId>,
250250

251251
/// If there are any `yield` nested within a scope, this map
252-
/// stores the `Span` of the last one and the number of expressions
253-
/// which came before it in a generator body.
252+
/// stores the `Span` of the last one and its index in the
253+
/// postorder of the Visitor traversal on the HIR.
254+
///
255+
/// HIR Visitor postorder indexes might seem like a peculiar
256+
/// thing to care about. but it turns out that HIR bindings
257+
/// and the temporary results of HIR expressions are never
258+
/// storage-live at the end of HIR nodes with postorder indexes
259+
/// lower than theirs, and therefore don't need to be suspended
260+
/// at yield-points at these indexes.
261+
///
262+
/// Let's show that: let `D` be our binding/temporary and `U` be our
263+
/// other HIR node, with `HIR-postorder(U) < HIR-postorder(D)`.
264+
///
265+
/// Then:
266+
/// 1. From the ordering guarantee of HIR visitors (see
267+
/// `rustc::hir::intravisit`), `D` does not dominate `U`.
268+
/// 2. Therefore, `D` is *potentially* storage-dead at `U` (because
269+
/// we might visit `U` without ever getting to `D`).
270+
/// 3. However, we guarantee that at each HIR point, each
271+
/// binding/temporary is always either always storage-live
272+
/// or always storage-dead. This is what is being guaranteed
273+
/// by `terminating_scopes` including all blocks where the
274+
/// count of executions is not guaranteed.
275+
/// 4. By `2.` and `3.`, `D` is *statically* dead at `U`,
276+
/// QED.
277+
///
278+
/// I don't think this property relies on `3.` in an essential way - it
279+
/// is probably still correct even if we have "unrestricted" terminating
280+
/// scopes. However, why use the complicated proof when a simple one
281+
/// works?
254282
yield_in_scope: FxHashMap<Scope, (Span, usize)>,
255283

256284
/// The number of visit_expr calls done in the body.
@@ -754,8 +782,6 @@ fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, stmt:
754782
fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr: &'tcx hir::Expr) {
755783
debug!("resolve_expr(expr.id={:?})", expr.id);
756784

757-
visitor.expr_count += 1;
758-
759785
let prev_cx = visitor.cx;
760786
visitor.enter_node_scope_with_dtor(expr.hir_id.local_id);
761787

@@ -837,6 +863,8 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
837863
_ => intravisit::walk_expr(visitor, expr)
838864
}
839865

866+
visitor.expr_count += 1;
867+
840868
if let hir::ExprYield(..) = expr.node {
841869
// Mark this expr's scope and all parent scopes as containing `yield`.
842870
let mut scope = Scope::Node(expr.hir_id.local_id);

src/librustc_typeck/check/generator_interior.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ impl<'a, 'gcx, 'tcx> InteriorVisitor<'a, 'gcx, 'tcx> {
3535

3636
let live_across_yield = scope.map_or(Some(DUMMY_SP), |s| {
3737
self.region_scope_tree.yield_in_scope(s).and_then(|(span, expr_count)| {
38-
// Check if the span in the region comes after the expression
38+
// If we are recording an expression that is the last yield
39+
// in the scope, or that has a postorder CFG index larger
40+
// than the one of all of the yields, then its value can't
41+
// be storage-live (and therefore live) at any of the yields.
42+
//
43+
// See the mega-comment at `yield_in_scope` for a proof.
3944
if expr.is_none() || expr_count >= self.expr_count {
4045
Some(span)
4146
} else {
@@ -114,14 +119,13 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'gcx, 'tcx> {
114119
}
115120

116121
fn visit_expr(&mut self, expr: &'tcx Expr) {
122+
intravisit::walk_expr(self, expr);
123+
117124
self.expr_count += 1;
118125

119126
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
120127

121-
122128
let ty = self.fcx.tables.borrow().expr_ty_adjusted(expr);
123129
self.record(ty, scope, Some(expr));
124-
125-
intravisit::walk_expr(self, expr);
126130
}
127131
}

src/test/ui/generator/not-send-sync.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not s
99
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:26:17: 30:6 a:&std::cell::Cell<i32> _]`
1010
= note: required by `main::assert_send`
1111

12-
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>)]`
12+
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>, ())]`
1313
--> $DIR/not-send-sync.rs:19:5
1414
|
1515
19 | assert_sync(|| {
1616
| ^^^^^^^^^^^ `std::cell::Cell<i32>` cannot be shared between threads safely
1717
|
18-
= 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>`
19-
= note: required because it appears within the type `((), std::cell::Cell<i32>)`
20-
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:19:17: 23:6 ((), std::cell::Cell<i32>)]`
18+
= 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>`
19+
= note: required because it appears within the type `(std::cell::Cell<i32>, ())`
20+
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`
2121
= note: required by `main::assert_sync`
2222

2323
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)