Skip to content

Commit facecf6

Browse files
committed
Fetch less HIR in signature check.
1 parent e9e1226 commit facecf6

File tree

11 files changed

+97
-507
lines changed

11 files changed

+97
-507
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+37-62
Original file line numberDiff line numberDiff line change
@@ -530,46 +530,33 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
530530
}
531531
DefKind::Fn => {} // entirely within check_item_body
532532
DefKind::Impl { of_trait } => {
533-
if of_trait {
534-
let it = tcx.hir().item(id);
535-
let hir::ItemKind::Impl(impl_) = it.kind else { return };
536-
debug!("ItemKind::Impl {} with id {:?}", it.ident, it.owner_id);
537-
if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.owner_id) {
538-
check_impl_items_against_trait(
539-
tcx,
540-
it.span,
541-
it.owner_id.def_id,
542-
impl_trait_ref.subst_identity(),
543-
&impl_.items,
544-
);
545-
check_on_unimplemented(tcx, it);
546-
}
533+
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(id.owner_id) {
534+
check_impl_items_against_trait(
535+
tcx,
536+
id.owner_id.def_id,
537+
impl_trait_ref.subst_identity(),
538+
);
539+
check_on_unimplemented(tcx, id);
547540
}
548541
}
549542
DefKind::Trait => {
550-
let it = tcx.hir().item(id);
551-
let hir::ItemKind::Trait(_, _, _, _, items) = it.kind else {
552-
return;
553-
};
554-
check_on_unimplemented(tcx, it);
555-
556-
for item in items.iter() {
557-
let item = tcx.hir().trait_item(item.id);
558-
match &item.kind {
559-
hir::TraitItemKind::Fn(sig, _) => {
560-
let abi = sig.header.abi;
561-
fn_maybe_err(tcx, item.ident.span, abi);
543+
let assoc_items = tcx.associated_items(id.owner_id);
544+
check_on_unimplemented(tcx, id);
545+
546+
for assoc_item in assoc_items.in_definition_order() {
547+
match assoc_item.kind {
548+
ty::AssocKind::Fn => {
549+
let abi = tcx.fn_sig(assoc_item.def_id).skip_binder().abi();
550+
fn_maybe_err(tcx, assoc_item.ident(tcx).span, abi);
562551
}
563-
hir::TraitItemKind::Type(.., Some(default)) => {
564-
let assoc_item = tcx.associated_item(item.owner_id);
552+
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
565553
let trait_substs =
566-
InternalSubsts::identity_for_item(tcx, it.owner_id.to_def_id());
554+
InternalSubsts::identity_for_item(tcx, id.owner_id.to_def_id());
567555
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
568556
tcx,
569557
assoc_item,
570558
assoc_item,
571-
default.span,
572-
tcx.mk_trait_ref(it.owner_id.to_def_id(), trait_substs),
559+
tcx.mk_trait_ref(id.owner_id.to_def_id(), trait_substs),
573560
);
574561
}
575562
_ => {}
@@ -681,7 +668,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
681668
}
682669
}
683670

684-
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
671+
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: hir::ItemId) {
685672
// an error would be reported if this fails.
686673
let _ = OnUnimplementedDirective::of_item(tcx, item.owner_id.to_def_id());
687674
}
@@ -691,7 +678,7 @@ pub(super) fn check_specialization_validity<'tcx>(
691678
trait_def: &ty::TraitDef,
692679
trait_item: &ty::AssocItem,
693680
impl_id: DefId,
694-
impl_item: &hir::ImplItemRef,
681+
impl_item: DefId,
695682
) {
696683
let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
697684
let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
@@ -737,10 +724,8 @@ pub(super) fn check_specialization_validity<'tcx>(
737724

738725
fn check_impl_items_against_trait<'tcx>(
739726
tcx: TyCtxt<'tcx>,
740-
full_impl_span: Span,
741727
impl_id: LocalDefId,
742728
impl_trait_ref: ty::TraitRef<'tcx>,
743-
impl_item_refs: &[hir::ImplItemRef],
744729
) {
745730
// If the trait reference itself is erroneous (so the compilation is going
746731
// to fail), skip checking the items here -- the `impl_item` table in `tcx`
@@ -749,12 +734,14 @@ fn check_impl_items_against_trait<'tcx>(
749734
return;
750735
}
751736

737+
let impl_item_refs = tcx.associated_item_def_ids(impl_id);
738+
752739
// Negative impls are not expected to have any items
753740
match tcx.impl_polarity(impl_id) {
754741
ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
755742
ty::ImplPolarity::Negative => {
756743
if let [first_item_ref, ..] = impl_item_refs {
757-
let first_item_span = tcx.hir().impl_item(first_item_ref.id).span;
744+
let first_item_span = tcx.def_span(first_item_ref);
758745
struct_span_err!(
759746
tcx.sess,
760747
first_item_span,
@@ -769,43 +756,27 @@ fn check_impl_items_against_trait<'tcx>(
769756

770757
let trait_def = tcx.trait_def(impl_trait_ref.def_id);
771758

772-
for impl_item in impl_item_refs {
773-
let ty_impl_item = tcx.associated_item(impl_item.id.owner_id);
759+
for &impl_item in impl_item_refs {
760+
let ty_impl_item = tcx.associated_item(impl_item);
774761
let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id {
775762
tcx.associated_item(trait_item_id)
776763
} else {
777764
// Checked in `associated_item`.
778-
tcx.sess.delay_span_bug(impl_item.span, "missing associated item in trait");
765+
tcx.sess.delay_span_bug(tcx.def_span(impl_item), "missing associated item in trait");
779766
continue;
780767
};
781-
let impl_item_full = tcx.hir().impl_item(impl_item.id);
782-
match impl_item_full.kind {
783-
hir::ImplItemKind::Const(..) => {
768+
match ty_impl_item.kind {
769+
ty::AssocKind::Const => {
784770
let _ = tcx.compare_impl_const((
785-
impl_item.id.owner_id.def_id,
771+
impl_item.expect_local(),
786772
ty_impl_item.trait_item_def_id.unwrap(),
787773
));
788774
}
789-
hir::ImplItemKind::Fn(..) => {
790-
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
791-
compare_impl_method(
792-
tcx,
793-
&ty_impl_item,
794-
&ty_trait_item,
795-
impl_trait_ref,
796-
opt_trait_span,
797-
);
775+
ty::AssocKind::Fn => {
776+
compare_impl_method(tcx, &ty_impl_item, &ty_trait_item, impl_trait_ref);
798777
}
799-
hir::ImplItemKind::Type(impl_ty) => {
800-
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
801-
compare_impl_ty(
802-
tcx,
803-
&ty_impl_item,
804-
impl_ty.span,
805-
&ty_trait_item,
806-
impl_trait_ref,
807-
opt_trait_span,
808-
);
778+
ty::AssocKind::Type => {
779+
compare_impl_ty(tcx, &ty_impl_item, &ty_trait_item, impl_trait_ref);
809780
}
810781
}
811782

@@ -840,6 +811,8 @@ fn check_impl_items_against_trait<'tcx>(
840811
.map_or(false, |node_item| !node_item.defining_node.is_from_trait());
841812

842813
if !is_implemented_here {
814+
let full_impl_span =
815+
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(impl_id));
843816
match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
844817
EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
845818
tcx,
@@ -866,6 +839,8 @@ fn check_impl_items_against_trait<'tcx>(
866839
}
867840

868841
if !missing_items.is_empty() {
842+
let full_impl_span =
843+
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(impl_id));
869844
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
870845
}
871846

0 commit comments

Comments
 (0)