Skip to content

Commit 31a2296

Browse files
committed
RE-ORDERME treat borrows of "free-regions" specially, rather than struggle with the overall infrastructure right now.
1 parent b6f3508 commit 31a2296

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,21 @@ impl<'hir> Map<'hir> {
433433
}
434434
}
435435

436+
/// Returns `(owner, body)` where `owner` is nearest closure or
437+
/// item that contains `node_id`, and `body` is owner's body.
438+
pub fn node_owner(&self, mut node_id: NodeId) -> (NodeId, NodeId) {
439+
let mut parent = self.get_parent_node(node_id);
440+
assert!(parent != node_id); // if parent == node_id { return parent; }
441+
loop {
442+
if self.map[parent.as_usize()].is_body_owner(node_id) {
443+
return (parent, node_id);
444+
}
445+
node_id = parent;
446+
parent = self.get_parent_node(node_id);
447+
assert!(node_id != parent);
448+
}
449+
}
450+
436451
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
437452
self.local_def_id(self.body_owner(id))
438453
}

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,30 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7070
}
7171
ExprKind::Borrow { region, borrow_kind, arg } => {
7272
if let Region::ReScope(extent) = *region {
73-
this.seen_borrows.insert(extent);
73+
let tcx = this.hir.tcx();
74+
75+
// Find extent for the body of the immediately enclosing closure or fn.
76+
let expr_code_extent = match expr.temp_lifetime {
77+
None => panic!("should not see ReScope borrows outside of closures/fns"),
78+
Some(expr_code_extent) => expr_code_extent,
79+
};
80+
let expr_id = expr_code_extent.node_id(&tcx.region_maps);
81+
let (_owner_id, body_id) = tcx.hir.node_owner(expr_id);
82+
// FIXME: should this be destruction scope of
83+
// `body_id` (or a CallSiteScope)?
84+
let body_extent = tcx.region_maps.node_extent(body_id);
85+
assert!(tcx.region_maps.is_subscope_of(expr_code_extent, body_extent));
86+
87+
if tcx.region_maps.is_subscope_of(extent, body_extent) {
88+
// extent of borrow ends during this execution
89+
this.seen_borrows.insert(extent);
90+
this.mark_borrowed(extent, expr_span);
91+
} else {
92+
// must be borrow of region outside closure; do nothing.
93+
debug!("extent {:?} is outside of body extent: {:?}", extent, body_extent);
94+
}
7495
}
96+
7597
let arg_lvalue = unpack!(block = this.as_lvalue(block, arg));
7698
block.and(Rvalue::Ref(region, borrow_kind, arg_lvalue))
7799
}

0 commit comments

Comments
 (0)