@@ -42,7 +42,7 @@ pub enum DefRegion {
42
42
/* lifetime decl */ ast:: NodeId ) ,
43
43
DefLateBoundRegion ( ty:: DebruijnIndex ,
44
44
/* lifetime decl */ ast:: NodeId ) ,
45
- DefFreeRegion ( /* block scope */ region:: DestructionScopeData ,
45
+ DefFreeRegion ( region:: CallSiteScopeData ,
46
46
/* lifetime decl */ ast:: NodeId ) ,
47
47
}
48
48
@@ -83,9 +83,9 @@ enum ScopeChain<'a> {
83
83
/// LateScope(['a, 'b, ...], s) extends s with late-bound
84
84
/// lifetimes introduced by the declaration binder_id.
85
85
LateScope ( & ' a Vec < hir:: LifetimeDef > , Scope < ' a > ) ,
86
- /// lifetimes introduced by items within a code block are scoped
87
- /// to that block .
88
- BlockScope ( region :: DestructionScopeData , Scope < ' a > ) ,
86
+
87
+ /// lifetimes introduced by a fn are scoped to the call-site for that fn .
88
+ FnScope { fn_id : ast :: NodeId , body_id : ast :: NodeId , s : Scope < ' a > } ,
89
89
RootScope
90
90
}
91
91
@@ -172,20 +172,20 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
172
172
}
173
173
174
174
fn visit_fn ( & mut self , fk : FnKind < ' v > , fd : & ' v hir:: FnDecl ,
175
- b : & ' v hir:: Block , s : Span , _ : ast:: NodeId ) {
175
+ b : & ' v hir:: Block , s : Span , fn_id : ast:: NodeId ) {
176
176
match fk {
177
177
FnKind :: ItemFn ( _, generics, _, _, _, _) => {
178
178
self . visit_early_late ( subst:: FnSpace , generics, |this| {
179
- this. walk_fn ( fk, fd, b, s)
179
+ this. add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
180
180
} )
181
181
}
182
182
FnKind :: Method ( _, sig, _) => {
183
183
self . visit_early_late ( subst:: FnSpace , & sig. generics , |this| {
184
- this. walk_fn ( fk, fd, b, s)
184
+ this. add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
185
185
} )
186
186
}
187
187
FnKind :: Closure => {
188
- self . walk_fn ( fk, fd, b, s)
188
+ self . add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
189
189
}
190
190
}
191
191
}
@@ -236,12 +236,6 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
236
236
replace ( & mut self . labels_in_fn , saved) ;
237
237
}
238
238
239
- fn visit_block ( & mut self , b : & hir:: Block ) {
240
- self . with ( BlockScope ( region:: DestructionScopeData :: new ( b. id ) ,
241
- self . scope ) ,
242
- |_, this| intravisit:: walk_block ( this, b) ) ;
243
- }
244
-
245
239
fn visit_lifetime ( & mut self , lifetime_ref : & hir:: Lifetime ) {
246
240
if lifetime_ref. name == special_idents:: static_lifetime. name {
247
241
self . insert_lifetime ( lifetime_ref, DefStaticRegion ) ;
@@ -437,7 +431,7 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
437
431
label_span : Span ) {
438
432
loop {
439
433
match * scope {
440
- BlockScope ( _ , s ) => { scope = s; }
434
+ FnScope { s , .. } => { scope = s; }
441
435
RootScope => { return ; }
442
436
443
437
EarlyScope ( _, lifetimes, s) |
@@ -461,14 +455,13 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
461
455
}
462
456
463
457
impl < ' a > LifetimeContext < ' a > {
464
- // This is just like intravisit::walk_fn, except that it extracts the
465
- // labels of the function body and swaps them in before visiting
466
- // the function body itself.
467
- fn walk_fn < ' b > ( & mut self ,
468
- fk : FnKind ,
469
- fd : & hir:: FnDecl ,
470
- fb : & ' b hir:: Block ,
471
- _span : Span ) {
458
+ fn add_scope_and_walk_fn < ' b > ( & mut self ,
459
+ fk : FnKind ,
460
+ fd : & hir:: FnDecl ,
461
+ fb : & ' b hir:: Block ,
462
+ _span : Span ,
463
+ fn_id : ast:: NodeId ) {
464
+
472
465
match fk {
473
466
FnKind :: ItemFn ( _, generics, _, _, _, _) => {
474
467
intravisit:: walk_fn_decl ( self , fd) ;
@@ -488,7 +481,8 @@ impl<'a> LifetimeContext<'a> {
488
481
// `self.labels_in_fn`.
489
482
extract_labels ( self , fb) ;
490
483
491
- self . visit_block ( fb) ;
484
+ self . with ( FnScope { fn_id : fn_id, body_id : fb. id , s : self . scope } ,
485
+ |_old_scope, this| this. visit_block ( fb) )
492
486
}
493
487
494
488
fn with < F > ( & mut self , wrap_scope : ScopeChain , f : F ) where
@@ -559,8 +553,11 @@ impl<'a> LifetimeContext<'a> {
559
553
let mut scope = self . scope ;
560
554
loop {
561
555
match * scope {
562
- BlockScope ( blk_scope, s) => {
563
- return self . resolve_free_lifetime_ref ( blk_scope, lifetime_ref, s) ;
556
+ FnScope { fn_id, body_id, s } => {
557
+ return self . resolve_free_lifetime_ref (
558
+ region:: CallSiteScopeData { fn_id : fn_id, body_id : body_id } ,
559
+ lifetime_ref,
560
+ s) ;
564
561
}
565
562
566
563
RootScope => {
@@ -604,7 +601,7 @@ impl<'a> LifetimeContext<'a> {
604
601
}
605
602
606
603
fn resolve_free_lifetime_ref ( & mut self ,
607
- scope_data : region:: DestructionScopeData ,
604
+ scope_data : region:: CallSiteScopeData ,
608
605
lifetime_ref : & hir:: Lifetime ,
609
606
scope : Scope ) {
610
607
debug ! ( "resolve_free_lifetime_ref \
@@ -622,8 +619,10 @@ impl<'a> LifetimeContext<'a> {
622
619
scope_data: {:?} scope: {:?} search_result: {:?}",
623
620
scope_data, scope, search_result) ;
624
621
match * scope {
625
- BlockScope ( blk_scope_data, s) => {
626
- scope_data = blk_scope_data;
622
+ FnScope { fn_id, body_id, s } => {
623
+ scope_data = region:: CallSiteScopeData {
624
+ fn_id : fn_id, body_id : body_id
625
+ } ;
627
626
scope = s;
628
627
}
629
628
@@ -711,7 +710,7 @@ impl<'a> LifetimeContext<'a> {
711
710
712
711
loop {
713
712
match * old_scope {
714
- BlockScope ( _ , s ) => {
713
+ FnScope { s , .. } => {
715
714
old_scope = s;
716
715
}
717
716
@@ -864,7 +863,7 @@ impl<'a> fmt::Debug for ScopeChain<'a> {
864
863
match * self {
865
864
EarlyScope ( space, defs, _) => write ! ( fmt, "EarlyScope({:?}, {:?})" , space, defs) ,
866
865
LateScope ( defs, _) => write ! ( fmt, "LateScope({:?})" , defs) ,
867
- BlockScope ( id , _ ) => write ! ( fmt, "BlockScope ({:?})" , id ) ,
866
+ FnScope { fn_id , body_id , s : _ } => write ! ( fmt, "FnScope ({:?}, {:?} )" , fn_id , body_id ) ,
868
867
RootScope => write ! ( fmt, "RootScope" ) ,
869
868
}
870
869
}
0 commit comments