Skip to content

Commit 6db0a0e

Browse files
committed
Auto merge of #91299 - cjgillot:expect-ldid, r=petrochenkov
Take a LocalDefId in expect_*item. Items and item-likes are always HIR owners. When trying to find such nodes, there is no ambiguity, the `LocalDefId` and the `HirId::owner` always match. In such cases, `local_def_id_to_hir_id` does not carry any meaningful information, so we can just skip calling it altogether.
2 parents 8b95491 + 5fb4648 commit 6db0a0e

File tree

28 files changed

+95
-137
lines changed

28 files changed

+95
-137
lines changed

compiler/rustc_infer/src/infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
467467
parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id)
468468
};
469469
let (in_definition_scope, origin) =
470-
match tcx.hir().expect_item(opaque_hir_id).kind {
470+
match tcx.hir().expect_item(def_id).kind {
471471
// Anonymous `impl Trait`
472472
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
473473
impl_trait_fn: Some(parent),

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3011,7 +3011,7 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
30113011
this_decl_ty,
30123012
CItemKind::Declaration,
30133013
) {
3014-
let orig_fi = tcx.hir().expect_foreign_item(existing_hid);
3014+
let orig_fi = tcx.hir().expect_foreign_item(existing_hid.expect_owner());
30153015
let orig = Self::name_of_extern_decl(tcx, orig_fi);
30163016

30173017
// We want to ensure that we use spans for both decls that include where the

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,7 @@ impl EncodeContext<'a, 'tcx> {
11521152
debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
11531153
let tcx = self.tcx;
11541154

1155-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1156-
let ast_item = tcx.hir().expect_trait_item(hir_id);
1155+
let ast_item = tcx.hir().expect_trait_item(def_id.expect_local());
11571156
let trait_item = tcx.associated_item(def_id);
11581157

11591158
let container = match trait_item.defaultness {
@@ -1221,8 +1220,7 @@ impl EncodeContext<'a, 'tcx> {
12211220
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
12221221
let tcx = self.tcx;
12231222

1224-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1225-
let ast_item = self.tcx.hir().expect_impl_item(hir_id);
1223+
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
12261224
let impl_item = self.tcx.associated_item(def_id);
12271225

12281226
let container = match impl_item.defaultness {

compiler/rustc_middle/src/hir/map/mod.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -869,24 +869,24 @@ impl<'hir> Map<'hir> {
869869
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
870870
}
871871

872-
pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> {
873-
match self.tcx.hir_owner(id.expect_owner()) {
872+
pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> {
873+
match self.tcx.hir_owner(id) {
874874
Some(Owner { node: OwnerNode::Item(item), .. }) => item,
875-
_ => bug!("expected item, found {}", self.node_to_string(id)),
875+
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
876876
}
877877
}
878878

879-
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> {
880-
match self.tcx.hir_owner(id.expect_owner()) {
879+
pub fn expect_impl_item(&self, id: LocalDefId) -> &'hir ImplItem<'hir> {
880+
match self.tcx.hir_owner(id) {
881881
Some(Owner { node: OwnerNode::ImplItem(item), .. }) => item,
882-
_ => bug!("expected impl item, found {}", self.node_to_string(id)),
882+
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
883883
}
884884
}
885885

886-
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> {
887-
match self.tcx.hir_owner(id.expect_owner()) {
886+
pub fn expect_trait_item(&self, id: LocalDefId) -> &'hir TraitItem<'hir> {
887+
match self.tcx.hir_owner(id) {
888888
Some(Owner { node: OwnerNode::TraitItem(item), .. }) => item,
889-
_ => bug!("expected trait item, found {}", self.node_to_string(id)),
889+
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
890890
}
891891
}
892892

@@ -897,10 +897,12 @@ impl<'hir> Map<'hir> {
897897
}
898898
}
899899

900-
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> {
901-
match self.tcx.hir_owner(id.expect_owner()) {
900+
pub fn expect_foreign_item(&self, id: LocalDefId) -> &'hir ForeignItem<'hir> {
901+
match self.tcx.hir_owner(id) {
902902
Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
903-
_ => bug!("expected foreign item, found {}", self.node_to_string(id)),
903+
_ => {
904+
bug!("expected foreign item, found {}", self.node_to_string(HirId::make_owner(id)))
905+
}
904906
}
905907
}
906908

compiler/rustc_middle/src/ty/error.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,7 @@ fn foo(&self) -> Self::T { String::new() }
777777
if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() {
778778
let opaque_local_def_id = def_id.as_local();
779779
let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
780-
let hir = self.hir();
781-
let opaque_hir_id = hir.local_def_id_to_hir_id(opaque_local_def_id);
782-
match &hir.expect_item(opaque_hir_id).kind {
780+
match &self.hir().expect_item(opaque_local_def_id).kind {
783781
hir::ItemKind::OpaqueTy(opaque_hir_ty) => opaque_hir_ty,
784782
_ => bug!("The HirId comes from a `ty::Opaque`"),
785783
}

compiler/rustc_passes/src/check_attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
3232
match impl_item.kind {
3333
hir::ImplItemKind::Const(..) => Target::AssocConst,
3434
hir::ImplItemKind::Fn(..) => {
35-
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id());
35+
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()).expect_owner();
3636
let containing_item = tcx.hir().expect_item(parent_hir_id);
3737
let containing_impl_is_for_trait = match &containing_item.kind {
3838
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
@@ -582,7 +582,7 @@ impl CheckAttrVisitor<'tcx> {
582582
Target::Impl => Some("implementation block"),
583583
Target::ForeignMod => Some("extern block"),
584584
Target::AssocTy => {
585-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
585+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
586586
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
587587
if Target::from_item(containing_item) == Target::Impl {
588588
Some("type alias in implementation block")
@@ -591,7 +591,7 @@ impl CheckAttrVisitor<'tcx> {
591591
}
592592
}
593593
Target::AssocConst => {
594-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
594+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
595595
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
596596
// We can't link to trait impl's consts.
597597
let err = "associated constant in trait implementation block";

compiler/rustc_passes/src/reachable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ impl<'tcx> ReachableContext<'tcx> {
173173
// Check the impl. If the generics on the self
174174
// type of the impl require inlining, this method
175175
// does too.
176-
let impl_hir_id = self.tcx.hir().local_def_id_to_hir_id(impl_did);
177-
match self.tcx.hir().expect_item(impl_hir_id).kind {
176+
match self.tcx.hir().expect_item(impl_did).kind {
178177
hir::ItemKind::Impl { .. } => {
179178
let generics = self.tcx.generics_of(impl_did);
180179
generics.requires_monomorphization(self.tcx)

compiler/rustc_privacy/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,7 @@ impl EmbargoVisitor<'tcx> {
559559
// have normal hygine, so we can treat them like other items without type
560560
// privacy and mark them reachable.
561561
DefKind::Macro(_) => {
562-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
563-
let item = self.tcx.hir().expect_item(hir_id);
562+
let item = self.tcx.hir().expect_item(def_id);
564563
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }) = item.kind {
565564
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
566565
self.update(def_id, level);
@@ -581,8 +580,7 @@ impl EmbargoVisitor<'tcx> {
581580
DefKind::Struct | DefKind::Union => {
582581
// While structs and unions have type privacy, their fields do not.
583582
if vis.is_public() {
584-
let item =
585-
self.tcx.hir().expect_item(self.tcx.hir().local_def_id_to_hir_id(def_id));
583+
let item = self.tcx.hir().expect_item(def_id);
586584
if let hir::ItemKind::Struct(ref struct_def, _)
587585
| hir::ItemKind::Union(ref struct_def, _) = item.kind
588586
{
@@ -653,9 +651,7 @@ impl EmbargoVisitor<'tcx> {
653651
// If the module is `self`, i.e. the current crate,
654652
// there will be no corresponding item.
655653
.filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE)
656-
.and_then(|def_id| {
657-
def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
658-
})
654+
.and_then(|def_id| def_id.as_local())
659655
.map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id))
660656
{
661657
if let hir::ItemKind::Mod(m) = &item.kind {

compiler/rustc_resolve/src/late/lifetimes.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ fn do_resolve(
445445
trait_definition_only: bool,
446446
with_scope_for_path: bool,
447447
) -> NamedRegionMap {
448-
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(local_def_id));
448+
let item = tcx.hir().expect_item(local_def_id);
449449
let mut named_region_map = NamedRegionMap {
450450
defs: Default::default(),
451451
late_bound: Default::default(),
@@ -1134,7 +1134,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11341134
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
11351135
let tcx = self.tcx;
11361136
self.visit_early_late(
1137-
Some(tcx.hir().get_parent_item(trait_item.hir_id())),
1137+
Some(tcx.hir().get_parent_did(trait_item.hir_id())),
11381138
trait_item.hir_id(),
11391139
&sig.decl,
11401140
&trait_item.generics,
@@ -1203,7 +1203,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12031203
self.missing_named_lifetime_spots.push((&impl_item.generics).into());
12041204
let tcx = self.tcx;
12051205
self.visit_early_late(
1206-
Some(tcx.hir().get_parent_item(impl_item.hir_id())),
1206+
Some(tcx.hir().get_parent_did(impl_item.hir_id())),
12071207
impl_item.hir_id(),
12081208
&sig.decl,
12091209
&impl_item.generics,
@@ -2176,7 +2176,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21762176
/// ordering is not important there.
21772177
fn visit_early_late<F>(
21782178
&mut self,
2179-
parent_id: Option<hir::HirId>,
2179+
parent_id: Option<LocalDefId>,
21802180
hir_id: hir::HirId,
21812181
decl: &'tcx hir::FnDecl<'tcx>,
21822182
generics: &'tcx hir::Generics<'tcx>,
@@ -2758,7 +2758,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
27582758

27592759
Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => {
27602760
if let hir::ItemKind::Trait(.., ref trait_items) =
2761-
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
2761+
self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
27622762
{
27632763
assoc_item_kind =
27642764
trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind);
@@ -2771,7 +2771,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
27712771

27722772
Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => {
27732773
if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) =
2774-
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
2774+
self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
27752775
{
27762776
impl_self = Some(self_ty);
27772777
assoc_item_kind =

compiler/rustc_ty_utils/src/ty.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
123123
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
124124
let parent_id = tcx.hir().get_parent_item(id);
125125
let parent_def_id = tcx.hir().local_def_id(parent_id);
126-
let parent_item = tcx.hir().expect_item(parent_id);
126+
let parent_item = tcx.hir().expect_item(parent_def_id);
127127
match parent_item.kind {
128128
hir::ItemKind::Impl(ref impl_) => {
129129
if let Some(impl_item_ref) =
@@ -158,8 +158,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
158158
}
159159

160160
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
161-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
162-
let item = tcx.hir().expect_item(hir_id);
161+
let item = tcx.hir().expect_item(def_id.expect_local());
163162
if let hir::ItemKind::Impl(impl_) = &item.kind {
164163
impl_.defaultness
165164
} else {
@@ -168,8 +167,7 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
168167
}
169168

170169
fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
171-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
172-
let item = tcx.hir().expect_item(hir_id);
170+
let item = tcx.hir().expect_item(def_id.expect_local());
173171
if let hir::ItemKind::Impl(impl_) = &item.kind {
174172
impl_.constness
175173
} else {
@@ -202,8 +200,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
202200
}
203201

204202
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
205-
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
206-
let item = tcx.hir().expect_item(id);
203+
let item = tcx.hir().expect_item(def_id.expect_local());
207204
match item.kind {
208205
hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
209206
trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.def_id.to_def_id()),

compiler/rustc_typeck/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
458458
def_id: LocalDefId,
459459
span: Span,
460460
) {
461-
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(def_id));
461+
let item = tcx.hir().expect_item(def_id);
462462
debug!(?item, ?span);
463463

464464
struct FoundParentLifetime;

compiler/rustc_typeck/src/check/coercion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1637,11 +1637,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16371637
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
16381638
// Get the `impl Trait`'s `DefId`.
16391639
if let ty::Opaque(def_id, _) = ty.kind() {
1640-
let hir_id = fcx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
16411640
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and
16421641
// get the `Trait`'s `DefId`.
16431642
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) =
1644-
fcx.tcx.hir().expect_item(hir_id).kind
1643+
fcx.tcx.hir().expect_item(def_id.expect_local()).kind
16451644
{
16461645
// Are of this `impl Trait`'s traits object safe?
16471646
is_object_safe = bounds.iter().all(|bound| {

0 commit comments

Comments
 (0)