@@ -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
@@ -173,20 +173,20 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
173173 }
174174
175175 fn visit_fn ( & mut self , fk : FnKind < ' v > , fd : & ' v hir:: FnDecl ,
176- b : & ' v hir:: Block , s : Span , _ : ast:: NodeId ) {
176+ b : & ' v hir:: Block , s : Span , fn_id : ast:: NodeId ) {
177177 match fk {
178178 FnKind :: ItemFn ( _, generics, _, _, _, _) => {
179179 self . visit_early_late ( subst:: FnSpace , generics, |this| {
180- this. walk_fn ( fk, fd, b, s)
180+ this. add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
181181 } )
182182 }
183183 FnKind :: Method ( _, sig, _) => {
184184 self . visit_early_late ( subst:: FnSpace , & sig. generics , |this| {
185- this. walk_fn ( fk, fd, b, s)
185+ this. add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
186186 } )
187187 }
188188 FnKind :: Closure => {
189- self . walk_fn ( fk, fd, b, s)
189+ self . add_scope_and_walk_fn ( fk, fd, b, s, fn_id )
190190 }
191191 }
192192 }
@@ -237,12 +237,6 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
237237 replace ( & mut self . labels_in_fn , saved) ;
238238 }
239239
240- fn visit_block ( & mut self , b : & hir:: Block ) {
241- self . with ( BlockScope ( region:: DestructionScopeData :: new ( b. id ) ,
242- self . scope ) ,
243- |_, this| intravisit:: walk_block ( this, b) ) ;
244- }
245-
246240 fn visit_lifetime ( & mut self , lifetime_ref : & hir:: Lifetime ) {
247241 if lifetime_ref. name == special_idents:: static_lifetime. name {
248242 self . insert_lifetime ( lifetime_ref, DefStaticRegion ) ;
@@ -438,7 +432,7 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
438432 label_span : Span ) {
439433 loop {
440434 match * scope {
441- BlockScope ( _ , s ) => { scope = s; }
435+ FnScope { s , .. } => { scope = s; }
442436 RootScope => { return ; }
443437
444438 EarlyScope ( _, lifetimes, s) |
@@ -462,14 +456,13 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
462456}
463457
464458impl < ' a > LifetimeContext < ' a > {
465- // This is just like intravisit::walk_fn, except that it extracts the
466- // labels of the function body and swaps them in before visiting
467- // the function body itself.
468- fn walk_fn < ' b > ( & mut self ,
469- fk : FnKind ,
470- fd : & hir:: FnDecl ,
471- fb : & ' b hir:: Block ,
472- _span : Span ) {
459+ fn add_scope_and_walk_fn < ' b > ( & mut self ,
460+ fk : FnKind ,
461+ fd : & hir:: FnDecl ,
462+ fb : & ' b hir:: Block ,
463+ _span : Span ,
464+ fn_id : ast:: NodeId ) {
465+
473466 match fk {
474467 FnKind :: ItemFn ( _, generics, _, _, _, _) => {
475468 intravisit:: walk_fn_decl ( self , fd) ;
@@ -489,7 +482,8 @@ impl<'a> LifetimeContext<'a> {
489482 // `self.labels_in_fn`.
490483 extract_labels ( self , fb) ;
491484
492- self . visit_block ( fb) ;
485+ self . with ( FnScope { fn_id : fn_id, body_id : fb. id , s : self . scope } ,
486+ |_old_scope, this| this. visit_block ( fb) )
493487 }
494488
495489 fn with < F > ( & mut self , wrap_scope : ScopeChain , f : F ) where
@@ -560,8 +554,11 @@ impl<'a> LifetimeContext<'a> {
560554 let mut scope = self . scope ;
561555 loop {
562556 match * scope {
563- BlockScope ( blk_scope, s) => {
564- return self . resolve_free_lifetime_ref ( blk_scope, lifetime_ref, s) ;
557+ FnScope { fn_id, body_id, s } => {
558+ return self . resolve_free_lifetime_ref (
559+ region:: CallSiteScopeData { fn_id : fn_id, body_id : body_id } ,
560+ lifetime_ref,
561+ s) ;
565562 }
566563
567564 RootScope => {
@@ -605,7 +602,7 @@ impl<'a> LifetimeContext<'a> {
605602 }
606603
607604 fn resolve_free_lifetime_ref ( & mut self ,
608- scope_data : region:: DestructionScopeData ,
605+ scope_data : region:: CallSiteScopeData ,
609606 lifetime_ref : & hir:: Lifetime ,
610607 scope : Scope ) {
611608 debug ! ( "resolve_free_lifetime_ref \
@@ -623,8 +620,10 @@ impl<'a> LifetimeContext<'a> {
623620 scope_data: {:?} scope: {:?} search_result: {:?}",
624621 scope_data, scope, search_result) ;
625622 match * scope {
626- BlockScope ( blk_scope_data, s) => {
627- scope_data = blk_scope_data;
623+ FnScope { fn_id, body_id, s } => {
624+ scope_data = region:: CallSiteScopeData {
625+ fn_id : fn_id, body_id : body_id
626+ } ;
628627 scope = s;
629628 }
630629
@@ -712,7 +711,7 @@ impl<'a> LifetimeContext<'a> {
712711
713712 loop {
714713 match * old_scope {
715- BlockScope ( _ , s ) => {
714+ FnScope { s , .. } => {
716715 old_scope = s;
717716 }
718717
@@ -865,7 +864,7 @@ impl<'a> fmt::Debug for ScopeChain<'a> {
865864 match * self {
866865 EarlyScope ( space, defs, _) => write ! ( fmt, "EarlyScope({:?}, {:?})" , space, defs) ,
867866 LateScope ( defs, _) => write ! ( fmt, "LateScope({:?})" , defs) ,
868- BlockScope ( id , _ ) => write ! ( fmt, "BlockScope ({:?})" , id ) ,
867+ FnScope { fn_id , body_id , s : _ } => write ! ( fmt, "FnScope ({:?}, {:?} )" , fn_id , body_id ) ,
869868 RootScope => write ! ( fmt, "RootScope" ) ,
870869 }
871870 }
0 commit comments