Skip to content

Commit 52e3dff

Browse files
committed
Auto merge of #82743 - jackh726:resolve-refactor, r=nikomatsakis
Refactor rustc_resolve::late::lifetimes to resolve per-item There are some changes to tests that I'd like some feedback on; so this is still WIP. The reason behind this change will (hopefully) allow us to (as part of #76814) be able to essentially use the lifetime resolve code to resolve *all* late bound vars (including those of super traits). Currently, it only resolves those that are *syntactically* in scope. In #76814, I'm essentially finding that I would essentially have to redo the passing of bound vars through scopes (i.e. when instantiating a poly trait ref), and that's what this code does anyways. However, to be able to do this (ask super traits what bound vars are in scope), we have to be able to resolve items separately. The first commit is actually partially orthogonal. Essentially removing one use of late bound debruijn indices. Not exactly sure who would be best to review here. Let r? `@nikomatsakis`
2 parents cb473c2 + 44e9d20 commit 52e3dff

22 files changed

+581
-610
lines changed

compiler/rustc_middle/src/middle/resolve_lifetime.rs

-5
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,4 @@ pub struct ResolveLifetimes {
7878
/// be late-bound if (a) it does NOT appear in a where-clause and
7979
/// (b) it DOES appear in the arguments.
8080
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
81-
82-
/// For each type and trait definition, maps type parameters
83-
/// to the trait object lifetime defaults computed from them.
84-
pub object_lifetime_defaults:
85-
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
8681
}

compiler/rustc_middle/src/query/mod.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,19 @@ rustc_queries! {
12581258
desc { "looking up link arguments for a crate" }
12591259
}
12601260

1261-
/// Lifetime resolution. See `middle::resolve_lifetimes`.
1262-
query resolve_lifetimes(_: CrateNum) -> ResolveLifetimes {
1261+
/// Does lifetime resolution, but does not descend into trait items. This
1262+
/// should only be used for resolving lifetimes of on trait definitions,
1263+
/// and is used to avoid cycles. Importantly, `resolve_lifetimes` still visits
1264+
/// the same lifetimes and is responsible for diagnostics.
1265+
/// See `rustc_resolve::late::lifetimes for details.
1266+
query resolve_lifetimes_trait_definition(_: LocalDefId) -> ResolveLifetimes {
1267+
storage(ArenaCacheSelector<'tcx>)
1268+
desc { "resolving lifetimes for a trait definition" }
1269+
}
1270+
/// Does lifetime resolution on items. Importantly, we can't resolve
1271+
/// lifetimes directly on things like trait methods, because of trait params.
1272+
/// See `rustc_resolve::late::lifetimes for details.
1273+
query resolve_lifetimes(_: LocalDefId) -> ResolveLifetimes {
12631274
storage(ArenaCacheSelector<'tcx>)
12641275
desc { "resolving lifetimes" }
12651276
}
@@ -1271,9 +1282,13 @@ rustc_queries! {
12711282
Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
12721283
desc { "testing if a region is late bound" }
12731284
}
1285+
/// For a given item (like a struct), gets the default lifetimes to be used
1286+
/// for each paramter if a trait object were to be passed for that parameter.
1287+
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
1288+
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
12741289
query object_lifetime_defaults_map(_: LocalDefId)
1275-
-> Option<&'tcx FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>> {
1276-
desc { "looking up lifetime defaults for a region" }
1290+
-> Option<Vec<ObjectLifetimeDefault>> {
1291+
desc { "looking up lifetime defaults for a region on an item" }
12771292
}
12781293

12791294
query visibility(def_id: DefId) -> ty::Visibility {

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,7 @@ impl<'tcx> TyCtxt<'tcx> {
26412641
}
26422642

26432643
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
2644+
debug!(?id, "named_region");
26442645
self.named_region_map(id.owner).and_then(|map| map.get(&id.local_id).cloned())
26452646
}
26462647

@@ -2649,9 +2650,8 @@ impl<'tcx> TyCtxt<'tcx> {
26492650
.map_or(false, |(owner, set)| owner == id.owner && set.contains(&id.local_id))
26502651
}
26512652

2652-
pub fn object_lifetime_defaults(self, id: HirId) -> Option<&'tcx [ObjectLifetimeDefault]> {
2653+
pub fn object_lifetime_defaults(self, id: HirId) -> Option<Vec<ObjectLifetimeDefault>> {
26532654
self.object_lifetime_defaults_map(id.owner)
2654-
.and_then(|map| map.get(&id.local_id).map(|v| &**v))
26552655
}
26562656
}
26572657

0 commit comments

Comments
 (0)