@@ -24,9 +24,7 @@ use session::Session;
24
24
use hir:: def:: { Def , DefMap } ;
25
25
use hir:: def_id:: DefId ;
26
26
use middle:: region;
27
- use ty:: subst;
28
27
use ty;
29
- use std:: fmt;
30
28
use std:: mem:: replace;
31
29
use syntax:: ast;
32
30
use syntax:: parse:: token:: keywords;
@@ -41,8 +39,7 @@ use hir::intravisit::{self, Visitor, FnKind};
41
39
#[ derive( Clone , Copy , PartialEq , Eq , Hash , RustcEncodable , RustcDecodable , Debug ) ]
42
40
pub enum DefRegion {
43
41
DefStaticRegion ,
44
- DefEarlyBoundRegion ( /* space */ subst:: ParamSpace ,
45
- /* index */ u32 ,
42
+ DefEarlyBoundRegion ( /* index */ u32 ,
46
43
/* lifetime decl */ ast:: NodeId ) ,
47
44
DefLateBoundRegion ( ty:: DebruijnIndex ,
48
45
/* lifetime decl */ ast:: NodeId ) ,
@@ -90,10 +87,11 @@ struct LifetimeContext<'a, 'tcx: 'a> {
90
87
labels_in_fn : Vec < ( ast:: Name , Span ) > ,
91
88
}
92
89
90
+ #[ derive( PartialEq , Debug ) ]
93
91
enum ScopeChain < ' a > {
94
- /// EarlyScope(i, ['a, 'b, ...], s) extends s with early-bound
95
- /// lifetimes, assigning indexes 'a => i, 'b => i+1, ... etc .
96
- EarlyScope ( subst :: ParamSpace , & ' a [ hir:: LifetimeDef ] , Scope < ' a > ) ,
92
+ /// EarlyScope(['a, 'b, ...], s) extends s with early-bound
93
+ /// lifetimes.
94
+ EarlyScope ( & ' a [ hir:: LifetimeDef ] , Scope < ' a > ) ,
97
95
/// LateScope(['a, 'b, ...], s) extends s with late-bound
98
96
/// lifetimes introduced by the declaration binder_id.
99
97
LateScope ( & ' a [ hir:: LifetimeDef ] , Scope < ' a > ) ,
@@ -159,8 +157,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
159
157
hir:: ItemImpl ( _, _, ref generics, _, _, _) => {
160
158
// These kinds of items have only early bound lifetime parameters.
161
159
let lifetimes = & generics. lifetimes ;
162
- let early_scope = EarlyScope ( subst:: TypeSpace , lifetimes, & ROOT_SCOPE ) ;
163
- this. with ( early_scope, |old_scope, this| {
160
+ this. with ( EarlyScope ( lifetimes, & ROOT_SCOPE ) , |old_scope, this| {
164
161
this. check_lifetime_defs ( old_scope, lifetimes) ;
165
162
intravisit:: walk_item ( this, item) ;
166
163
} ) ;
@@ -181,11 +178,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
181
178
self . with ( RootScope , |_, this| {
182
179
match item. node {
183
180
hir:: ForeignItemFn ( ref decl, ref generics) => {
184
- this. visit_early_late ( item. id ,
185
- subst:: FnSpace ,
186
- decl,
187
- generics,
188
- |this| {
181
+ this. visit_early_late ( item. id , decl, generics, |this| {
189
182
intravisit:: walk_foreign_item ( this, item) ;
190
183
} )
191
184
}
@@ -203,14 +196,13 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
203
196
b : & ' v hir:: Block , s : Span , fn_id : ast:: NodeId ) {
204
197
match fk {
205
198
FnKind :: ItemFn ( _, generics, _, _, _, _, _) => {
206
- self . visit_early_late ( fn_id, subst :: FnSpace , decl, generics, |this| {
199
+ self . visit_early_late ( fn_id, decl, generics, |this| {
207
200
this. add_scope_and_walk_fn ( fk, decl, b, s, fn_id)
208
201
} )
209
202
}
210
203
FnKind :: Method ( _, sig, _, _) => {
211
204
self . visit_early_late (
212
205
fn_id,
213
- subst:: FnSpace ,
214
206
decl,
215
207
& sig. generics ,
216
208
|this| this. add_scope_and_walk_fn ( fk, decl, b, s, fn_id) ) ;
@@ -263,7 +255,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
263
255
264
256
if let hir:: MethodTraitItem ( ref sig, None ) = trait_item. node {
265
257
self . visit_early_late (
266
- trait_item. id , subst :: FnSpace ,
258
+ trait_item. id ,
267
259
& sig. decl , & sig. generics ,
268
260
|this| intravisit:: walk_trait_item ( this, trait_item) )
269
261
} else {
@@ -469,7 +461,7 @@ fn extract_labels(ctxt: &mut LifetimeContext, b: &hir::Block) {
469
461
FnScope { s, .. } => { scope = s; }
470
462
RootScope => { return ; }
471
463
472
- EarlyScope ( _ , lifetimes, s) |
464
+ EarlyScope ( lifetimes, s) |
473
465
LateScope ( lifetimes, s) => {
474
466
for lifetime_def in lifetimes {
475
467
// FIXME (#24278): non-hygienic comparison
@@ -557,7 +549,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
557
549
/// ordering is not important there.
558
550
fn visit_early_late < F > ( & mut self ,
559
551
fn_id : ast:: NodeId ,
560
- early_space : subst:: ParamSpace ,
561
552
decl : & hir:: FnDecl ,
562
553
generics : & hir:: Generics ,
563
554
walk : F ) where
@@ -576,7 +567,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
576
567
. partition ( |l| self . map . late_bound . contains_key ( & l. lifetime . id ) ) ;
577
568
578
569
let this = self ;
579
- this. with ( EarlyScope ( early_space , & early, this. scope ) , move |old_scope, this| {
570
+ this. with ( EarlyScope ( & early, this. scope ) , move |old_scope, this| {
580
571
this. with ( LateScope ( & late, this. scope ) , move |_, this| {
581
572
this. check_lifetime_defs ( old_scope, & generics. lifetimes ) ;
582
573
walk ( this) ;
@@ -606,11 +597,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
606
597
break ;
607
598
}
608
599
609
- EarlyScope ( space , lifetimes, s) => {
600
+ EarlyScope ( lifetimes, s) => {
610
601
match search_lifetimes ( lifetimes, lifetime_ref) {
611
- Some ( ( index, lifetime_def) ) => {
602
+ Some ( ( mut index, lifetime_def) ) => {
603
+ // Adjust for nested early scopes, e.g. in methods.
604
+ let mut parent = s;
605
+ while let EarlyScope ( lifetimes, s) = * parent {
606
+ index += lifetimes. len ( ) as u32 ;
607
+ parent = s;
608
+ }
609
+ assert_eq ! ( * parent, RootScope ) ;
610
+
612
611
let decl_id = lifetime_def. id ;
613
- let def = DefEarlyBoundRegion ( space , index, decl_id) ;
612
+ let def = DefEarlyBoundRegion ( index, decl_id) ;
614
613
self . insert_lifetime ( lifetime_ref, def) ;
615
614
return ;
616
615
}
@@ -672,7 +671,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
672
671
break ;
673
672
}
674
673
675
- EarlyScope ( _ , lifetimes, s) |
674
+ EarlyScope ( lifetimes, s) |
676
675
LateScope ( lifetimes, s) => {
677
676
search_result = search_lifetimes ( lifetimes, lifetime_ref) ;
678
677
if search_result. is_some ( ) {
@@ -768,7 +767,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
768
767
return ;
769
768
}
770
769
771
- EarlyScope ( _ , lifetimes, s) |
770
+ EarlyScope ( lifetimes, s) |
772
771
LateScope ( lifetimes, s) => {
773
772
if let Some ( ( _, lifetime_def) ) = search_lifetimes ( lifetimes, lifetime) {
774
773
signal_shadowing_problem (
@@ -963,14 +962,3 @@ fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
963
962
}
964
963
}
965
964
}
966
-
967
- impl < ' a > fmt:: Debug for ScopeChain < ' a > {
968
- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
969
- match * self {
970
- EarlyScope ( space, defs, _) => write ! ( fmt, "EarlyScope({:?}, {:?})" , space, defs) ,
971
- LateScope ( defs, _) => write ! ( fmt, "LateScope({:?})" , defs) ,
972
- FnScope { fn_id, body_id, s : _ } => write ! ( fmt, "FnScope({:?}, {:?})" , fn_id, body_id) ,
973
- RootScope => write ! ( fmt, "RootScope" ) ,
974
- }
975
- }
976
- }
0 commit comments