Skip to content

Commit

Permalink
Auto merge of #82743 - jackh726:resolve-refactor, r=nikomatsakis
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Mar 25, 2021
2 parents cb473c2 + 44e9d20 commit 52e3dff
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 610 deletions.
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/middle/resolve_lifetime.rs
Expand Up @@ -78,9 +78,4 @@ pub struct ResolveLifetimes {
/// be late-bound if (a) it does NOT appear in a where-clause and
/// (b) it DOES appear in the arguments.
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,

/// For each type and trait definition, maps type parameters
/// to the trait object lifetime defaults computed from them.
pub object_lifetime_defaults:
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
}
23 changes: 19 additions & 4 deletions compiler/rustc_middle/src/query/mod.rs
Expand Up @@ -1258,8 +1258,19 @@ rustc_queries! {
desc { "looking up link arguments for a crate" }
}

/// Lifetime resolution. See `middle::resolve_lifetimes`.
query resolve_lifetimes(_: CrateNum) -> ResolveLifetimes {
/// Does lifetime resolution, but does not descend into trait items. This
/// should only be used for resolving lifetimes of on trait definitions,
/// and is used to avoid cycles. Importantly, `resolve_lifetimes` still visits
/// the same lifetimes and is responsible for diagnostics.
/// See `rustc_resolve::late::lifetimes for details.
query resolve_lifetimes_trait_definition(_: LocalDefId) -> ResolveLifetimes {
storage(ArenaCacheSelector<'tcx>)
desc { "resolving lifetimes for a trait definition" }
}
/// Does lifetime resolution on items. Importantly, we can't resolve
/// lifetimes directly on things like trait methods, because of trait params.
/// See `rustc_resolve::late::lifetimes for details.
query resolve_lifetimes(_: LocalDefId) -> ResolveLifetimes {
storage(ArenaCacheSelector<'tcx>)
desc { "resolving lifetimes" }
}
Expand All @@ -1271,9 +1282,13 @@ rustc_queries! {
Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
desc { "testing if a region is late bound" }
}
/// For a given item (like a struct), gets the default lifetimes to be used
/// for each paramter if a trait object were to be passed for that parameter.
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
query object_lifetime_defaults_map(_: LocalDefId)
-> Option<&'tcx FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>> {
desc { "looking up lifetime defaults for a region" }
-> Option<Vec<ObjectLifetimeDefault>> {
desc { "looking up lifetime defaults for a region on an item" }
}

query visibility(def_id: DefId) -> ty::Visibility {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -2641,6 +2641,7 @@ impl<'tcx> TyCtxt<'tcx> {
}

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

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

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

Expand Down

0 comments on commit 52e3dff

Please sign in to comment.