Skip to content

Commit 59ea28e

Browse files
committed
Make object_lifetime_defaults a cross-crate query.
1 parent 7e9b92c commit 59ea28e

File tree

2 files changed

+20
-41
lines changed

2 files changed

+20
-41
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ rustc_queries! {
15911591
/// for each parameter if a trait object were to be passed for that parameter.
15921592
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
15931593
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
1594-
query object_lifetime_defaults(_: LocalDefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
1594+
query object_lifetime_defaults(_: DefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
15951595
desc { "looking up lifetime defaults for a region on an item" }
15961596
}
15971597
query late_bound_vars_map(_: LocalDefId)

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1212
use rustc_errors::struct_span_err;
1313
use rustc_hir as hir;
1414
use rustc_hir::def::{DefKind, Res};
15-
use rustc_hir::def_id::{DefIdMap, LocalDefId};
15+
use rustc_hir::def_id::LocalDefId;
1616
use rustc_hir::hir_id::ItemLocalId;
1717
use rustc_hir::intravisit::{self, Visitor};
1818
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node};
@@ -156,9 +156,6 @@ pub(crate) struct LifetimeContext<'a, 'tcx> {
156156
/// we eventually need lifetimes resolve for trait items.
157157
trait_definition_only: bool,
158158

159-
/// Cache for cross-crate per-definition object lifetime defaults.
160-
xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
161-
162159
/// When encountering an undefined named lifetime, we will suggest introducing it in these
163160
/// places.
164161
pub(crate) missing_named_lifetime_spots: Vec<MissingLifetimeSpot<'tcx>>,
@@ -348,9 +345,23 @@ pub fn provide(providers: &mut ty::query::Providers) {
348345

349346
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
350347
is_late_bound_map,
351-
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
352-
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
353-
_ => None,
348+
object_lifetime_defaults: |tcx, def_id| {
349+
if let Some(def_id) = def_id.as_local() {
350+
match tcx.hir().get_by_def_id(def_id) {
351+
Node::Item(item) => compute_object_lifetime_defaults(tcx, item),
352+
_ => None,
353+
}
354+
} else {
355+
Some(tcx.arena.alloc_from_iter(tcx.generics_of(def_id).params.iter().filter_map(
356+
|param| match param.kind {
357+
GenericParamDefKind::Type { object_lifetime_default, .. } => {
358+
Some(object_lifetime_default)
359+
}
360+
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
361+
GenericParamDefKind::Lifetime => None,
362+
},
363+
)))
364+
}
354365
},
355366
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
356367
lifetime_scope_map: |tcx, id| {
@@ -425,7 +436,6 @@ fn do_resolve(
425436
map: &mut named_region_map,
426437
scope: ROOT_SCOPE,
427438
trait_definition_only,
428-
xcrate_object_lifetime_defaults: Default::default(),
429439
missing_named_lifetime_spots: vec![],
430440
};
431441
visitor.visit_item(item);
@@ -1573,22 +1583,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15731583
F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
15741584
{
15751585
let LifetimeContext { tcx, map, .. } = self;
1576-
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
15771586
let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
15781587
let mut this = LifetimeContext {
15791588
tcx: *tcx,
15801589
map,
15811590
scope: &wrap_scope,
15821591
trait_definition_only: self.trait_definition_only,
1583-
xcrate_object_lifetime_defaults,
15841592
missing_named_lifetime_spots,
15851593
};
15861594
let span = tracing::debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
15871595
{
15881596
let _enter = span.enter();
15891597
f(&mut this);
15901598
}
1591-
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
15921599
self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
15931600
}
15941601

@@ -1904,35 +1911,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
19041911
}
19051912
Set1::Many => None,
19061913
};
1907-
if let Some(def_id) = def_id.as_local() {
1908-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1909-
self.tcx
1910-
.object_lifetime_defaults(id.owner)
1911-
.unwrap()
1912-
.iter()
1913-
.map(set_to_region)
1914-
.collect()
1915-
} else {
1916-
let tcx = self.tcx;
1917-
self.xcrate_object_lifetime_defaults
1918-
.entry(def_id)
1919-
.or_insert_with(|| {
1920-
tcx.generics_of(def_id)
1921-
.params
1922-
.iter()
1923-
.filter_map(|param| match param.kind {
1924-
GenericParamDefKind::Type { object_lifetime_default, .. } => {
1925-
Some(object_lifetime_default)
1926-
}
1927-
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
1928-
GenericParamDefKind::Lifetime => None,
1929-
})
1930-
.collect()
1931-
})
1932-
.iter()
1933-
.map(set_to_region)
1934-
.collect()
1935-
}
1914+
self.tcx.object_lifetime_defaults(def_id).unwrap().iter().map(set_to_region).collect()
19361915
});
19371916

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

0 commit comments

Comments
 (0)