@@ -32,8 +32,7 @@ use syntax::codemap::DUMMY_SP;
32
32
use ty:: TyCtxt ;
33
33
use ty:: maps:: Providers ;
34
34
35
- use hir;
36
- use hir:: def_id:: { CrateNum , LOCAL_CRATE } ;
35
+ use hir; use hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
37
36
use hir:: intravisit:: { self , Visitor , FnKind , NestedVisitorMap } ;
38
37
use hir:: { Block , Item , FnDecl , Arm , Pat , PatKind , Stmt , Expr , Local } ;
39
38
@@ -347,7 +346,11 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
347
346
/// arbitrary amounts of stack space. Terminating scopes end
348
347
/// up being contained in a DestructionScope that contains the
349
348
/// destructor's execution.
350
- terminating_scopes : NodeSet
349
+ terminating_scopes : NodeSet ,
350
+
351
+ target_fn_id : DefId ,
352
+
353
+ found_target_fn : bool ,
351
354
}
352
355
353
356
@@ -1139,50 +1142,54 @@ fn resolve_fn<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'tcx, 'a>,
1139
1142
body_id : hir:: BodyId ,
1140
1143
sp : Span ,
1141
1144
id : ast:: NodeId ) {
1142
- debug ! ( "region::resolve_fn(id={:?}, \
1143
- span={:?}, \
1144
- body.id={:?}, \
1145
- cx.parent={:?})",
1146
- id,
1147
- visitor. sess. codemap( ) . span_to_string( sp) ,
1148
- body_id,
1149
- visitor. cx. parent) ;
1150
1145
1151
1146
visitor. cx . parent = visitor. new_code_extent (
1152
1147
CodeExtentData :: CallSiteScope { fn_id : id, body_id : body_id. node_id } ) ;
1153
1148
1154
- let fn_decl_scope = visitor. new_code_extent (
1155
- CodeExtentData :: ParameterScope { fn_id : id, body_id : body_id. node_id } ) ;
1156
-
1157
- if let Some ( root_id) = visitor. cx . root_id {
1158
- visitor. region_maps . record_fn_parent ( body_id. node_id , root_id) ;
1149
+ if body_id == visitor. target_fn_id {
1150
+ // We've found the top level `fn`. Store it and its children in the `RegionMaps`
1151
+ visitor. found_target_fn = true ;
1159
1152
}
1160
1153
1161
- let outer_cx = visitor. cx ;
1162
- let outer_ts = mem:: replace ( & mut visitor. terminating_scopes , NodeSet ( ) ) ;
1163
- visitor. terminating_scopes . insert ( body_id. node_id ) ;
1164
-
1165
- // The arguments and `self` are parented to the fn.
1166
- visitor. cx = Context {
1167
- root_id : Some ( body_id. node_id ) ,
1168
- parent : ROOT_CODE_EXTENT ,
1169
- var_parent : fn_decl_scope,
1170
- } ;
1154
+ if visitor. found_target_fn {
1155
+ debug ! ( "region::resolve_fn(id={:?}, \
1156
+ span={:?}, \
1157
+ body.id={:?}, \
1158
+ cx.parent={:?})",
1159
+ id,
1160
+ visitor. sess. codemap( ) . span_to_string( sp) ,
1161
+ body_id,
1162
+ visitor. cx. parent) ;
1163
+
1164
+ let fn_decl_scope = visitor. new_code_extent (
1165
+ CodeExtentData :: ParameterScope { fn_id : id, body_id : body_id. node_id } ) ;
1166
+
1167
+ let outer_cx = visitor. cx ;
1168
+ let outer_ts = mem:: replace ( & mut visitor. terminating_scopes , NodeSet ( ) ) ;
1169
+ visitor. terminating_scopes . insert ( body_id. node_id ) ;
1170
+
1171
+ // The arguments and `self` are parented to the fn.
1172
+ visitor. cx = Context {
1173
+ root_id : Some ( body_id. node_id ) ,
1174
+ parent : ROOT_CODE_EXTENT ,
1175
+ var_parent : fn_decl_scope,
1176
+ } ;
1171
1177
1172
- intravisit:: walk_fn_decl ( visitor, decl) ;
1173
- intravisit:: walk_fn_kind ( visitor, kind) ;
1178
+ intravisit:: walk_fn_decl ( visitor, decl) ;
1179
+ intravisit:: walk_fn_kind ( visitor, kind) ;
1174
1180
1175
- // The body of the every fn is a root scope.
1176
- visitor. cx = Context {
1177
- root_id : Some ( body_id. node_id ) ,
1178
- parent : fn_decl_scope,
1179
- var_parent : fn_decl_scope
1180
- } ;
1181
- visitor. visit_nested_body ( body_id) ;
1181
+ // The body of the every fn is a root scope.
1182
+ visitor. cx = Context {
1183
+ root_id : Some ( body_id. node_id ) ,
1184
+ parent : fn_decl_scope,
1185
+ var_parent : fn_decl_scope
1186
+ } ;
1187
+ visitor. visit_nested_body ( body_id) ;
1182
1188
1183
- // Restore context we had at the start.
1184
- visitor. cx = outer_cx;
1185
- visitor. terminating_scopes = outer_ts;
1189
+ // Restore context we had at the start.
1190
+ visitor. cx = outer_cx;
1191
+ visitor. terminating_scopes = outer_ts;
1192
+ }
1186
1193
}
1187
1194
1188
1195
impl < ' hir , ' a > RegionResolutionVisitor < ' hir , ' a > {
@@ -1264,14 +1271,12 @@ impl<'hir, 'a> Visitor<'hir> for RegionResolutionVisitor<'hir, 'a> {
1264
1271
}
1265
1272
1266
1273
pub fn resolve_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) -> Rc < RegionMaps > {
1267
- ty:: queries:: region_resolve_crate :: get ( tcx, DUMMY_SP , LOCAL_CRATE )
1274
+ ty:: queries:: region_resolve_fn :: get ( tcx, DUMMY_SP , LOCAL_CRATE )
1268
1275
}
1269
1276
1270
- fn region_resolve_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , crate_num : CrateNum )
1277
+ fn region_resolve_fn < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , fn_id : DefId )
1271
1278
-> Rc < RegionMaps >
1272
1279
{
1273
- debug_assert ! ( crate_num == LOCAL_CRATE ) ;
1274
-
1275
1280
let sess = & tcx. sess ;
1276
1281
let hir_map = & tcx. hir ;
1277
1282
@@ -1302,16 +1307,19 @@ fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateN
1302
1307
parent : ROOT_CODE_EXTENT ,
1303
1308
var_parent : ROOT_CODE_EXTENT
1304
1309
} ,
1305
- terminating_scopes : NodeSet ( )
1310
+ terminating_scopes : NodeSet ( ) ,
1311
+ target_fn_id : fn_id,
1312
+ found_target_fn : false ,
1306
1313
} ;
1307
1314
krate. visit_all_item_likes ( & mut visitor. as_deep_visitor ( ) ) ;
1315
+ debug_assert ! ( visitor. found_target_fn) ;
1308
1316
}
1309
1317
Rc :: new ( maps)
1310
1318
}
1311
1319
1312
1320
pub fn provide ( providers : & mut Providers ) {
1313
1321
* providers = Providers {
1314
- region_resolve_crate ,
1322
+ region_resolve_fn ,
1315
1323
..* providers
1316
1324
} ;
1317
1325
}
0 commit comments