Check variances in the non-hir wfchecker

This commit is contained in:
Oli Scherer
2025-06-04 11:16:11 +00:00
parent d27c05709c
commit ee8fa4eb16
12 changed files with 105 additions and 78 deletions

View File

@@ -36,6 +36,7 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
use super::compare_impl_item::check_type_bounds;
use super::*;
use crate::check::wfcheck::check_variances_for_type_defn;
fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
if let ExternAbi::Cdecl { unwind } = abi {
@@ -762,6 +763,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
DefKind::Const => {}
DefKind::Enum => {
check_enum(tcx, def_id);
check_variances_for_type_defn(tcx, def_id);
}
DefKind::Fn => {
if let Some(i) = tcx.intrinsic(def_id) {
@@ -802,9 +804,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
}
DefKind::Struct => {
check_struct(tcx, def_id);
check_variances_for_type_defn(tcx, def_id);
}
DefKind::Union => {
check_union(tcx, def_id);
check_variances_for_type_defn(tcx, def_id);
}
DefKind::OpaqueTy => {
check_opaque_precise_captures(tcx, def_id);
@@ -832,6 +836,9 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
}
DefKind::TyAlias => {
check_type_alias_type_params_are_used(tcx, def_id);
if tcx.type_alias_is_lazy(def_id) {
check_variances_for_type_defn(tcx, def_id);
}
}
DefKind::ForeignMod => {
let it = tcx.hir_expect_item(def_id);

View File

@@ -292,25 +292,13 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
}
hir::ItemKind::Fn { ident, sig, .. } => check_item_fn(tcx, def_id, ident, sig.decl),
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span, item.span),
hir::ItemKind::Struct(..) => {
let res = check_type_defn(tcx, item, false);
check_variances_for_type_defn(tcx, def_id);
res
}
hir::ItemKind::Union(..) => {
let res = check_type_defn(tcx, item, true);
check_variances_for_type_defn(tcx, def_id);
res
}
hir::ItemKind::Enum(..) => {
let res = check_type_defn(tcx, item, true);
check_variances_for_type_defn(tcx, def_id);
res
}
hir::ItemKind::Struct(..) => check_type_defn(tcx, item, false),
hir::ItemKind::Union(..) => check_type_defn(tcx, item, true),
hir::ItemKind::Enum(..) => check_type_defn(tcx, item, true),
hir::ItemKind::Trait(..) => check_trait(tcx, item),
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
hir::ItemKind::TyAlias(.., hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
let res = enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
let ty = tcx.type_of(def_id).instantiate_identity();
let item_ty =
wfcx.deeply_normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
@@ -321,9 +309,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
);
check_where_clauses(wfcx, item.span, def_id);
Ok(())
});
check_variances_for_type_defn(tcx, def_id);
res
})
}
_ => Ok(()),
}
@@ -1977,7 +1963,7 @@ fn legacy_receiver_is_implemented<'tcx>(
}
}
fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
pub(super) fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
match tcx.def_kind(def_id) {
DefKind::Enum | DefKind::Struct | DefKind::Union => {
// Ok