@@ -67,8 +67,30 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
67
67
}
68
68
ExprKind :: Borrow { region, borrow_kind, arg } => {
69
69
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
+ }
71
92
}
93
+
72
94
let arg_lvalue = unpack ! ( block = this. as_lvalue( block, arg) ) ;
73
95
block. and ( Rvalue :: Ref ( region, borrow_kind, arg_lvalue) )
74
96
}
0 commit comments