@@ -244,12 +244,7 @@ enum Scope<'a> {
244
244
/// of the resulting opaque type.
245
245
opaque_type_parent : bool ,
246
246
247
- /// True only if this `Binder` scope is from the quantifiers on a
248
- /// `PolyTraitRef`. This is necessary for `associated_type_bounds`, which
249
- /// requires binders of nested trait refs to be merged.
250
- from_poly_trait_ref : bool ,
251
-
252
- binder_depth : u32 ,
247
+ scope_type : BinderScopeType ,
253
248
254
249
/// The late bound vars for a given item are stored by `HirId` to be
255
250
/// queried later. However, if we enter an elision scope, we have to
@@ -335,6 +330,13 @@ enum Scope<'a> {
335
330
Root ,
336
331
}
337
332
333
+ #[ derive( Copy , Clone , Debug ) ]
334
+ enum BinderScopeType {
335
+ Other ,
336
+ PolyTraitRef ,
337
+ Concatenating ,
338
+ }
339
+
338
340
// A helper struct for debugging scopes without printing parent scopes
339
341
struct TruncatedScopeDebug < ' a > ( & ' a Scope < ' a > ) ;
340
342
@@ -346,8 +348,7 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
346
348
next_early_index,
347
349
track_lifetime_uses,
348
350
opaque_type_parent,
349
- from_poly_trait_ref,
350
- binder_depth,
351
+ scope_type,
351
352
hir_id,
352
353
s : _,
353
354
} => f
@@ -356,8 +357,7 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
356
357
. field ( "next_early_index" , next_early_index)
357
358
. field ( "track_lifetime_uses" , track_lifetime_uses)
358
359
. field ( "opaque_type_parent" , opaque_type_parent)
359
- . field ( "from_poly_trait_ref" , from_poly_trait_ref)
360
- . field ( "binder_depth" , binder_depth)
360
+ . field ( "scope_type" , scope_type)
361
361
. field ( "hir_id" , hir_id)
362
362
. field ( "s" , & ".." )
363
363
. finish ( ) ,
@@ -622,48 +622,6 @@ fn late_region_as_bound_region<'tcx>(tcx: TyCtxt<'tcx>, region: &Region) -> ty::
622
622
}
623
623
}
624
624
625
- impl < ' a , ' tcx > LifetimeContext < ' a , ' tcx > {
626
- fn depth ( & self , concanetate : bool ) -> u32 {
627
- let mut passed_boundary = false ;
628
- let mut scope = self . scope ;
629
- loop {
630
- match * scope {
631
- Scope :: Root => {
632
- break 0 ;
633
- }
634
-
635
- Scope :: TraitRefBoundary { s, .. } => {
636
- passed_boundary = true ;
637
- scope = s;
638
- }
639
-
640
- Scope :: Binder { binder_depth, from_poly_trait_ref, .. } => {
641
- if concanetate && !passed_boundary && !from_poly_trait_ref {
642
- bug ! ( "{:?}" , self . scope) ;
643
- }
644
- break if concanetate {
645
- if passed_boundary || !from_poly_trait_ref {
646
- binder_depth + 1
647
- } else {
648
- binder_depth
649
- }
650
- } else {
651
- binder_depth + 1
652
- } ;
653
- }
654
-
655
- Scope :: Elision { s, .. }
656
- | Scope :: ObjectLifetimeDefault { s, .. }
657
- | Scope :: TraitRefHackInner { s, .. }
658
- | Scope :: Supertrait { s, .. }
659
- | Scope :: Body { s, .. } => {
660
- scope = s;
661
- }
662
- }
663
- }
664
- }
665
- }
666
-
667
625
impl < ' a , ' tcx > Visitor < ' tcx > for LifetimeContext < ' a , ' tcx > {
668
626
type Map = Map < ' tcx > ;
669
627
@@ -721,8 +679,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
721
679
s : self . scope ,
722
680
track_lifetime_uses : true ,
723
681
opaque_type_parent : false ,
724
- from_poly_trait_ref : false ,
725
- binder_depth : self . depth ( false ) ,
682
+ scope_type : BinderScopeType :: Other ,
726
683
} ;
727
684
self . with ( scope, move |_old_scope, this| {
728
685
intravisit:: walk_fn ( this, fk, fd, b, s, hir_id)
@@ -847,8 +804,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
847
804
next_early_index : index + non_lifetime_count,
848
805
opaque_type_parent : true ,
849
806
track_lifetime_uses,
850
- from_poly_trait_ref : false ,
851
- binder_depth : self . depth ( false ) ,
807
+ scope_type : BinderScopeType :: Other ,
852
808
s : ROOT_SCOPE ,
853
809
} ;
854
810
self . with ( scope, |old_scope, this| {
@@ -920,8 +876,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
920
876
next_early_index,
921
877
track_lifetime_uses : true ,
922
878
opaque_type_parent : false ,
923
- from_poly_trait_ref : false ,
924
- binder_depth : self . depth ( false ) ,
879
+ scope_type : BinderScopeType :: Other ,
925
880
} ;
926
881
self . with ( scope, |old_scope, this| {
927
882
// a bare fn has no bounds, so everything
@@ -1117,8 +1072,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1117
1072
s : this. scope ,
1118
1073
track_lifetime_uses : true ,
1119
1074
opaque_type_parent : false ,
1120
- from_poly_trait_ref : false ,
1121
- binder_depth : this. depth ( false ) ,
1075
+ scope_type : BinderScopeType :: Other ,
1122
1076
} ;
1123
1077
this. with ( scope, |_old_scope, this| {
1124
1078
this. visit_generics ( generics) ;
@@ -1138,8 +1092,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1138
1092
s : self . scope ,
1139
1093
track_lifetime_uses : true ,
1140
1094
opaque_type_parent : false ,
1141
- from_poly_trait_ref : false ,
1142
- binder_depth : self . depth ( false ) ,
1095
+ scope_type : BinderScopeType :: Other ,
1143
1096
} ;
1144
1097
self . with ( scope, |_old_scope, this| {
1145
1098
let scope = Scope :: TraitRefBoundary { s : this. scope } ;
@@ -1198,8 +1151,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1198
1151
s : self . scope ,
1199
1152
track_lifetime_uses : true ,
1200
1153
opaque_type_parent : true ,
1201
- from_poly_trait_ref : false ,
1202
- binder_depth : self . depth ( false ) ,
1154
+ scope_type : BinderScopeType :: Other ,
1203
1155
} ;
1204
1156
self . with ( scope, |old_scope, this| {
1205
1157
this. check_lifetime_params ( old_scope, & generics. params ) ;
@@ -1268,8 +1220,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1268
1220
s : self . scope ,
1269
1221
track_lifetime_uses : true ,
1270
1222
opaque_type_parent : true ,
1271
- from_poly_trait_ref : false ,
1272
- binder_depth : self . depth ( false ) ,
1223
+ scope_type : BinderScopeType :: Other ,
1273
1224
} ;
1274
1225
self . with ( scope, |old_scope, this| {
1275
1226
this. check_lifetime_params ( old_scope, & generics. params ) ;
@@ -1373,29 +1324,28 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1373
1324
} )
1374
1325
. unzip ( ) ;
1375
1326
this. map . late_bound_vars . insert ( bounded_ty. hir_id , binders. clone ( ) ) ;
1376
- if !lifetimes. is_empty ( ) {
1377
- let next_early_index = this. next_early_index ( ) ;
1378
- let scope = Scope :: Binder {
1379
- hir_id : bounded_ty. hir_id ,
1380
- lifetimes,
1381
- s : this. scope ,
1382
- next_early_index,
1383
- track_lifetime_uses : true ,
1384
- opaque_type_parent : false ,
1385
- from_poly_trait_ref : true ,
1386
- binder_depth : this. depth ( false ) ,
1387
- } ;
1388
- this. with ( scope, |old_scope, this| {
1389
- this. check_lifetime_params ( old_scope, & bound_generic_params) ;
1390
- this. visit_ty ( & bounded_ty) ;
1391
- this. trait_ref_hack = Some ( bounded_ty. hir_id ) ;
1392
- walk_list ! ( this, visit_param_bound, bounds) ;
1393
- this. trait_ref_hack = None ;
1394
- } )
1395
- } else {
1327
+ if !lifetimes. is_empty ( ) {
1328
+ let next_early_index = this. next_early_index ( ) ;
1329
+ let scope = Scope :: Binder {
1330
+ hir_id : bounded_ty. hir_id ,
1331
+ lifetimes,
1332
+ s : this. scope ,
1333
+ next_early_index,
1334
+ track_lifetime_uses : true ,
1335
+ opaque_type_parent : false ,
1336
+ scope_type : BinderScopeType :: PolyTraitRef ,
1337
+ } ;
1338
+ this. with ( scope, |old_scope, this| {
1339
+ this. check_lifetime_params ( old_scope, & bound_generic_params) ;
1396
1340
this. visit_ty ( & bounded_ty) ;
1341
+ this. trait_ref_hack = Some ( bounded_ty. hir_id ) ;
1397
1342
walk_list ! ( this, visit_param_bound, bounds) ;
1398
- }
1343
+ this. trait_ref_hack = None ;
1344
+ } )
1345
+ } else {
1346
+ this. visit_ty ( & bounded_ty) ;
1347
+ walk_list ! ( this, visit_param_bound, bounds) ;
1348
+ }
1399
1349
}
1400
1350
& hir:: WherePredicate :: RegionPredicate ( hir:: WhereRegionPredicate {
1401
1351
ref lifetime,
@@ -1429,8 +1379,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1429
1379
next_early_index : self . next_early_index ( ) ,
1430
1380
track_lifetime_uses : true ,
1431
1381
opaque_type_parent : false ,
1432
- from_poly_trait_ref : false ,
1433
- binder_depth : self . depth ( false ) ,
1382
+ scope_type : BinderScopeType :: Other ,
1434
1383
} ;
1435
1384
self . with ( scope, |_, this| {
1436
1385
intravisit:: walk_param_bound ( this, bound) ;
@@ -1527,12 +1476,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1527
1476
break vec ! [ ] ;
1528
1477
}
1529
1478
1530
- Scope :: Binder { hir_id, from_poly_trait_ref, .. } => {
1531
- if !from_poly_trait_ref {
1532
- // We should only see super trait lifetimes if there is a `Binder` above
1533
- assert ! ( supertrait_lifetimes. is_empty( ) ) ;
1534
- break vec ! [ ] ;
1535
- }
1479
+ Scope :: Binder { hir_id, .. } => {
1536
1480
// Nested poly trait refs have the binders concatenated
1537
1481
let mut full_binders =
1538
1482
self . map . late_bound_vars . entry ( * hir_id) . or_default ( ) . clone ( ) ;
@@ -1569,15 +1513,41 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1569
1513
self . map . late_bound_vars . insert ( trait_ref. trait_ref . hir_ref_id , binders) ;
1570
1514
1571
1515
if trait_ref_hack. is_none ( ) || has_lifetimes {
1516
+ let scope_type = {
1517
+ let mut scope = self . scope ;
1518
+ loop {
1519
+ match * scope {
1520
+ Scope :: Root | Scope :: TraitRefBoundary { .. } => {
1521
+ break BinderScopeType :: PolyTraitRef ;
1522
+ }
1523
+
1524
+ Scope :: Binder { scope_type, .. } => {
1525
+ if let BinderScopeType :: Other = scope_type {
1526
+ bug ! (
1527
+ "Expected all syntacic poly trait refs to be surrounded by a `TraitRefBoundary`"
1528
+ )
1529
+ }
1530
+ break BinderScopeType :: Concatenating ;
1531
+ }
1532
+
1533
+ Scope :: Elision { s, .. }
1534
+ | Scope :: ObjectLifetimeDefault { s, .. }
1535
+ | Scope :: TraitRefHackInner { s, .. }
1536
+ | Scope :: Supertrait { s, .. }
1537
+ | Scope :: Body { s, .. } => {
1538
+ scope = s;
1539
+ }
1540
+ }
1541
+ }
1542
+ } ;
1572
1543
let scope = Scope :: Binder {
1573
1544
hir_id : trait_ref. trait_ref . hir_ref_id ,
1574
1545
lifetimes,
1575
1546
s : self . scope ,
1576
1547
next_early_index,
1577
1548
track_lifetime_uses : true ,
1578
1549
opaque_type_parent : false ,
1579
- from_poly_trait_ref : true ,
1580
- binder_depth : self . depth ( true ) ,
1550
+ scope_type,
1581
1551
} ;
1582
1552
self . with ( scope, |old_scope, this| {
1583
1553
this. check_lifetime_params ( old_scope, & trait_ref. bound_generic_params ) ;
@@ -2327,8 +2297,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2327
2297
s : self . scope ,
2328
2298
opaque_type_parent : true ,
2329
2299
track_lifetime_uses : false ,
2330
- from_poly_trait_ref : false ,
2331
- binder_depth : self . depth ( false ) ,
2300
+ scope_type : BinderScopeType :: Other ,
2332
2301
} ;
2333
2302
self . with ( scope, move |old_scope, this| {
2334
2303
this. check_lifetime_params ( old_scope, & generics. params ) ;
@@ -2386,7 +2355,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2386
2355
// given name or we run out of scopes.
2387
2356
// search.
2388
2357
let mut late_depth = 0 ;
2389
- let mut first_binder_depth = None ;
2390
2358
let mut scope = self . scope ;
2391
2359
let mut outermost_body = None ;
2392
2360
let result = loop {
@@ -2404,7 +2372,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2404
2372
break None ;
2405
2373
}
2406
2374
2407
- Scope :: Binder { ref lifetimes, s , binder_depth , .. } => {
2375
+ Scope :: Binder { ref lifetimes, scope_type , s , .. } => {
2408
2376
match lifetime_ref. name {
2409
2377
LifetimeName :: Param ( param_name) => {
2410
2378
if let Some ( & def) = lifetimes. get ( & param_name. normalize_to_macros_2_0 ( ) )
@@ -2414,8 +2382,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2414
2382
}
2415
2383
_ => bug ! ( "expected LifetimeName::Param" ) ,
2416
2384
}
2417
- first_binder_depth = first_binder_depth. or ( Some ( binder_depth) ) ;
2418
- late_depth = first_binder_depth. unwrap_or ( binder_depth) - binder_depth + 1 ;
2385
+ match scope_type {
2386
+ BinderScopeType :: Other => late_depth += 1 ,
2387
+ BinderScopeType :: PolyTraitRef => late_depth += 1 ,
2388
+ BinderScopeType :: Concatenating => { }
2389
+ }
2419
2390
scope = s;
2420
2391
}
2421
2392
@@ -3126,7 +3097,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
3126
3097
3127
3098
let span = lifetime_refs[ 0 ] . span ;
3128
3099
let mut late_depth = 0 ;
3129
- let mut first_binder_depth = None ;
3130
3100
let mut scope = self . scope ;
3131
3101
let mut lifetime_names = FxHashSet :: default ( ) ;
3132
3102
let mut lifetime_spans = vec ! [ ] ;
@@ -3137,16 +3107,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
3137
3107
3138
3108
Scope :: Root => break None ,
3139
3109
3140
- Scope :: Binder { s, ref lifetimes, binder_depth , .. } => {
3110
+ Scope :: Binder { s, ref lifetimes, scope_type , .. } => {
3141
3111
// collect named lifetimes for suggestions
3142
3112
for name in lifetimes. keys ( ) {
3143
3113
if let hir:: ParamName :: Plain ( name) = name {
3144
3114
lifetime_names. insert ( name. name ) ;
3145
3115
lifetime_spans. push ( name. span ) ;
3146
3116
}
3147
3117
}
3148
- first_binder_depth = first_binder_depth. or ( Some ( binder_depth) ) ;
3149
- late_depth = first_binder_depth. unwrap_or ( binder_depth) - binder_depth + 1 ;
3118
+ match scope_type {
3119
+ BinderScopeType :: Other => late_depth += 1 ,
3120
+ BinderScopeType :: PolyTraitRef => late_depth += 1 ,
3121
+ BinderScopeType :: Concatenating => { }
3122
+ }
3150
3123
scope = s;
3151
3124
}
3152
3125
@@ -3303,13 +3276,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
3303
3276
fn resolve_object_lifetime_default ( & mut self , lifetime_ref : & ' tcx hir:: Lifetime ) {
3304
3277
debug ! ( "resolve_object_lifetime_default(lifetime_ref={:?})" , lifetime_ref) ;
3305
3278
let mut late_depth = 0 ;
3306
- let mut first_binder_depth = None ;
3307
3279
let mut scope = self . scope ;
3308
3280
let lifetime = loop {
3309
3281
match * scope {
3310
- Scope :: Binder { s, binder_depth, .. } => {
3311
- first_binder_depth = first_binder_depth. or ( Some ( binder_depth) ) ;
3312
- late_depth = first_binder_depth. unwrap_or ( binder_depth) - binder_depth + 1 ;
3282
+ Scope :: Binder { s, scope_type, .. } => {
3283
+ match scope_type {
3284
+ BinderScopeType :: Other => late_depth += 1 ,
3285
+ BinderScopeType :: PolyTraitRef => late_depth += 1 ,
3286
+ BinderScopeType :: Concatenating => { }
3287
+ }
3313
3288
scope = s;
3314
3289
}
3315
3290
0 commit comments