@@ -70,8 +70,30 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
70
70
}
71
71
ExprKind :: Borrow { region, borrow_kind, arg } => {
72
72
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
+ }
74
95
}
96
+
75
97
let arg_lvalue = unpack ! ( block = this. as_lvalue( block, arg) ) ;
76
98
block. and ( Rvalue :: Ref ( region, borrow_kind, arg_lvalue) )
77
99
}
0 commit comments