Skip to content

Commit d587a01

Browse files
committed
Don't run hir wfcheck if ty wfcheck handled everything
1 parent b3ec77f commit d587a01

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ 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+
return res;
771772
}
772773
DefKind::Const => {}
773774
_ => unreachable!(),
@@ -803,10 +804,17 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
803804
tcx.ensure_ok().predicates_of(def_id);
804805
tcx.ensure_ok().associated_items(def_id);
805806
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)?;
807+
res = res.and(
808+
tcx.ensure_ok()
809+
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
810+
);
808811

809-
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
812+
if res.is_ok() {
813+
// Checking this only makes sense if the all trait impls satisfy basic
814+
// requirements (see `coherent_trait` query), otherwise
815+
// we run into infinite recursions a lot.
816+
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
817+
}
810818
}
811819
}
812820
DefKind::Trait => {
@@ -884,6 +892,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
884892
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
885893
tcx.ensure_ok().const_conditions(def_id);
886894
}
895+
return res;
887896
}
888897
DefKind::TyAlias => {
889898
tcx.ensure_ok().generics_of(def_id);
@@ -976,6 +985,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
976985
// We do not call `type_of` for closures here as that
977986
// depends on typecheck and would therefore hide
978987
// any further errors in case one typeck fails.
988+
return res;
979989
}
980990
DefKind::AssocFn => {
981991
tcx.ensure_ok().codegen_fn_attrs(def_id);
@@ -990,6 +1000,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
9901000
res = res.and(check_trait_item(tcx, def_id));
9911001
}
9921002
}
1003+
return res;
9931004
}
9941005
DefKind::AssocConst => {
9951006
tcx.ensure_ok().type_of(def_id);
@@ -1002,6 +1013,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10021013
res = res.and(check_trait_item(tcx, def_id));
10031014
}
10041015
}
1016+
return res;
10051017
}
10061018
DefKind::AssocTy => {
10071019
tcx.ensure_ok().predicates_of(def_id);
@@ -1020,10 +1032,18 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10201032
if has_type {
10211033
tcx.ensure_ok().type_of(def_id);
10221034
}
1035+
return res;
10231036
}
1037+
DefKind::AnonConst | DefKind::InlineConst => return res,
10241038
_ => {}
10251039
}
1026-
res
1040+
let node = tcx.hir_node_by_def_id(def_id);
1041+
res.and(match node {
1042+
hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
1043+
hir::Node::Item(item) => wfcheck::check_item(tcx, item),
1044+
hir::Node::ForeignItem(item) => wfcheck::check_foreign_item(tcx, item),
1045+
_ => unreachable!("{node:?}"),
1046+
})
10271047
}
10281048

10291049
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)