Skip to content

Commit 422eea2

Browse files
committed
Don't run hir wfcheck if ty wfcheck handled everything
1 parent 65aac24 commit 422eea2

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,12 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
767767
DefKind::Static { .. } => {
768768
check_static_inhabited(tcx, def_id);
769769
check_static_linkage(tcx, def_id);
770-
wfcheck::check_static_item(tcx, def_id)?;
770+
res = res.and(wfcheck::check_static_item(tcx, def_id));
771+
772+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
773+
// checks. Returning early here does not miss any checks and
774+
// avoids this query from having a direct dependency edge on the HIR
775+
return res;
771776
}
772777
DefKind::Const => {}
773778
_ => unreachable!(),
@@ -803,10 +808,17 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
803808
tcx.ensure_ok().predicates_of(def_id);
804809
tcx.ensure_ok().associated_items(def_id);
805810
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
806-
tcx.ensure_ok()
807-
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)?;
811+
res = res.and(
812+
tcx.ensure_ok()
813+
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
814+
);
808815

809-
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
816+
if res.is_ok() {
817+
// Checking this only makes sense if the all trait impls satisfy basic
818+
// requirements (see `coherent_trait` query), otherwise
819+
// we run into infinite recursions a lot.
820+
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
821+
}
810822
}
811823
}
812824
DefKind::Trait => {
@@ -884,6 +896,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
884896
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
885897
tcx.ensure_ok().const_conditions(def_id);
886898
}
899+
900+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
901+
// checks. Returning early here does not miss any checks and
902+
// avoids this query from having a direct dependency edge on the HIR
903+
return res;
887904
}
888905
DefKind::TyAlias => {
889906
tcx.ensure_ok().generics_of(def_id);
@@ -976,6 +993,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
976993
// We do not call `type_of` for closures here as that
977994
// depends on typecheck and would therefore hide
978995
// any further errors in case one typeck fails.
996+
997+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
998+
// checks. Returning early here does not miss any checks and
999+
// avoids this query from having a direct dependency edge on the HIR
1000+
return res;
9791001
}
9801002
DefKind::AssocFn => {
9811003
tcx.ensure_ok().codegen_fn_attrs(def_id);
@@ -990,6 +1012,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
9901012
res = res.and(check_trait_item(tcx, def_id));
9911013
}
9921014
}
1015+
1016+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
1017+
// checks. Returning early here does not miss any checks and
1018+
// avoids this query from having a direct dependency edge on the HIR
1019+
return res;
9931020
}
9941021
DefKind::AssocConst => {
9951022
tcx.ensure_ok().type_of(def_id);
@@ -1002,6 +1029,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10021029
res = res.and(check_trait_item(tcx, def_id));
10031030
}
10041031
}
1032+
1033+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
1034+
// checks. Returning early here does not miss any checks and
1035+
// avoids this query from having a direct dependency edge on the HIR
1036+
return res;
10051037
}
10061038
DefKind::AssocTy => {
10071039
tcx.ensure_ok().predicates_of(def_id);
@@ -1020,10 +1052,26 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10201052
if has_type {
10211053
tcx.ensure_ok().type_of(def_id);
10221054
}
1055+
1056+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
1057+
// checks. Returning early here does not miss any checks and
1058+
// avoids this query from having a direct dependency edge on the HIR
1059+
return res;
10231060
}
1061+
1062+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
1063+
// checks. Returning early here does not miss any checks and
1064+
// avoids this query from having a direct dependency edge on the HIR
1065+
DefKind::AnonConst | DefKind::InlineConst => return res,
10241066
_ => {}
10251067
}
1026-
res
1068+
let node = tcx.hir_node_by_def_id(def_id);
1069+
res.and(match node {
1070+
hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
1071+
hir::Node::Item(item) => wfcheck::check_item(tcx, item),
1072+
hir::Node::ForeignItem(item) => wfcheck::check_foreign_item(tcx, item),
1073+
_ => unreachable!("{node:?}"),
1074+
})
10271075
}
10281076

10291077
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, def_id: LocalDefId) {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,6 @@ where
191191

192192
fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
193193
let mut res = crate::check::check::check_item_type(tcx, def_id);
194-
let node = tcx.hir_node_by_def_id(def_id);
195-
res = res.and(match node {
196-
hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
197-
hir::Node::Item(item) => check_item(tcx, item),
198-
hir::Node::TraitItem(..) => Ok(()),
199-
hir::Node::ImplItem(..) => Ok(()),
200-
hir::Node::ForeignItem(item) => check_foreign_item(tcx, item),
201-
hir::Node::ConstBlock(_) | hir::Node::Expr(_) | hir::Node::OpaqueTy(_) => Ok(()),
202-
_ => unreachable!("{node:?}"),
203-
});
204194

205195
for param in &tcx.generics_of(def_id).own_params {
206196
res = res.and(check_param_wf(tcx, param));
@@ -223,7 +213,10 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
223213
/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
224214
/// the types first.
225215
#[instrument(skip(tcx), level = "debug")]
226-
fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<(), ErrorGuaranteed> {
216+
pub(super) fn check_item<'tcx>(
217+
tcx: TyCtxt<'tcx>,
218+
item: &'tcx hir::Item<'tcx>,
219+
) -> Result<(), ErrorGuaranteed> {
227220
let def_id = item.owner_id.def_id;
228221

229222
debug!(
@@ -305,7 +298,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
305298
}
306299
}
307300

308-
fn check_foreign_item<'tcx>(
301+
pub(super) fn check_foreign_item<'tcx>(
309302
tcx: TyCtxt<'tcx>,
310303
item: &'tcx hir::ForeignItem<'tcx>,
311304
) -> Result<(), ErrorGuaranteed> {

0 commit comments

Comments
 (0)