Skip to content

Separate object lifetime default from lifetime resolution #97393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ rustc_queries! {
/// for each parameter 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]`.
Comment on lines 1591 to 1593
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is still true?

query object_lifetime_defaults(_: LocalDefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
query object_lifetime_defaults(_: DefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
desc { "looking up lifetime defaults for a region on an item" }
}
query late_bound_vars_map(_: LocalDefId)
Expand Down
59 changes: 19 additions & 40 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefIdMap, LocalDefId};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::hir_id::ItemLocalId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node};
Expand Down Expand Up @@ -156,9 +156,6 @@ pub(crate) struct LifetimeContext<'a, 'tcx> {
/// we eventually need lifetimes resolve for trait items.
trait_definition_only: bool,

/// Cache for cross-crate per-definition object lifetime defaults.
xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,

/// When encountering an undefined named lifetime, we will suggest introducing it in these
/// places.
pub(crate) missing_named_lifetime_spots: Vec<MissingLifetimeSpot<'tcx>>,
Expand Down Expand Up @@ -348,9 +345,23 @@ pub fn provide(providers: &mut ty::query::Providers) {

named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
is_late_bound_map,
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
_ => None,
object_lifetime_defaults: |tcx, def_id| {
if let Some(def_id) = def_id.as_local() {
match tcx.hir().get_by_def_id(def_id) {
Node::Item(item) => compute_object_lifetime_defaults(tcx, item),
_ => None,
}
} else {
Some(tcx.arena.alloc_from_iter(tcx.generics_of(def_id).params.iter().filter_map(
|param| match param.kind {
GenericParamDefKind::Type { object_lifetime_default, .. } => {
Some(object_lifetime_default)
}
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
GenericParamDefKind::Lifetime => None,
},
)))
}
},
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
lifetime_scope_map: |tcx, id| {
Expand Down Expand Up @@ -425,7 +436,6 @@ fn do_resolve(
map: &mut named_region_map,
scope: ROOT_SCOPE,
trait_definition_only,
xcrate_object_lifetime_defaults: Default::default(),
missing_named_lifetime_spots: vec![],
};
visitor.visit_item(item);
Expand Down Expand Up @@ -1573,22 +1583,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
{
let LifetimeContext { tcx, map, .. } = self;
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
let mut this = LifetimeContext {
tcx: *tcx,
map,
scope: &wrap_scope,
trait_definition_only: self.trait_definition_only,
xcrate_object_lifetime_defaults,
missing_named_lifetime_spots,
};
let span = tracing::debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
{
let _enter = span.enter();
f(&mut this);
}
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
}

Expand Down Expand Up @@ -1904,35 +1911,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}
Set1::Many => None,
};
if let Some(def_id) = def_id.as_local() {
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
self.tcx
.object_lifetime_defaults(id.owner)
.unwrap()
.iter()
.map(set_to_region)
.collect()
} else {
let tcx = self.tcx;
self.xcrate_object_lifetime_defaults
.entry(def_id)
.or_insert_with(|| {
tcx.generics_of(def_id)
.params
.iter()
.filter_map(|param| match param.kind {
GenericParamDefKind::Type { object_lifetime_default, .. } => {
Some(object_lifetime_default)
}
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
GenericParamDefKind::Lifetime => None,
})
.collect()
})
.iter()
.map(set_to_region)
.collect()
}
self.tcx.object_lifetime_defaults(def_id).unwrap().iter().map(set_to_region).collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the unwrap here be changed to an expect or uwrap_or_else(|| bug!())?

});

debug!("visit_segment_args: object_lifetime_defaults={:?}", object_lifetime_defaults);
Expand Down