Skip to content

Commit 45e431c

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

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
@@ -438,6 +438,21 @@ impl<'hir> Map<'hir> {
438438
}
439439
}
440440

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

src/librustc_mir/build/expr/as_rvalue.rs

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

0 commit comments

Comments
 (0)