Skip to content

Start moving wf checking away from HIR #142030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,15 @@ pub enum TraitItemKind<'hir> {
/// type.
Type(GenericBounds<'hir>, Option<&'hir Ty<'hir>>),
}
impl TraitItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
TraitItemKind::Const(..) => "associated constant",
TraitItemKind::Fn(..) => "function",
TraitItemKind::Type(..) => "associated type",
}
}
}

// The bodies for items are stored "out of line", in a separate
// hashmap in the `Crate`. Here we just record the hir-id of the item
Expand Down Expand Up @@ -3202,6 +3211,15 @@ pub enum ImplItemKind<'hir> {
/// An associated type.
Type(&'hir Ty<'hir>),
}
impl ImplItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
ImplItemKind::Const(..) => "associated constant",
ImplItemKind::Fn(..) => "function",
ImplItemKind::Type(..) => "associated type",
}
}
}

/// A constraint on an associated item.
///
Expand Down Expand Up @@ -4527,6 +4545,16 @@ pub enum ForeignItemKind<'hir> {
Type,
}

impl ForeignItemKind<'_> {
pub fn descr(&self) -> &'static str {
match self {
ForeignItemKind::Fn(..) => "function",
ForeignItemKind::Static(..) => "static variable",
ForeignItemKind::Type => "type",
}
}
}

/// A variable captured by a closure.
#[derive(Debug, Copy, Clone, HashStable_Generic)]
pub struct Upvar {
Expand Down Expand Up @@ -4834,6 +4862,10 @@ impl<'hir> Node<'hir> {
ImplItemKind::Type(ty) => Some(ty),
_ => None,
},
Node::ForeignItem(it) => match it.kind {
ForeignItemKind::Static(ty, ..) => Some(ty),
_ => None,
},
_ => None,
}
}
Expand Down
186 changes: 165 additions & 21 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_errors::{EmissionGuarantee, MultiSpan};
use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::{LangItem, Node, intravisit};
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
use rustc_infer::traits::{Obligation, ObligationCauseCode};
use rustc_infer::traits::{Obligation, ObligationCauseCode, WellFormedLoc};
use rustc_lint_defs::builtin::{
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, UNSUPPORTED_CALLING_CONVENTIONS,
};
Expand All @@ -36,6 +36,10 @@ 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_associated_item, check_trait_item, check_variances_for_type_defn, check_where_clauses,
enter_wf_checking_ctxt,
};

fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
if let ExternAbi::Cdecl { unwind } = abi {
Expand Down Expand Up @@ -729,7 +733,8 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
}

pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
let mut res = Ok(());
let generics = tcx.generics_of(def_id);

for param in &generics.own_params {
Expand All @@ -754,15 +759,35 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}

match tcx.def_kind(def_id) {
DefKind::Static { .. } => {
check_static_inhabited(tcx, def_id);
check_static_linkage(tcx, def_id);
def_kind @ (DefKind::Static { .. } | DefKind::Const) => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
match def_kind {
DefKind::Static { .. } => {
check_static_inhabited(tcx, def_id);
check_static_linkage(tcx, def_id);
res = res.and(wfcheck::check_static_item(tcx, def_id));
return res;
Copy link
Member

@compiler-errors compiler-errors Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of these returns should have a comment documenting why it's fine to not hir WF check them.

}
DefKind::Const => {}
_ => unreachable!(),
}
}
DefKind::Const => {}
DefKind::Enum => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
crate::collect::lower_enum_variant_types(tcx, def_id.to_def_id());
check_enum(tcx, def_id);
check_variances_for_type_defn(tcx, def_id);
}
DefKind::Fn => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
tcx.ensure_ok().fn_sig(def_id);
tcx.ensure_ok().codegen_fn_attrs(def_id);
if let Some(i) = tcx.intrinsic(def_id) {
intrinsic::check_intrinsic_type(
tcx,
Expand All @@ -773,17 +798,31 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
}
DefKind::Impl { of_trait } => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().impl_trait_header(def_id);
tcx.ensure_ok().predicates_of(def_id);
tcx.ensure_ok().associated_items(def_id);
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
if tcx
.ensure_ok()
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)
.is_ok()
{
res = res.and(
tcx.ensure_ok()
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
);

if res.is_ok() {
// Checking this only makes sense if the all trait impls satisfy basic
// requirements (see `coherent_trait` query), otherwise
// we run into infinite recursions a lot.
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
}
}
}
DefKind::Trait => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().trait_def(def_id);
tcx.ensure_ok().explicit_super_predicates_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
tcx.ensure_ok().associated_items(def_id);
let assoc_items = tcx.associated_items(def_id);
check_on_unimplemented(tcx, def_id);

Expand All @@ -802,11 +841,33 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
}
}
DefKind::Struct => {
check_struct(tcx, def_id);
DefKind::TraitAlias => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().explicit_implied_predicates_of(def_id);
tcx.ensure_ok().explicit_super_predicates_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
}
DefKind::Union => {
check_union(tcx, def_id);
def_kind @ (DefKind::Struct | DefKind::Union) => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);

let adt = tcx.adt_def(def_id).non_enum_variant();
for f in adt.fields.iter() {
tcx.ensure_ok().generics_of(f.did);
tcx.ensure_ok().type_of(f.did);
tcx.ensure_ok().predicates_of(f.did);
}

if let Some((_, ctor_def_id)) = adt.ctor {
crate::collect::lower_variant_ctor(tcx, ctor_def_id.expect_local());
}
match def_kind {
DefKind::Struct => check_struct(tcx, def_id),
DefKind::Union => check_union(tcx, def_id),
_ => unreachable!(),
}
check_variances_for_type_defn(tcx, def_id);
}
DefKind::OpaqueTy => {
check_opaque_precise_captures(tcx, def_id);
Expand All @@ -831,14 +892,33 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
tcx.ensure_ok().const_conditions(def_id);
}
return res;
}
DefKind::TyAlias => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
check_type_alias_type_params_are_used(tcx, def_id);
if tcx.type_alias_is_lazy(def_id) {
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
let ty = tcx.type_of(def_id).instantiate_identity();
let span = tcx.def_span(def_id);
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
wfcx.register_wf_obligation(
span,
Some(WellFormedLoc::Ty(def_id)),
item_ty.into(),
);
check_where_clauses(wfcx, def_id);
Ok(())
}));
check_variances_for_type_defn(tcx, def_id);
}
}
DefKind::ForeignMod => {
let it = tcx.hir_expect_item(def_id);
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
return;
return Ok(());
};

check_abi(tcx, it.hir_id(), it.span, abi);
Expand Down Expand Up @@ -877,15 +957,23 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}

let item = tcx.hir_foreign_item(item.id);
match &item.kind {
hir::ForeignItemKind::Fn(sig, _, _) => {
tcx.ensure_ok().generics_of(item.owner_id);
tcx.ensure_ok().type_of(item.owner_id);
tcx.ensure_ok().predicates_of(item.owner_id);
if tcx.is_conditionally_const(def_id) {
tcx.ensure_ok().explicit_implied_const_bounds(def_id);
tcx.ensure_ok().const_conditions(def_id);
}
match item.kind {
hir::ForeignItemKind::Fn(sig, ..) => {
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
tcx.ensure_ok().fn_sig(item.owner_id);
require_c_abi_if_c_variadic(tcx, sig.decl, abi, item.span);
}
hir::ForeignItemKind::Static(..) => {
check_static_inhabited(tcx, def_id);
check_static_linkage(tcx, def_id);
tcx.ensure_ok().codegen_fn_attrs(item.owner_id);
}
_ => {}
_ => (),
}
}
}
Expand All @@ -897,9 +985,65 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
// We do not call `type_of` for closures here as that
// depends on typecheck and would therefore hide
// any further errors in case one typeck fails.
return res;
}
DefKind::AssocFn => {
tcx.ensure_ok().codegen_fn_attrs(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().fn_sig(def_id);
tcx.ensure_ok().predicates_of(def_id);
res = res.and(check_associated_item(tcx, def_id));
let assoc_item = tcx.associated_item(def_id);
match assoc_item.container {
ty::AssocItemContainer::Impl => {}
ty::AssocItemContainer::Trait => {
res = res.and(check_trait_item(tcx, def_id));
}
}
return res;
}
DefKind::AssocConst => {
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
res = res.and(check_associated_item(tcx, def_id));
let assoc_item = tcx.associated_item(def_id);
match assoc_item.container {
ty::AssocItemContainer::Impl => {}
ty::AssocItemContainer::Trait => {
res = res.and(check_trait_item(tcx, def_id));
}
}
return res;
}
DefKind::AssocTy => {
tcx.ensure_ok().predicates_of(def_id);
res = res.and(check_associated_item(tcx, def_id));

let assoc_item = tcx.associated_item(def_id);
let has_type = match assoc_item.container {
ty::AssocItemContainer::Impl => true,
ty::AssocItemContainer::Trait => {
tcx.ensure_ok().item_bounds(def_id);
tcx.ensure_ok().item_self_bounds(def_id);
res = res.and(check_trait_item(tcx, def_id));
assoc_item.defaultness(tcx).has_value()
}
};
if has_type {
tcx.ensure_ok().type_of(def_id);
}
return res;
}
DefKind::AnonConst | DefKind::InlineConst => return res,
_ => {}
}
let node = tcx.hir_node_by_def_id(def_id);
res.and(match node {
hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
hir::Node::Item(item) => wfcheck::check_item(tcx, item),
hir::Node::ForeignItem(item) => wfcheck::check_foreign_item(tcx, item),
_ => unreachable!("{node:?}"),
})
}

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