Skip to content

Commit f9d61cd

Browse files
committed
Auto merge of #90146 - cjgillot:no-id-map, r=nagisa
Reduce use of LocalDefId <-> HirId maps This is an attempt to reduce the perf effect of #89278. r? `@ghost`
2 parents ec4bcaa + 8617ff0 commit f9d61cd

File tree

87 files changed

+374
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+374
-413
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
409409
let generics = tcx.generics_of(self.mir_def_id());
410410
let param = generics.type_param(&param_ty, tcx);
411411
if let Some(generics) = tcx
412-
.hir()
413-
.get_generics(tcx.typeck_root_def_id(self.mir_def_id().to_def_id()))
412+
.typeck_root_def_id(self.mir_def_id().to_def_id())
413+
.as_local()
414+
.and_then(|def_id| tcx.hir().get_generics(def_id))
414415
{
415416
suggest_constraining_type_param(
416417
tcx,

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+32-36
Original file line numberDiff line numberDiff line change
@@ -628,42 +628,39 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
628628
};
629629
(
630630
true,
631-
td.as_local().and_then(|tld| {
632-
let h = hir_map.local_def_id_to_hir_id(tld);
633-
match hir_map.find(h) {
634-
Some(Node::Item(hir::Item {
635-
kind: hir::ItemKind::Trait(_, _, _, _, items),
636-
..
637-
})) => {
638-
let mut f_in_trait_opt = None;
639-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
640-
let hi = fi.hir_id();
641-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
642-
continue;
643-
}
644-
if hir_map.name(hi) != hir_map.name(my_hir) {
645-
continue;
646-
}
647-
f_in_trait_opt = Some(hi);
648-
break;
631+
td.as_local().and_then(|tld| match hir_map.find_by_def_id(tld) {
632+
Some(Node::Item(hir::Item {
633+
kind: hir::ItemKind::Trait(_, _, _, _, items),
634+
..
635+
})) => {
636+
let mut f_in_trait_opt = None;
637+
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
638+
let hi = fi.hir_id();
639+
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
640+
continue;
649641
}
650-
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
651-
Some(Node::TraitItem(hir::TraitItem {
652-
kind:
653-
hir::TraitItemKind::Fn(
654-
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
655-
_,
656-
),
657-
..
658-
})) => {
659-
let hir::Ty { span, .. } = inputs[local.index() - 1];
660-
Some(span)
661-
}
662-
_ => None,
663-
})
642+
if hir_map.name(hi) != hir_map.name(my_hir) {
643+
continue;
644+
}
645+
f_in_trait_opt = Some(hi);
646+
break;
664647
}
665-
_ => None,
648+
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
649+
Some(Node::TraitItem(hir::TraitItem {
650+
kind:
651+
hir::TraitItemKind::Fn(
652+
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
653+
_,
654+
),
655+
..
656+
})) => {
657+
let hir::Ty { span, .. } = inputs[local.index() - 1];
658+
Some(span)
659+
}
660+
_ => None,
661+
})
666662
}
663+
_ => None,
667664
}),
668665
)
669666
}
@@ -896,7 +893,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
896893
if look_at_return && hir.get_return_block(closure_id).is_some() {
897894
// ...otherwise we are probably in the tail expression of the function, point at the
898895
// return type.
899-
match hir.get(hir.get_parent_item(fn_call_id)) {
896+
match hir.get_by_def_id(hir.get_parent_item(fn_call_id)) {
900897
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
901898
| hir::Node::TraitItem(hir::TraitItem {
902899
ident,
@@ -1075,8 +1072,7 @@ fn get_mut_span_in_struct_field<'tcx>(
10751072
if let ty::Adt(def, _) = ty.kind() {
10761073
let field = def.all_fields().nth(field.index())?;
10771074
// Use the HIR types to construct the diagnostic message.
1078-
let hir_id = tcx.hir().local_def_id_to_hir_id(field.did.as_local()?);
1079-
let node = tcx.hir().find(hir_id)?;
1075+
let node = tcx.hir().find_by_def_id(field.did.as_local()?)?;
10801076
// Now we're dealing with the actual struct that we're going to suggest a change to,
10811077
// we can expect a field that is an immutable reference to a type.
10821078
if let hir::Node::Field(field) = node {

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
704704
hir::AsyncGeneratorKind::Block => " of async block",
705705
hir::AsyncGeneratorKind::Closure => " of async closure",
706706
hir::AsyncGeneratorKind::Fn => {
707-
let parent_item = hir.get(hir.get_parent_item(mir_hir_id));
707+
let parent_item = hir.get_by_def_id(hir.get_parent_item(mir_hir_id));
708708
let output = &parent_item
709709
.fn_decl()
710710
.expect("generator lowered from async fn should be in fn")

Diff for: compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
7676
//
7777
// As a result, if this id is an FFI item (foreign item) then we only
7878
// let it through if it's included statically.
79-
match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
79+
match tcx.hir().get_by_def_id(def_id) {
8080
Node::ForeignItem(..) => {
8181
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
8282
}

Diff for: compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hir as hir;
2-
use rustc_hir::def_id::DefId;
2+
use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_middle::ty::query::Providers;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::symbol::Symbol;
@@ -15,7 +15,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
1515
}
1616
}
1717

18-
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
18+
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
19+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1920
let parent_id = tcx.hir().get_parent_node(hir_id);
2021
matches!(
2122
tcx.hir().get(parent_id),
@@ -29,15 +30,15 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
2930
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
3031
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
3132
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
32-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
33-
34-
let node = tcx.hir().get(hir_id);
33+
let def_id = def_id.expect_local();
34+
let node = tcx.hir().get_by_def_id(def_id);
3535

3636
if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
3737
node
3838
{
3939
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
4040
// foreign items cannot be evaluated at compile-time.
41+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
4142
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
4243
tcx.lookup_const_stability(def_id).is_some()
4344
} else {
@@ -50,7 +51,7 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
5051

5152
// If the function itself is not annotated with `const`, it may still be a `const fn`
5253
// if it resides in a const trait impl.
53-
is_parent_const_impl_raw(tcx, hir_id)
54+
is_parent_const_impl_raw(tcx, def_id)
5455
} else {
5556
matches!(node, hir::Node::Ctor(_))
5657
}

Diff for: compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
221221
// Prevent const trait methods from being annotated as `stable`.
222222
// FIXME: Do this as part of stability checking.
223223
if self.is_const_stable_const_fn() {
224-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
225-
if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) {
224+
if crate::const_eval::is_parent_const_impl_raw(tcx, def_id) {
226225
self.ccx
227226
.tcx
228227
.sess

Diff for: compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ impl Qualif for CustomEq {
210210
// because that component may be part of an enum variant (e.g.,
211211
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
212212
// structural-match (`Option::None`).
213-
let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id());
214-
traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some()
213+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
215214
}
216215

217216
fn in_adt_inherently<'tcx>(

Diff for: compiler/rustc_hir/src/hir_id.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ pub struct HirId {
1919
}
2020

2121
impl HirId {
22+
#[inline]
2223
pub fn expect_owner(self) -> LocalDefId {
2324
assert_eq!(self.local_id.index(), 0);
2425
self.owner
2526
}
2627

28+
#[inline]
2729
pub fn as_owner(self) -> Option<LocalDefId> {
2830
if self.local_id.index() == 0 { Some(self.owner) } else { None }
2931
}

Diff for: compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ impl<'tcx> DirtyCleanVisitor<'tcx> {
223223
/// Return all DepNode labels that should be asserted for this item.
224224
/// index=0 is the "name" used for error messages
225225
fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) {
226-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_id);
227-
let node = self.tcx.hir().get(hir_id);
226+
let node = self.tcx.hir().get_by_def_id(item_id);
228227
let (name, labels) = match node {
229228
HirNode::Item(item) => {
230229
match item.kind {

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
151151
) -> (String, Span) {
152152
let sm = tcx.sess.source_map();
153153

154-
let scope = region.free_region_binding_scope(tcx);
155-
let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local());
154+
let scope = region.free_region_binding_scope(tcx).expect_local();
156155
match *region {
157156
ty::ReEarlyBound(ref br) => {
158-
let mut sp = sm.guess_head_span(tcx.hir().span(node));
157+
let mut sp = sm.guess_head_span(tcx.def_span(scope));
159158
if let Some(param) =
160159
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
161160
{
@@ -166,7 +165,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
166165
ty::ReFree(ty::FreeRegion {
167166
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
168167
}) => {
169-
let mut sp = sm.guess_head_span(tcx.hir().span(node));
168+
let mut sp = sm.guess_head_span(tcx.def_span(scope));
170169
if let Some(param) =
171170
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
172171
{
@@ -181,13 +180,13 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
181180
} else {
182181
(
183182
format!("the anonymous lifetime #{} defined here", idx + 1),
184-
tcx.hir().span(node),
183+
tcx.def_span(scope),
185184
)
186185
}
187186
}
188187
_ => (
189188
format!("the lifetime `{}` as defined here", region),
190-
sm.guess_head_span(tcx.hir().span(node)),
189+
sm.guess_head_span(tcx.def_span(scope)),
191190
),
192191
},
193192
_ => bug!(),
@@ -1759,8 +1758,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17591758
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values {
17601759
if let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() {
17611760
if let Some(def_id) = def_id.as_local() {
1762-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1763-
let span = self.tcx.hir().span(hir_id);
1761+
let span = self.tcx.def_span(def_id);
17641762
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
17651763
}
17661764
}
@@ -2212,9 +2210,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22122210
if let Some(Node::Item(Item {
22132211
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
22142212
..
2215-
})) = hir.find(parent_id)
2213+
})) = hir.find_by_def_id(parent_id)
22162214
{
2217-
Some(self.tcx.generics_of(hir.local_def_id(parent_id).to_def_id()))
2215+
Some(self.tcx.generics_of(parent_id))
22182216
} else {
22192217
None
22202218
},
@@ -2245,7 +2243,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22452243
if let Node::GenericParam(param) = hir.get(id) {
22462244
has_bounds = !param.bounds.is_empty();
22472245
}
2248-
let sp = hir.span(id);
2246+
let sp = self.tcx.def_span(def_id);
22492247
// `sp` only covers `T`, change it so that it covers
22502248
// `T:` when appropriate
22512249
let is_impl_trait = bound_kind.to_string().starts_with("impl ");
@@ -2291,12 +2289,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22912289
.as_ref()
22922290
.and_then(|(_, g, _)| g.params.first())
22932291
.and_then(|param| param.def_id.as_local())
2294-
.map(|def_id| {
2295-
(
2296-
hir.span(hir.local_def_id_to_hir_id(def_id)).shrink_to_lo(),
2297-
format!("{}, ", new_lt),
2298-
)
2299-
});
2292+
.map(|def_id| (self.tcx.def_span(def_id).shrink_to_lo(), format!("{}, ", new_lt)));
23002293

23012294
let labeled_user_string = match bound_kind {
23022295
GenericKind::Param(ref p) => format!("the parameter type `{}`", p),

Diff for: compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
187187
| ObligationCauseCode::BlockTailExpression(hir_id) = cause.code()
188188
{
189189
let parent_id = tcx.hir().get_parent_item(*hir_id);
190+
let parent_id = tcx.hir().local_def_id_to_hir_id(parent_id);
190191
if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id) {
191192
let mut span: MultiSpan = fn_decl.output.span().into();
192193
let mut add_label = true;
@@ -425,7 +426,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
425426
let tcx = self.tcx();
426427
match tcx.hir().get_if_local(def_id) {
427428
Some(Node::ImplItem(impl_item)) => {
428-
match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) {
429+
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) {
429430
Some(Node::Item(Item {
430431
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
431432
..
@@ -434,13 +435,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
434435
}
435436
}
436437
Some(Node::TraitItem(trait_item)) => {
437-
let parent_id = tcx.hir().get_parent_item(trait_item.hir_id());
438-
match tcx.hir().find(parent_id) {
438+
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
439+
match tcx.hir().find_by_def_id(trait_did) {
439440
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
440441
// The method being called is defined in the `trait`, but the `'static`
441442
// obligation comes from the `impl`. Find that `impl` so that we can point
442443
// at it in the suggestion.
443-
let trait_did = tcx.hir().local_def_id(parent_id).to_def_id();
444+
let trait_did = trait_did.to_def_id();
444445
match tcx
445446
.hir()
446447
.trait_impls(trait_did)

Diff for: compiler/rustc_infer/src/infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hi
635635
let scope = tcx.hir().get_defining_scope(opaque_hir_id);
636636
// We walk up the node tree until we hit the root or the scope of the opaque type.
637637
while hir_id != scope && hir_id != hir::CRATE_HIR_ID {
638-
hir_id = tcx.hir().get_parent_item(hir_id);
638+
hir_id = tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(hir_id));
639639
}
640640
// Syntactically, we are allowed to define the concrete type if:
641641
let res = hir_id == scope;

Diff for: compiler/rustc_lint/src/builtin.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
610610
// reported for missing docs.
611611
let real_trait = trait_ref.path.res.def_id();
612612
let Some(def_id) = real_trait.as_local() else { return };
613-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
614-
let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) else { return };
613+
let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(def_id) else { return };
615614
if let hir::VisibilityKind::Inherited = item.vis.node {
616615
for impl_item_ref in items {
617616
self.private_traits.insert(impl_item_ref.id.hir_id());
@@ -656,7 +655,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
656655

657656
// If the method is an impl for an item with docs_hidden, don't doc.
658657
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl {
659-
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
658+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
660659
let impl_ty = cx.tcx.type_of(parent);
661660
let outerdef = match impl_ty.kind() {
662661
ty::Adt(def, _) => Some(def.did),
@@ -1212,7 +1211,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
12121211
check_no_mangle_on_generic_fn(
12131212
no_mangle_attr,
12141213
Some(generics),
1215-
cx.tcx.hir().get_generics(it.id.def_id.to_def_id()).unwrap(),
1214+
cx.tcx.hir().get_generics(it.id.def_id).unwrap(),
12161215
it.span,
12171216
);
12181217
}

0 commit comments

Comments
 (0)