Skip to content

Commit 39bc74e

Browse files
committed
Make object_lifetime_defaults a cross-crate query.
1 parent 04f72f9 commit 39bc74e

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

compiler/rustc_middle/src/query/mod.rs

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

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1111
use rustc_errors::struct_span_err;
1212
use rustc_hir as hir;
1313
use rustc_hir::def::{DefKind, Res};
14-
use rustc_hir::def_id::{DefIdMap, LocalDefId};
14+
use rustc_hir::def_id::LocalDefId;
1515
use rustc_hir::intravisit::{self, Visitor};
1616
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node};
1717
use rustc_middle::bug;
@@ -24,7 +24,6 @@ use rustc_span::symbol::{sym, Ident};
2424
use rustc_span::Span;
2525
use std::borrow::Cow;
2626
use std::fmt;
27-
use std::mem::take;
2827

2928
trait RegionExt {
3029
fn early(hir_map: Map<'_>, index: &mut u32, param: &GenericParam<'_>) -> (LocalDefId, Region);
@@ -131,9 +130,6 @@ pub(crate) struct LifetimeContext<'a, 'tcx> {
131130
/// be false if the `Item` we are resolving lifetimes for is not a trait or
132131
/// we eventually need lifetimes resolve for trait items.
133132
trait_definition_only: bool,
134-
135-
/// Cache for cross-crate per-definition object lifetime defaults.
136-
xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
137133
}
138134

139135
#[derive(Debug)]
@@ -294,9 +290,23 @@ pub fn provide(providers: &mut ty::query::Providers) {
294290

295291
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
296292
is_late_bound_map,
297-
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
298-
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
299-
_ => None,
293+
object_lifetime_defaults: |tcx, def_id| {
294+
if let Some(def_id) = def_id.as_local() {
295+
match tcx.hir().get_by_def_id(def_id) {
296+
Node::Item(item) => compute_object_lifetime_defaults(tcx, item),
297+
_ => None,
298+
}
299+
} else {
300+
Some(tcx.arena.alloc_from_iter(tcx.generics_of(def_id).params.iter().filter_map(
301+
|param| match param.kind {
302+
GenericParamDefKind::Type { object_lifetime_default, .. } => {
303+
Some(object_lifetime_default)
304+
}
305+
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
306+
GenericParamDefKind::Lifetime => None,
307+
},
308+
)))
309+
}
300310
},
301311
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
302312

@@ -363,7 +373,6 @@ fn do_resolve(
363373
map: &mut named_region_map,
364374
scope: ROOT_SCOPE,
365375
trait_definition_only,
366-
xcrate_object_lifetime_defaults: Default::default(),
367376
};
368377
visitor.visit_item(item);
369378

@@ -1413,20 +1422,17 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14131422
F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
14141423
{
14151424
let LifetimeContext { tcx, map, .. } = self;
1416-
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
14171425
let mut this = LifetimeContext {
14181426
tcx: *tcx,
14191427
map,
14201428
scope: &wrap_scope,
14211429
trait_definition_only: self.trait_definition_only,
1422-
xcrate_object_lifetime_defaults,
14231430
};
14241431
let span = tracing::debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
14251432
{
14261433
let _enter = span.enter();
14271434
f(&mut this);
14281435
}
1429-
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
14301436
}
14311437

14321438
/// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
@@ -1780,35 +1786,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17801786
}
17811787
Set1::Many => None,
17821788
};
1783-
if let Some(def_id) = def_id.as_local() {
1784-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1785-
self.tcx
1786-
.object_lifetime_defaults(id.owner)
1787-
.unwrap()
1788-
.iter()
1789-
.map(set_to_region)
1790-
.collect()
1791-
} else {
1792-
let tcx = self.tcx;
1793-
self.xcrate_object_lifetime_defaults
1794-
.entry(def_id)
1795-
.or_insert_with(|| {
1796-
tcx.generics_of(def_id)
1797-
.params
1798-
.iter()
1799-
.filter_map(|param| match param.kind {
1800-
GenericParamDefKind::Type { object_lifetime_default, .. } => {
1801-
Some(object_lifetime_default)
1802-
}
1803-
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
1804-
GenericParamDefKind::Lifetime => None,
1805-
})
1806-
.collect()
1807-
})
1808-
.iter()
1809-
.map(set_to_region)
1810-
.collect()
1811-
}
1789+
self.tcx.object_lifetime_defaults(def_id).unwrap().iter().map(set_to_region).collect()
18121790
});
18131791

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

0 commit comments

Comments
 (0)