Skip to content

Commit 457c4c1

Browse files
committed
Add BinderScopeType to replace binder_depth and from_poly_trait_ref
1 parent 32942ab commit 457c4c1

File tree

1 file changed

+86
-111
lines changed

1 file changed

+86
-111
lines changed

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 86 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,7 @@ enum Scope<'a> {
244244
/// of the resulting opaque type.
245245
opaque_type_parent: bool,
246246

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,
253248

254249
/// The late bound vars for a given item are stored by `HirId` to be
255250
/// queried later. However, if we enter an elision scope, we have to
@@ -335,6 +330,13 @@ enum Scope<'a> {
335330
Root,
336331
}
337332

333+
#[derive(Copy, Clone, Debug)]
334+
enum BinderScopeType {
335+
Other,
336+
PolyTraitRef,
337+
Concatenating,
338+
}
339+
338340
// A helper struct for debugging scopes without printing parent scopes
339341
struct TruncatedScopeDebug<'a>(&'a Scope<'a>);
340342

@@ -346,8 +348,7 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
346348
next_early_index,
347349
track_lifetime_uses,
348350
opaque_type_parent,
349-
from_poly_trait_ref,
350-
binder_depth,
351+
scope_type,
351352
hir_id,
352353
s: _,
353354
} => f
@@ -356,8 +357,7 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
356357
.field("next_early_index", next_early_index)
357358
.field("track_lifetime_uses", track_lifetime_uses)
358359
.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)
361361
.field("hir_id", hir_id)
362362
.field("s", &"..")
363363
.finish(),
@@ -622,48 +622,6 @@ fn late_region_as_bound_region<'tcx>(tcx: TyCtxt<'tcx>, region: &Region) -> ty::
622622
}
623623
}
624624

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-
667625
impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
668626
type Map = Map<'tcx>;
669627

@@ -721,8 +679,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
721679
s: self.scope,
722680
track_lifetime_uses: true,
723681
opaque_type_parent: false,
724-
from_poly_trait_ref: false,
725-
binder_depth: self.depth(false),
682+
scope_type: BinderScopeType::Other,
726683
};
727684
self.with(scope, move |_old_scope, this| {
728685
intravisit::walk_fn(this, fk, fd, b, s, hir_id)
@@ -847,8 +804,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
847804
next_early_index: index + non_lifetime_count,
848805
opaque_type_parent: true,
849806
track_lifetime_uses,
850-
from_poly_trait_ref: false,
851-
binder_depth: self.depth(false),
807+
scope_type: BinderScopeType::Other,
852808
s: ROOT_SCOPE,
853809
};
854810
self.with(scope, |old_scope, this| {
@@ -920,8 +876,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
920876
next_early_index,
921877
track_lifetime_uses: true,
922878
opaque_type_parent: false,
923-
from_poly_trait_ref: false,
924-
binder_depth: self.depth(false),
879+
scope_type: BinderScopeType::Other,
925880
};
926881
self.with(scope, |old_scope, this| {
927882
// a bare fn has no bounds, so everything
@@ -1117,8 +1072,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11171072
s: this.scope,
11181073
track_lifetime_uses: true,
11191074
opaque_type_parent: false,
1120-
from_poly_trait_ref: false,
1121-
binder_depth: this.depth(false),
1075+
scope_type: BinderScopeType::Other,
11221076
};
11231077
this.with(scope, |_old_scope, this| {
11241078
this.visit_generics(generics);
@@ -1138,8 +1092,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11381092
s: self.scope,
11391093
track_lifetime_uses: true,
11401094
opaque_type_parent: false,
1141-
from_poly_trait_ref: false,
1142-
binder_depth: self.depth(false),
1095+
scope_type: BinderScopeType::Other,
11431096
};
11441097
self.with(scope, |_old_scope, this| {
11451098
let scope = Scope::TraitRefBoundary { s: this.scope };
@@ -1198,8 +1151,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11981151
s: self.scope,
11991152
track_lifetime_uses: true,
12001153
opaque_type_parent: true,
1201-
from_poly_trait_ref: false,
1202-
binder_depth: self.depth(false),
1154+
scope_type: BinderScopeType::Other,
12031155
};
12041156
self.with(scope, |old_scope, this| {
12051157
this.check_lifetime_params(old_scope, &generics.params);
@@ -1268,8 +1220,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12681220
s: self.scope,
12691221
track_lifetime_uses: true,
12701222
opaque_type_parent: true,
1271-
from_poly_trait_ref: false,
1272-
binder_depth: self.depth(false),
1223+
scope_type: BinderScopeType::Other,
12731224
};
12741225
self.with(scope, |old_scope, this| {
12751226
this.check_lifetime_params(old_scope, &generics.params);
@@ -1373,29 +1324,28 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13731324
})
13741325
.unzip();
13751326
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);
13961340
this.visit_ty(&bounded_ty);
1341+
this.trait_ref_hack = Some(bounded_ty.hir_id);
13971342
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+
}
13991349
}
14001350
&hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
14011351
ref lifetime,
@@ -1429,8 +1379,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
14291379
next_early_index: self.next_early_index(),
14301380
track_lifetime_uses: true,
14311381
opaque_type_parent: false,
1432-
from_poly_trait_ref: false,
1433-
binder_depth: self.depth(false),
1382+
scope_type: BinderScopeType::Other,
14341383
};
14351384
self.with(scope, |_, this| {
14361385
intravisit::walk_param_bound(this, bound);
@@ -1527,12 +1476,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
15271476
break vec![];
15281477
}
15291478

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, .. } => {
15361480
// Nested poly trait refs have the binders concatenated
15371481
let mut full_binders =
15381482
self.map.late_bound_vars.entry(*hir_id).or_default().clone();
@@ -1569,15 +1513,41 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
15691513
self.map.late_bound_vars.insert(trait_ref.trait_ref.hir_ref_id, binders);
15701514

15711515
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+
};
15721543
let scope = Scope::Binder {
15731544
hir_id: trait_ref.trait_ref.hir_ref_id,
15741545
lifetimes,
15751546
s: self.scope,
15761547
next_early_index,
15771548
track_lifetime_uses: true,
15781549
opaque_type_parent: false,
1579-
from_poly_trait_ref: true,
1580-
binder_depth: self.depth(true),
1550+
scope_type,
15811551
};
15821552
self.with(scope, |old_scope, this| {
15831553
this.check_lifetime_params(old_scope, &trait_ref.bound_generic_params);
@@ -2327,8 +2297,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23272297
s: self.scope,
23282298
opaque_type_parent: true,
23292299
track_lifetime_uses: false,
2330-
from_poly_trait_ref: false,
2331-
binder_depth: self.depth(false),
2300+
scope_type: BinderScopeType::Other,
23322301
};
23332302
self.with(scope, move |old_scope, this| {
23342303
this.check_lifetime_params(old_scope, &generics.params);
@@ -2386,7 +2355,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23862355
// given name or we run out of scopes.
23872356
// search.
23882357
let mut late_depth = 0;
2389-
let mut first_binder_depth = None;
23902358
let mut scope = self.scope;
23912359
let mut outermost_body = None;
23922360
let result = loop {
@@ -2404,7 +2372,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
24042372
break None;
24052373
}
24062374

2407-
Scope::Binder { ref lifetimes, s, binder_depth, .. } => {
2375+
Scope::Binder { ref lifetimes, scope_type, s, .. } => {
24082376
match lifetime_ref.name {
24092377
LifetimeName::Param(param_name) => {
24102378
if let Some(&def) = lifetimes.get(&param_name.normalize_to_macros_2_0())
@@ -2414,8 +2382,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
24142382
}
24152383
_ => bug!("expected LifetimeName::Param"),
24162384
}
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+
}
24192390
scope = s;
24202391
}
24212392

@@ -3126,7 +3097,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
31263097

31273098
let span = lifetime_refs[0].span;
31283099
let mut late_depth = 0;
3129-
let mut first_binder_depth = None;
31303100
let mut scope = self.scope;
31313101
let mut lifetime_names = FxHashSet::default();
31323102
let mut lifetime_spans = vec![];
@@ -3137,16 +3107,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
31373107

31383108
Scope::Root => break None,
31393109

3140-
Scope::Binder { s, ref lifetimes, binder_depth, .. } => {
3110+
Scope::Binder { s, ref lifetimes, scope_type, .. } => {
31413111
// collect named lifetimes for suggestions
31423112
for name in lifetimes.keys() {
31433113
if let hir::ParamName::Plain(name) = name {
31443114
lifetime_names.insert(name.name);
31453115
lifetime_spans.push(name.span);
31463116
}
31473117
}
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+
}
31503123
scope = s;
31513124
}
31523125

@@ -3303,13 +3276,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
33033276
fn resolve_object_lifetime_default(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
33043277
debug!("resolve_object_lifetime_default(lifetime_ref={:?})", lifetime_ref);
33053278
let mut late_depth = 0;
3306-
let mut first_binder_depth = None;
33073279
let mut scope = self.scope;
33083280
let lifetime = loop {
33093281
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+
}
33133288
scope = s;
33143289
}
33153290

0 commit comments

Comments
 (0)