Skip to content

Commit 549f25e

Browse files
committed
Make object_lifetime_defaults a cross-crate query.
1 parent 872503d commit 549f25e

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::{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::intravisit::{self, Visitor};
1717
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node};
1818
use rustc_hir::{GenericParamKind, HirIdMap};
@@ -152,9 +152,6 @@ pub(crate) struct LifetimeContext<'a, 'tcx> {
152152
/// we eventually need lifetimes resolve for trait items.
153153
trait_definition_only: bool,
154154

155-
/// Cache for cross-crate per-definition object lifetime defaults.
156-
xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
157-
158155
/// When encountering an undefined named lifetime, we will suggest introducing it in these
159156
/// places.
160157
pub(crate) missing_named_lifetime_spots: Vec<MissingLifetimeSpot<'tcx>>,
@@ -353,9 +350,23 @@ pub fn provide(providers: &mut ty::query::Providers) {
353350

354351
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
355352
is_late_bound_map,
356-
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
357-
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
358-
_ => None,
353+
object_lifetime_defaults: |tcx, def_id| {
354+
if let Some(def_id) = def_id.as_local() {
355+
match tcx.hir().get_by_def_id(def_id) {
356+
Node::Item(item) => compute_object_lifetime_defaults(tcx, item),
357+
_ => None,
358+
}
359+
} else {
360+
Some(tcx.arena.alloc_from_iter(tcx.generics_of(def_id).params.iter().filter_map(
361+
|param| match param.kind {
362+
GenericParamDefKind::Type { object_lifetime_default, .. } => {
363+
Some(object_lifetime_default)
364+
}
365+
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
366+
GenericParamDefKind::Lifetime => None,
367+
},
368+
)))
369+
}
359370
},
360371
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
361372

@@ -422,7 +433,6 @@ fn do_resolve(
422433
map: &mut named_region_map,
423434
scope: ROOT_SCOPE,
424435
trait_definition_only,
425-
xcrate_object_lifetime_defaults: Default::default(),
426436
missing_named_lifetime_spots: vec![],
427437
};
428438
visitor.visit_item(item);
@@ -1511,22 +1521,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15111521
F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
15121522
{
15131523
let LifetimeContext { tcx, map, .. } = self;
1514-
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
15151524
let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
15161525
let mut this = LifetimeContext {
15171526
tcx: *tcx,
15181527
map,
15191528
scope: &wrap_scope,
15201529
trait_definition_only: self.trait_definition_only,
1521-
xcrate_object_lifetime_defaults,
15221530
missing_named_lifetime_spots,
15231531
};
15241532
let span = tracing::debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
15251533
{
15261534
let _enter = span.enter();
15271535
f(&mut this);
15281536
}
1529-
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
15301537
self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
15311538
}
15321539

@@ -1879,35 +1886,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18791886
}
18801887
Set1::Many => None,
18811888
};
1882-
if let Some(def_id) = def_id.as_local() {
1883-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1884-
self.tcx
1885-
.object_lifetime_defaults(id.owner)
1886-
.unwrap()
1887-
.iter()
1888-
.map(set_to_region)
1889-
.collect()
1890-
} else {
1891-
let tcx = self.tcx;
1892-
self.xcrate_object_lifetime_defaults
1893-
.entry(def_id)
1894-
.or_insert_with(|| {
1895-
tcx.generics_of(def_id)
1896-
.params
1897-
.iter()
1898-
.filter_map(|param| match param.kind {
1899-
GenericParamDefKind::Type { object_lifetime_default, .. } => {
1900-
Some(object_lifetime_default)
1901-
}
1902-
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
1903-
GenericParamDefKind::Lifetime => None,
1904-
})
1905-
.collect()
1906-
})
1907-
.iter()
1908-
.map(set_to_region)
1909-
.collect()
1910-
}
1889+
self.tcx.object_lifetime_defaults(def_id).unwrap().iter().map(set_to_region).collect()
19111890
});
19121891

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

0 commit comments

Comments
 (0)