Skip to content

Commit dd2dee1

Browse files
committed
Refactor check_item_type to work on LocalDefId instead of ItemId
1 parent e51e98d commit dd2dee1

File tree

1 file changed

+29
-34
lines changed
  • compiler/rustc_hir_analysis/src/check

1 file changed

+29
-34
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+29-34
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
198198

199199
/// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo`
200200
/// projections that would result in "inheriting lifetimes".
201-
fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
202-
let item = tcx.hir().item(id);
201+
fn check_opaque(tcx: TyCtxt<'_>, def_id: LocalDefId) {
202+
let item = tcx.hir().expect_item(def_id);
203203
let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
204204
tcx.dcx().span_delayed_bug(item.span, "expected opaque item");
205205
return;
@@ -440,40 +440,35 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
440440
}
441441
}
442442

443-
fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
444-
debug!(
445-
"check_item_type(it.def_id={:?}, it.name={})",
446-
id.owner_id,
447-
tcx.def_path_str(id.owner_id)
448-
);
443+
fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
449444
let _indenter = indenter();
450-
match tcx.def_kind(id.owner_id) {
445+
match tcx.def_kind(def_id) {
451446
DefKind::Static(..) => {
452-
tcx.ensure().typeck(id.owner_id.def_id);
453-
maybe_check_static_with_link_section(tcx, id.owner_id.def_id);
454-
check_static_inhabited(tcx, id.owner_id.def_id);
455-
check_static_linkage(tcx, id.owner_id.def_id);
447+
tcx.ensure().typeck(def_id);
448+
maybe_check_static_with_link_section(tcx, def_id);
449+
check_static_inhabited(tcx, def_id);
450+
check_static_linkage(tcx, def_id);
456451
}
457452
DefKind::Const => {
458-
tcx.ensure().typeck(id.owner_id.def_id);
453+
tcx.ensure().typeck(def_id);
459454
}
460455
DefKind::Enum => {
461-
check_enum(tcx, id.owner_id.def_id);
456+
check_enum(tcx, def_id);
462457
}
463458
DefKind::Fn => {} // entirely within check_item_body
464459
DefKind::Impl { of_trait } => {
465-
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(id.owner_id) {
460+
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(def_id) {
466461
check_impl_items_against_trait(
467462
tcx,
468-
id.owner_id.def_id,
463+
def_id,
469464
impl_trait_ref.instantiate_identity(),
470465
);
471-
check_on_unimplemented(tcx, id);
466+
check_on_unimplemented(tcx, def_id);
472467
}
473468
}
474469
DefKind::Trait => {
475-
let assoc_items = tcx.associated_items(id.owner_id);
476-
check_on_unimplemented(tcx, id);
470+
let assoc_items = tcx.associated_items(def_id);
471+
check_on_unimplemented(tcx, def_id);
477472

478473
for &assoc_item in assoc_items.in_definition_order() {
479474
match assoc_item.kind {
@@ -482,43 +477,43 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
482477
forbid_intrinsic_abi(tcx, assoc_item.ident(tcx).span, abi);
483478
}
484479
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
485-
let trait_args = GenericArgs::identity_for_item(tcx, id.owner_id);
480+
let trait_args = GenericArgs::identity_for_item(tcx, def_id);
486481
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
487482
tcx,
488483
assoc_item,
489484
assoc_item,
490-
ty::TraitRef::new(tcx, id.owner_id.to_def_id(), trait_args),
485+
ty::TraitRef::new(tcx, def_id.to_def_id(), trait_args),
491486
);
492487
}
493488
_ => {}
494489
}
495490
}
496491
}
497492
DefKind::Struct => {
498-
check_struct(tcx, id.owner_id.def_id);
493+
check_struct(tcx, def_id);
499494
}
500495
DefKind::Union => {
501-
check_union(tcx, id.owner_id.def_id);
496+
check_union(tcx, def_id);
502497
}
503498
DefKind::OpaqueTy => {
504-
let origin = tcx.opaque_type_origin(id.owner_id.def_id);
499+
let origin = tcx.opaque_type_origin(def_id);
505500
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id)
506501
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin
507502
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
508503
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
509504
{
510505
// Skip opaques from RPIT in traits with no default body.
511506
} else {
512-
check_opaque(tcx, id);
507+
check_opaque(tcx, def_id);
513508
}
514509
}
515510
DefKind::TyAlias => {
516-
let pty_ty = tcx.type_of(id.owner_id).instantiate_identity();
517-
let generics = tcx.generics_of(id.owner_id);
511+
let pty_ty = tcx.type_of(def_id).instantiate_identity();
512+
let generics = tcx.generics_of(def_id);
518513
check_type_params_are_used(tcx, generics, pty_ty);
519514
}
520515
DefKind::ForeignMod => {
521-
let it = tcx.hir().item(id);
516+
let it = tcx.hir().expect_item(def_id);
522517
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
523518
return;
524519
};
@@ -589,19 +584,19 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
589584
}
590585
}
591586
DefKind::GlobalAsm => {
592-
let it = tcx.hir().item(id);
587+
let it = tcx.hir().expect_item(def_id);
593588
let hir::ItemKind::GlobalAsm(asm) = it.kind else {
594589
span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it)
595590
};
596-
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.owner_id.def_id);
591+
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, def_id);
597592
}
598593
_ => {}
599594
}
600595
}
601596

602-
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: hir::ItemId) {
597+
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, def_id: LocalDefId) {
603598
// an error would be reported if this fails.
604-
let _ = OnUnimplementedDirective::of_item(tcx, item.owner_id.to_def_id());
599+
let _ = OnUnimplementedDirective::of_item(tcx, def_id.to_def_id());
605600
}
606601

607602
pub(super) fn check_specialization_validity<'tcx>(
@@ -1312,7 +1307,7 @@ pub(super) fn check_type_params_are_used<'tcx>(
13121307
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
13131308
let module = tcx.hir_module_items(module_def_id);
13141309
for id in module.items() {
1315-
check_item_type(tcx, id);
1310+
check_item_type(tcx, id.owner_id.def_id);
13161311
}
13171312
if module_def_id == LocalModDefId::CRATE_DEF_ID {
13181313
super::entry::check_for_entry_fn(tcx);

0 commit comments

Comments
 (0)