Consolidate type const checks on tcx.is_type_const

This commit is contained in:
Alan Egerton 2026-02-04 19:26:13 +00:00
parent 8bccf1224d
commit 13141af949
No known key found for this signature in database
GPG key ID: 3D7EA7527916B438
8 changed files with 21 additions and 30 deletions

View file

@ -923,7 +923,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
);
check_where_clauses(wfcx, def_id);
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
if tcx.is_type_const(def_id.into()) {
wfcheck::check_type_const(wfcx, def_id, ty, true)?;
}
Ok(())

View file

@ -6,10 +6,9 @@ use hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::codes::*;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, pluralize, struct_span_code_err};
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::VisitorExt;
use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, find_attr, intravisit};
use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisit};
use rustc_infer::infer::{self, BoundRegionConversionTime, InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::util;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@ -2051,12 +2050,8 @@ fn compare_type_const<'tcx>(
impl_const_item: ty::AssocItem,
trait_const_item: ty::AssocItem,
) -> Result<(), ErrorGuaranteed> {
let impl_is_type_const =
find_attr!(tcx.get_all_attrs(impl_const_item.def_id), AttributeKind::TypeConst(_));
let trait_type_const_span = find_attr!(
tcx.get_all_attrs(trait_const_item.def_id),
AttributeKind::TypeConst(sp) => *sp
);
let impl_is_type_const = tcx.is_type_const(impl_const_item.def_id);
let trait_type_const_span = tcx.type_const_span(trait_const_item.def_id);
if let Some(trait_type_const_span) = trait_type_const_span
&& !impl_is_type_const

View file

@ -953,7 +953,7 @@ pub(crate) fn check_associated_item(
wfcx.register_wf_obligation(span, loc, ty.into());
let has_value = item.defaultness(tcx).has_value();
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
if tcx.is_type_const(def_id.into()) {
check_type_const(wfcx, def_id, ty, has_value)?;
}

View file

@ -4,10 +4,9 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::codes::*;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::PolyTraitRef;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
use rustc_hir::{PolyTraitRef, find_attr};
use rustc_middle::bug;
use rustc_middle::ty::{
self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@ -603,10 +602,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
});
if let ty::AssocTag::Const = assoc_tag
&& !find_attr!(
self.tcx().get_all_attrs(assoc_item.def_id),
AttributeKind::TypeConst(_)
)
&& !self.tcx().is_type_const(assoc_item.def_id)
{
if tcx.features().min_generic_const_args() {
let mut err = self.dcx().struct_span_err(

View file

@ -28,10 +28,9 @@ use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
};
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId, find_attr};
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::DynCompatibilityViolation;
use rustc_macros::{TypeFoldable, TypeVisitable};
@ -1423,7 +1422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
LowerTypeRelativePathMode::Const,
)? {
TypeRelativePath::AssocItem(def_id, args) => {
if !find_attr!(self.tcx().get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
if !self.tcx().is_type_const(def_id) {
let mut err = self.dcx().struct_span_err(
span,
"use of trait associated const without `#[type_const]`",
@ -1896,7 +1895,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
ty::AssocTag::Const,
) {
Ok((item_def_id, item_args)) => {
if !find_attr!(self.tcx().get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) {
if !self.tcx().is_type_const(item_def_id) {
let mut err = self.dcx().struct_span_err(
span,
"use of `const` in the type system without `#[type_const]`",

View file

@ -85,12 +85,9 @@ mod variance;
pub use errors::NoVariantNamed;
use rustc_abi::{CVariadicStatus, ExternAbi};
use rustc_hir::attrs::AttributeKind;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{
find_attr, {self as hir},
};
use rustc_middle::mir::interpret::GlobalId;
use rustc_middle::query::Providers;
use rustc_middle::ty::{Const, Ty, TyCtxt};
@ -238,7 +235,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
}
DefKind::Const
if !tcx.generics_of(item_def_id).own_requires_monomorphization()
&& !find_attr!(tcx.get_all_attrs(item_def_id), AttributeKind::TypeConst(_)) =>
&& !tcx.is_type_const(item_def_id.into()) =>
{
// FIXME(generic_const_items): Passing empty instead of identity args is fishy but
// seems to be fine for now. Revisit this!

View file

@ -1886,10 +1886,15 @@ impl<'tcx> TyCtxt<'tcx> {
self.is_lang_item(self.parent(def_id), LangItem::AsyncDropInPlace)
}
pub fn type_const_span(self, def_id: DefId) -> Option<Span> {
matches!(self.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
.then(|| find_attr!(self.get_all_attrs(def_id), AttributeKind::TypeConst(sp) => *sp))
.flatten()
}
/// Check if the given `def_id` is a const with the `#[type_const]` attribute.
pub fn is_type_const(self, def_id: DefId) -> bool {
matches!(self.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
&& find_attr!(self.get_all_attrs(def_id), AttributeKind::TypeConst(_))
self.type_const_span(def_id).is_some()
}
/// Returns the movability of the coroutine of `def_id`, or panics

View file

@ -7,10 +7,9 @@
use std::ops::ControlFlow;
use rustc_errors::FatalError;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, LangItem, find_attr};
use rustc_hir::{self as hir, LangItem};
use rustc_middle::query::Providers;
use rustc_middle::ty::{
self, EarlyBinder, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
@ -333,7 +332,7 @@ pub fn dyn_compatibility_violations_for_assoc_item(
if tcx.features().min_generic_const_args() {
if !tcx.generics_of(item.def_id).is_own_empty() {
errors.push(AssocConstViolation::Generic);
} else if !find_attr!(tcx.get_all_attrs(item.def_id), AttributeKind::TypeConst(_)) {
} else if !tcx.is_type_const(item.def_id) {
errors.push(AssocConstViolation::NonType);
}