Skip to content

Commit 19a4520

Browse files
committed
Take a LocalDefId in expect_*item.
1 parent 84c2a85 commit 19a4520

File tree

27 files changed

+111
-154
lines changed

27 files changed

+111
-154
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
30123012
this_decl_ty,
30133013
CItemKind::Declaration,
30143014
) {
3015-
let orig_fi = tcx.hir().expect_foreign_item(existing_hid);
3015+
let orig_fi = tcx.hir().expect_foreign_item(existing_hid.expect_owner());
30163016
let orig = Self::name_of_extern_decl(tcx, orig_fi);
30173017

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

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,7 @@ impl EncodeContext<'a, 'tcx> {
11461146
debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
11471147
let tcx = self.tcx;
11481148

1149-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1150-
let ast_item = tcx.hir().expect_trait_item(hir_id);
1149+
let ast_item = tcx.hir().expect_trait_item(def_id.expect_local());
11511150
let trait_item = tcx.associated_item(def_id);
11521151

11531152
let container = match trait_item.defaultness {
@@ -1215,8 +1214,7 @@ impl EncodeContext<'a, 'tcx> {
12151214
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
12161215
let tcx = self.tcx;
12171216

1218-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1219-
let ast_item = self.tcx.hir().expect_impl_item(hir_id);
1217+
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
12201218
let impl_item = self.tcx.associated_item(def_id);
12211219

12221220
let container = match impl_item.defaultness {

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -867,24 +867,24 @@ impl<'hir> Map<'hir> {
867867
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
868868
}
869869

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

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

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

@@ -895,10 +895,12 @@ impl<'hir> Map<'hir> {
895895
}
896896
}
897897

898-
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> {
899-
match self.tcx.hir_owner(id.expect_owner()) {
898+
pub fn expect_foreign_item(&self, id: LocalDefId) -> &'hir ForeignItem<'hir> {
899+
match self.tcx.hir_owner(id) {
900900
Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
901-
_ => bug!("expected foreign item, found {}", self.node_to_string(id)),
901+
_ => {
902+
bug!("expected foreign item, found {}", self.node_to_string(HirId::make_owner(id)))
903+
}
902904
}
903905
}
904906

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,7 @@ fn foo(&self) -> Self::T { String::new() }
774774
if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() {
775775
let opaque_local_def_id = def_id.as_local();
776776
let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
777-
let hir = self.hir();
778-
let opaque_hir_id = hir.local_def_id_to_hir_id(opaque_local_def_id);
779-
match &hir.expect_item(opaque_hir_id).kind {
777+
match &self.hir().expect_item(opaque_local_def_id).kind {
780778
hir::ItemKind::OpaqueTy(opaque_hir_ty) => opaque_hir_ty,
781779
_ => bug!("The HirId comes from a `ty::Opaque`"),
782780
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
3131
match impl_item.kind {
3232
hir::ImplItemKind::Const(..) => Target::AssocConst,
3333
hir::ImplItemKind::Fn(..) => {
34-
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id());
34+
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()).expect_owner();
3535
let containing_item = tcx.hir().expect_item(parent_hir_id);
3636
let containing_impl_is_for_trait = match &containing_item.kind {
3737
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
@@ -565,7 +565,7 @@ impl CheckAttrVisitor<'tcx> {
565565
Target::Impl => Some("implementation block"),
566566
Target::ForeignMod => Some("extern block"),
567567
Target::AssocTy => {
568-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
568+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
569569
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
570570
if Target::from_item(containing_item) == Target::Impl {
571571
Some("type alias in implementation block")
@@ -574,7 +574,7 @@ impl CheckAttrVisitor<'tcx> {
574574
}
575575
}
576576
Target::AssocConst => {
577-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
577+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
578578
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
579579
// We can't link to trait impl's consts.
580580
let err = "associated constant in trait implementation block";

compiler/rustc_passes/src/reachable.rs

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ impl EmbargoVisitor<'tcx> {
560560
// have normal hygine, so we can treat them like other items without type
561561
// privacy and mark them reachable.
562562
DefKind::Macro(_) => {
563-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
564-
let item = self.tcx.hir().expect_item(hir_id);
563+
let item = self.tcx.hir().expect_item(def_id);
565564
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }) = item.kind {
566565
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
567566
self.update(def_id, level);
@@ -583,7 +582,7 @@ impl EmbargoVisitor<'tcx> {
583582
// While structs and unions have type privacy, their fields do not.
584583
if let ty::Visibility::Public = vis {
585584
let item =
586-
self.tcx.hir().expect_item(self.tcx.hir().local_def_id_to_hir_id(def_id));
585+
self.tcx.hir().expect_item(def_id);
587586
if let hir::ItemKind::Struct(ref struct_def, _)
588587
| hir::ItemKind::Union(ref struct_def, _) = item.kind
589588
{
@@ -653,9 +652,7 @@ impl EmbargoVisitor<'tcx> {
653652
// If the module is `self`, i.e. the current crate,
654653
// there will be no corresponding item.
655654
.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-
})
655+
.and_then(|def_id| def_id.as_local())
659656
.map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id))
660657
{
661658
if let hir::ItemKind::Mod(m) = &item.kind {

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 6 additions & 6 deletions
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(),
@@ -1137,7 +1137,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11371137
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
11381138
let tcx = self.tcx;
11391139
self.visit_early_late(
1140-
Some(tcx.hir().get_parent_item(trait_item.hir_id())),
1140+
Some(tcx.hir().get_parent_did(trait_item.hir_id())),
11411141
trait_item.hir_id(),
11421142
&sig.decl,
11431143
&trait_item.generics,
@@ -1206,7 +1206,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12061206
self.missing_named_lifetime_spots.push((&impl_item.generics).into());
12071207
let tcx = self.tcx;
12081208
self.visit_early_late(
1209-
Some(tcx.hir().get_parent_item(impl_item.hir_id())),
1209+
Some(tcx.hir().get_parent_did(impl_item.hir_id())),
12101210
impl_item.hir_id(),
12111211
&sig.decl,
12121212
&impl_item.generics,
@@ -2185,7 +2185,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21852185
/// ordering is not important there.
21862186
fn visit_early_late<F>(
21872187
&mut self,
2188-
parent_id: Option<hir::HirId>,
2188+
parent_id: Option<LocalDefId>,
21892189
hir_id: hir::HirId,
21902190
decl: &'tcx hir::FnDecl<'tcx>,
21912191
generics: &'tcx hir::Generics<'tcx>,
@@ -2772,7 +2772,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
27722772

27732773
Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => {
27742774
if let hir::ItemKind::Trait(.., ref trait_items) =
2775-
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
2775+
self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
27762776
{
27772777
assoc_item_kind =
27782778
trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind);
@@ -2785,7 +2785,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
27852785

27862786
Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => {
27872787
if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) =
2788-
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
2788+
self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
27892789
{
27902790
impl_self = Some(self_ty);
27912791
assoc_item_kind =

compiler/rustc_trait_selection/src/opaque_types.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -780,31 +780,28 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
780780
// }
781781
// ```
782782
if let Some(def_id) = def_id.as_local() {
783-
let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
784783
let parent_def_id = self.infcx.defining_use_anchor;
785784
let def_scope_default = || {
785+
let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
786786
let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id);
787787
parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id)
788788
};
789-
let (in_definition_scope, origin) =
790-
match tcx.hir().expect_item(opaque_hir_id).kind {
791-
// Anonymous `impl Trait`
792-
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
793-
impl_trait_fn: Some(parent),
794-
origin,
795-
..
796-
}) => (parent == parent_def_id.to_def_id(), origin),
797-
// Named `type Foo = impl Bar;`
798-
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
799-
impl_trait_fn: None,
800-
origin,
801-
..
802-
}) => (
803-
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
804-
origin,
805-
),
806-
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
807-
};
789+
let (in_definition_scope, origin) = match tcx.hir().expect_item(def_id).kind
790+
{
791+
// Anonymous `impl Trait`
792+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
793+
impl_trait_fn: Some(parent),
794+
origin,
795+
..
796+
}) => (parent == parent_def_id.to_def_id(), origin),
797+
// Named `type Foo = impl Bar;`
798+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
799+
impl_trait_fn: None,
800+
origin,
801+
..
802+
}) => (may_define_opaque_type(tcx, parent_def_id, def_id), origin),
803+
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
804+
};
808805
if in_definition_scope {
809806
let opaque_type_key =
810807
OpaqueTypeKey { def_id: def_id.to_def_id(), substs };
@@ -937,8 +934,9 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
937934
/// Here, `def_id` is the `LocalDefId` of the defining use of the opaque type (e.g., `f1` or `f2`),
938935
/// and `opaque_hir_id` is the `HirId` of the definition of the opaque type `Baz`.
939936
/// For the above example, this function returns `true` for `f1` and `false` for `f2`.
940-
fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hir::HirId) -> bool {
937+
fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_def_id: LocalDefId) -> bool {
941938
let mut hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
939+
let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(opaque_def_id);
942940

943941
// Named opaque types can be defined by any siblings or children of siblings.
944942
let scope = tcx.hir().get_defining_scope(opaque_hir_id);

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 4 additions & 7 deletions
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()),

0 commit comments

Comments
 (0)