@@ -42,7 +42,7 @@ pub enum DefRegion {
4242 /* lifetime decl */ ast:: NodeId ) ,
4343 DefLateBoundRegion ( ty:: DebruijnIndex ,
4444 /* lifetime decl */ ast:: NodeId ) ,
45- DefFreeRegion ( /* block scope */ region:: DestructionScopeData ,
45+ DefFreeRegion ( region:: CallSiteScopeData ,
4646 /* lifetime decl */ ast:: NodeId ) ,
4747}
4848
@@ -83,9 +83,9 @@ enum ScopeChain<'a> {
8383 /// LateScope(['a, 'b, ...], s) extends s with late-bound
8484 /// lifetimes introduced by the declaration binder_id.
8585 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 > } ,
8989 RootScope
9090}
9191
@@ -172,20 +172,20 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
172172 }
173173
174174 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 ) {
176176 match fk {
177177 FnKind :: ItemFn ( _, generics, _, _, _, _) => {
178178 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 )
180180 } )
181181 }
182182 FnKind :: Method ( _, sig, _) => {
183183 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 )
185185 } )
186186 }
187187 FnKind :: Closure => {
188- self . walk_fn ( fk, fd, b, s)
188+ self . add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
189189 }
190190 }
191191 }
@@ -236,12 +236,6 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
236236 replace ( & mut self . labels_in_fn , saved) ;
237237 }
238238
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-
245239 fn visit_lifetime ( & mut self , lifetime_ref : & hir:: Lifetime ) {
246240 if lifetime_ref. name == special_idents:: static_lifetime. name {
247241 self . insert_lifetime ( lifetime_ref, DefStaticRegion ) ;
@@ -437,7 +431,7 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
437431 label_span : Span ) {
438432 loop {
439433 match * scope {
440- BlockScope ( _ , s ) => { scope = s; }
434+ FnScope { s , .. } => { scope = s; }
441435 RootScope => { return ; }
442436
443437 EarlyScope ( _, lifetimes, s) |
@@ -461,14 +455,13 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
461455}
462456
463457impl < ' 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+
472465 match fk {
473466 FnKind :: ItemFn ( _, generics, _, _, _, _) => {
474467 intravisit:: walk_fn_decl ( self , fd) ;
@@ -488,7 +481,8 @@ impl<'a> LifetimeContext<'a> {
488481 // `self.labels_in_fn`.
489482 extract_labels ( self , fb) ;
490483
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) )
492486 }
493487
494488 fn with < F > ( & mut self , wrap_scope : ScopeChain , f : F ) where
@@ -559,8 +553,11 @@ impl<'a> LifetimeContext<'a> {
559553 let mut scope = self . scope ;
560554 loop {
561555 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) ;
564561 }
565562
566563 RootScope => {
@@ -604,7 +601,7 @@ impl<'a> LifetimeContext<'a> {
604601 }
605602
606603 fn resolve_free_lifetime_ref ( & mut self ,
607- scope_data : region:: DestructionScopeData ,
604+ scope_data : region:: CallSiteScopeData ,
608605 lifetime_ref : & hir:: Lifetime ,
609606 scope : Scope ) {
610607 debug ! ( "resolve_free_lifetime_ref \
@@ -622,8 +619,10 @@ impl<'a> LifetimeContext<'a> {
622619 scope_data: {:?} scope: {:?} search_result: {:?}",
623620 scope_data, scope, search_result) ;
624621 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+ } ;
627626 scope = s;
628627 }
629628
@@ -711,7 +710,7 @@ impl<'a> LifetimeContext<'a> {
711710
712711 loop {
713712 match * old_scope {
714- BlockScope ( _ , s ) => {
713+ FnScope { s , .. } => {
715714 old_scope = s;
716715 }
717716
@@ -864,7 +863,7 @@ impl<'a> fmt::Debug for ScopeChain<'a> {
864863 match * self {
865864 EarlyScope ( space, defs, _) => write ! ( fmt, "EarlyScope({:?}, {:?})" , space, defs) ,
866865 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 ) ,
868867 RootScope => write ! ( fmt, "RootScope" ) ,
869868 }
870869 }
0 commit comments