Skip to content

Commit 69d8183

Browse files
committed
Store LocalDefId in is_late_bound_map.
This allows to avoid looking at HIR from borrowck.
1 parent db03a2d commit 69d8183

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

compiler/rustc_borrowck/src/universal_regions.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -828,13 +828,11 @@ fn for_each_late_bound_region_defined_on<'tcx>(
828828
mut f: impl FnMut(ty::Region<'tcx>),
829829
) {
830830
if let Some((owner, late_bounds)) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
831-
for &late_bound in late_bounds.iter() {
832-
let region_def_id =
833-
tcx.hir().local_def_id(HirId { owner, local_id: late_bound }).to_def_id();
834-
let name = tcx.item_name(region_def_id);
831+
for &region_def_id in late_bounds.iter() {
832+
let name = tcx.item_name(region_def_id.to_def_id());
835833
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
836834
scope: owner.to_def_id(),
837-
bound_region: ty::BoundRegionKind::BrNamed(region_def_id, name),
835+
bound_region: ty::BoundRegionKind::BrNamed(region_def_id.to_def_id(), name),
838836
}));
839837
f(liberated_region);
840838
}

compiler/rustc_middle/src/middle/resolve_lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct ResolveLifetimes {
6464
/// Set of lifetime def ids that are late-bound; a region can
6565
/// be late-bound if (a) it does NOT appear in a where-clause and
6666
/// (b) it DOES appear in the arguments.
67-
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
67+
pub late_bound: FxHashMap<LocalDefId, FxHashSet<LocalDefId>>,
6868

6969
pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
7070
}

compiler/rustc_middle/src/query/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,7 @@ rustc_queries! {
15021502
Option<&'tcx FxHashMap<ItemLocalId, Region>> {
15031503
desc { "looking up a named region" }
15041504
}
1505-
query is_late_bound_map(_: LocalDefId) ->
1506-
Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
1505+
query is_late_bound_map(_: LocalDefId) -> Option<(LocalDefId, &'tcx FxHashSet<LocalDefId>)> {
15071506
desc { "testing if a region is late bound" }
15081507
}
15091508
/// For a given item (like a struct), gets the default lifetimes to be used

compiler/rustc_middle/src/ty/context.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2772,11 +2772,6 @@ impl<'tcx> TyCtxt<'tcx> {
27722772
self.named_region_map(id.owner).and_then(|map| map.get(&id.local_id).cloned())
27732773
}
27742774

2775-
pub fn is_late_bound(self, id: HirId) -> bool {
2776-
self.is_late_bound_map(id.owner)
2777-
.map_or(false, |(owner, set)| owner == id.owner && set.contains(&id.local_id))
2778-
}
2779-
27802775
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
27812776
self.mk_bound_variable_kinds(
27822777
self.late_bound_vars_map(id.owner)

compiler/rustc_resolve/src/late/lifetimes.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,15 @@ fn resolve_lifetimes_trait_definition(
427427
tcx: TyCtxt<'_>,
428428
local_def_id: LocalDefId,
429429
) -> ResolveLifetimes {
430-
convert_named_region_map(do_resolve(tcx, local_def_id, true, false))
430+
convert_named_region_map(tcx, do_resolve(tcx, local_def_id, true, false))
431431
}
432432

433433
/// Computes the `ResolveLifetimes` map that contains data for an entire `Item`.
434434
/// You should not read the result of this query directly, but rather use
435435
/// `named_region_map`, `is_late_bound_map`, etc.
436436
#[tracing::instrument(level = "debug", skip(tcx))]
437437
fn resolve_lifetimes(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> ResolveLifetimes {
438-
convert_named_region_map(do_resolve(tcx, local_def_id, false, false))
438+
convert_named_region_map(tcx, do_resolve(tcx, local_def_id, false, false))
439439
}
440440

441441
fn do_resolve(
@@ -468,7 +468,7 @@ fn do_resolve(
468468
named_region_map
469469
}
470470

471-
fn convert_named_region_map(named_region_map: NamedRegionMap) -> ResolveLifetimes {
471+
fn convert_named_region_map(tcx: TyCtxt<'_>, named_region_map: NamedRegionMap) -> ResolveLifetimes {
472472
let mut rl = ResolveLifetimes::default();
473473

474474
for (hir_id, v) in named_region_map.defs {
@@ -477,7 +477,8 @@ fn convert_named_region_map(named_region_map: NamedRegionMap) -> ResolveLifetime
477477
}
478478
for hir_id in named_region_map.late_bound {
479479
let map = rl.late_bound.entry(hir_id.owner).or_default();
480-
map.insert(hir_id.local_id);
480+
let def_id = tcx.hir().local_def_id(hir_id);
481+
map.insert(def_id);
481482
}
482483
for (hir_id, v) in named_region_map.late_bound_vars {
483484
let map = rl.late_bound_vars.entry(hir_id.owner).or_default();
@@ -537,7 +538,7 @@ fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId {
537538
fn is_late_bound_map<'tcx>(
538539
tcx: TyCtxt<'tcx>,
539540
def_id: LocalDefId,
540-
) -> Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
541+
) -> Option<(LocalDefId, &'tcx FxHashSet<LocalDefId>)> {
541542
match tcx.def_kind(def_id) {
542543
DefKind::AnonConst | DefKind::InlineConst => {
543544
let mut def_id = tcx
@@ -774,8 +775,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
774775
});
775776
}
776777
for (&owner, late_bound) in resolved_lifetimes.late_bound.iter() {
777-
late_bound.iter().for_each(|&local_id| {
778-
self.map.late_bound.insert(hir::HirId { owner, local_id });
778+
late_bound.iter().for_each(|&id| {
779+
let hir_id = self.tcx.local_def_id_to_hir_id(id);
780+
debug_assert_eq!(owner, hir_id.owner);
781+
self.map.late_bound.insert(hir_id);
779782
});
780783
}
781784
for (&owner, late_bound_vars) in

compiler/rustc_typeck/src/collect.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
13881388

13891389
fn has_late_bound_regions<'tcx>(
13901390
tcx: TyCtxt<'tcx>,
1391+
def_id: LocalDefId,
13911392
generics: &'tcx hir::Generics<'tcx>,
13921393
decl: &'tcx hir::FnDecl<'tcx>,
13931394
) -> Option<Span> {
@@ -1396,9 +1397,14 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
13961397
outer_index: ty::INNERMOST,
13971398
has_late_bound_regions: None,
13981399
};
1400+
let late_bound_map = tcx.is_late_bound_map(def_id);
1401+
let is_late_bound = |id| {
1402+
let id = tcx.hir().local_def_id(id);
1403+
late_bound_map.map_or(false, |(_, set)| set.contains(&id))
1404+
};
13991405
for param in generics.params {
14001406
if let GenericParamKind::Lifetime { .. } = param.kind {
1401-
if tcx.is_late_bound(param.hir_id) {
1407+
if is_late_bound(param.hir_id) {
14021408
return Some(param.span);
14031409
}
14041410
}
@@ -1410,25 +1416,25 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
14101416
match node {
14111417
Node::TraitItem(item) => match item.kind {
14121418
hir::TraitItemKind::Fn(ref sig, _) => {
1413-
has_late_bound_regions(tcx, &item.generics, sig.decl)
1419+
has_late_bound_regions(tcx, item.def_id, &item.generics, sig.decl)
14141420
}
14151421
_ => None,
14161422
},
14171423
Node::ImplItem(item) => match item.kind {
14181424
hir::ImplItemKind::Fn(ref sig, _) => {
1419-
has_late_bound_regions(tcx, &item.generics, sig.decl)
1425+
has_late_bound_regions(tcx, item.def_id, &item.generics, sig.decl)
14201426
}
14211427
_ => None,
14221428
},
14231429
Node::ForeignItem(item) => match item.kind {
14241430
hir::ForeignItemKind::Fn(fn_decl, _, ref generics) => {
1425-
has_late_bound_regions(tcx, generics, fn_decl)
1431+
has_late_bound_regions(tcx, item.def_id, generics, fn_decl)
14261432
}
14271433
_ => None,
14281434
},
14291435
Node::Item(item) => match item.kind {
14301436
hir::ItemKind::Fn(ref sig, .., ref generics, _) => {
1431-
has_late_bound_regions(tcx, generics, sig.decl)
1437+
has_late_bound_regions(tcx, item.def_id, generics, sig.decl)
14321438
}
14331439
_ => None,
14341440
},
@@ -1677,7 +1683,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16771683
params.push(opt_self);
16781684
}
16791685

1680-
let early_lifetimes = early_bound_lifetimes_from_generics(tcx, ast_generics);
1686+
let early_lifetimes = early_bound_lifetimes_from_generics(tcx, hir_id.owner, ast_generics);
16811687
params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
16821688
name: param.name.ident().name,
16831689
index: own_start + i as u32,
@@ -2034,10 +2040,23 @@ fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
20342040
/// `resolve_lifetime::early_bound_lifetimes`.
20352041
fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>(
20362042
tcx: TyCtxt<'tcx>,
2043+
def_id: LocalDefId,
20372044
generics: &'a hir::Generics<'a>,
20382045
) -> impl Iterator<Item = &'a hir::GenericParam<'a>> + Captures<'tcx> {
2046+
let late_bound_map = if generics.params.is_empty() {
2047+
// This function may be called on `def_id == CRATE_DEF_ID`,
2048+
// which makes `is_late_bound_map` ICE. Don't even try if there
2049+
// is no generic parameter.
2050+
None
2051+
} else {
2052+
tcx.is_late_bound_map(def_id)
2053+
};
2054+
let is_late_bound = move |hir_id| {
2055+
let id = tcx.hir().local_def_id(hir_id);
2056+
late_bound_map.map_or(false, |(_, set)| set.contains(&id))
2057+
};
20392058
generics.params.iter().filter(move |param| match param.kind {
2040-
GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
2059+
GenericParamKind::Lifetime { .. } => !is_late_bound(param.hir_id),
20412060
_ => false,
20422061
})
20432062
}
@@ -2221,7 +2240,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
22212240
// well. In the case of parameters declared on a fn or method, we
22222241
// have to be careful to only iterate over early-bound regions.
22232242
let mut index = parent_count + has_own_self as u32;
2224-
for param in early_bound_lifetimes_from_generics(tcx, ast_generics) {
2243+
for param in early_bound_lifetimes_from_generics(tcx, hir_id.owner, ast_generics) {
22252244
let region = tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
22262245
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
22272246
index,

0 commit comments

Comments
 (0)