Skip to content

Commit e80a113

Browse files
committed
Return ty::Region in object_lifetime_map.
1 parent 64e7067 commit e80a113

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ rustc_queries! {
15951595
desc { "looking up lifetime defaults for a region on an item" }
15961596
}
15971597
/// Fetch the lifetimes for all the trait objects in an item-like.
1598-
query object_lifetime_map(_: LocalDefId) -> FxHashMap<ItemLocalId, Region> {
1598+
query object_lifetime_map(_: LocalDefId) -> FxHashMap<ItemLocalId, ty::Region<'tcx>> {
15991599
storage(ArenaCacheSelector<'tcx>)
16001600
desc { "looking up lifetime defaults for a region on an item" }
16011601
}

compiler/rustc_typeck/src/astconv/mod.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
204204
let tcx = self.tcx();
205205

206206
let r = if let Some(rl) = tcx.named_region(lifetime.hir_id) {
207-
self.ast_region_to_region_inner(rl)
207+
Self::ast_region_to_region_inner(tcx, rl)
208208
} else {
209209
self.re_infer(def, lifetime.span).unwrap_or_else(|| {
210210
debug!(?lifetime, "unelided lifetime in signature");
@@ -226,9 +226,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
226226
r
227227
}
228228

229-
#[tracing::instrument(level = "debug", skip(self))]
230-
fn ast_region_to_region_inner(&self, lifetime: rl::Region) -> ty::Region<'tcx> {
231-
let tcx = self.tcx();
229+
#[tracing::instrument(level = "debug", skip(tcx))]
230+
pub fn ast_region_to_region_inner(tcx: TyCtxt<'tcx>, lifetime: rl::Region) -> ty::Region<'tcx> {
232231
let lifetime_name = |def_id| tcx.hir().name(tcx.hir().local_def_id_to_hir_id(def_id));
233232

234233
let r = match lifetime {
@@ -1547,33 +1546,33 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15471546
// Use explicitly-specified region bound.
15481547
let region_bound = if !lifetime.is_elided() {
15491548
self.ast_region_to_region(lifetime, None)
1549+
} else if let Some(region) =
1550+
self.compute_object_lifetime_bound(span, existential_predicates)
1551+
{
1552+
region
1553+
} else if let Some(rl) = tcx.named_region(lifetime.hir_id) {
1554+
Self::ast_region_to_region_inner(self.tcx(), rl)
1555+
} else if let Some(&region) =
1556+
tcx.object_lifetime_map(lifetime.hir_id.owner).get(&lifetime.hir_id.local_id)
1557+
{
1558+
region
15501559
} else {
1551-
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
1552-
if let Some(rl) = tcx.named_region(lifetime.hir_id) {
1553-
self.ast_region_to_region_inner(rl)
1554-
} else if let Some(&rl) =
1555-
tcx.object_lifetime_map(lifetime.hir_id.owner).get(&lifetime.hir_id.local_id)
1556-
{
1557-
self.ast_region_to_region_inner(rl)
1558-
} else {
1559-
self.re_infer(None, span).unwrap_or_else(|| {
1560-
let mut err = struct_span_err!(
1561-
tcx.sess,
1562-
span,
1563-
E0228,
1564-
"the lifetime bound for this object type cannot be deduced \
1560+
self.re_infer(None, span).unwrap_or_else(|| {
1561+
let mut err = struct_span_err!(
1562+
tcx.sess,
1563+
span,
1564+
E0228,
1565+
"the lifetime bound for this object type cannot be deduced \
15651566
from context; please supply an explicit bound"
1566-
);
1567-
if borrowed {
1568-
// We will have already emitted an error E0106 complaining about a
1569-
// missing named lifetime in `&dyn Trait`, so we elide this one.
1570-
err.delay_as_bug();
1571-
} else {
1572-
err.emit();
1573-
}
1574-
tcx.lifetimes.re_static
1575-
})
1567+
);
1568+
if borrowed {
1569+
// We will have already emitted an error E0106 complaining about a
1570+
// missing named lifetime in `&dyn Trait`, so we elide this one.
1571+
err.delay_as_bug();
1572+
} else {
1573+
err.emit();
15761574
}
1575+
tcx.lifetimes.re_static
15771576
})
15781577
};
15791578
debug!("region_bound: {:?}", region_bound);

compiler/rustc_typeck/src/collect/object_lifetime_defaults.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::AstConv;
12
use rustc_data_structures::fx::FxHashMap;
23
use rustc_hir as hir;
34
use rustc_hir::def::{DefKind, Res};
@@ -8,7 +9,7 @@ use rustc_hir::{GenericArg, GenericParamKind, LifetimeName, Node};
89
use rustc_middle::bug;
910
use rustc_middle::hir::nested_filter;
1011
use rustc_middle::middle::resolve_lifetime::*;
11-
use rustc_middle::ty::{DefIdTree, GenericParamDefKind, TyCtxt};
12+
use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
1213
use rustc_span::symbol::sym;
1314
use std::borrow::Cow;
1415

@@ -128,7 +129,7 @@ fn object_lifetime_defaults_for_item<'tcx>(
128129
pub(super) fn object_lifetime_map(
129130
tcx: TyCtxt<'_>,
130131
def_id: LocalDefId,
131-
) -> FxHashMap<ItemLocalId, Region> {
132+
) -> FxHashMap<ItemLocalId, ty::Region<'_>> {
132133
let mut named_region_map = Default::default();
133134
let mut visitor = LifetimeContext { tcx, defs: &mut named_region_map, scope: ROOT_SCOPE };
134135
let node = tcx.hir().get_by_def_id(def_id);
@@ -160,7 +161,7 @@ impl RegionExt for Region {
160161

161162
struct LifetimeContext<'a, 'tcx> {
162163
tcx: TyCtxt<'tcx>,
163-
defs: &'a mut FxHashMap<ItemLocalId, Region>,
164+
defs: &'a mut FxHashMap<ItemLocalId, ty::Region<'tcx>>,
164165
scope: ScopeRef<'a>,
165166
}
166167

@@ -586,6 +587,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
586587
}
587588
};
588589
let def = lifetime.shifted(late_depth);
590+
let def = <dyn AstConv<'tcx>>::ast_region_to_region_inner(self.tcx, def);
589591
debug!(?def);
590592
self.defs.insert(lifetime_ref.hir_id.local_id, def);
591593
}

0 commit comments

Comments
 (0)