Port #[type_const] to the new attribute system

This commit is contained in:
Pavel Grigorenko 2025-06-24 00:51:16 +03:00
parent 6f8e92d5aa
commit 813ec60744
9 changed files with 42 additions and 21 deletions

View file

@ -359,6 +359,9 @@ pub enum AttributeKind {
/// Represents `#[track_caller]`
TrackCaller(Span),
/// Represents `#[type_const]`.
TypeConst(Span),
/// Represents `#[used]`
Used { used_by: UsedBy, span: Span },
// tidy-alphabetical-end

View file

@ -59,6 +59,7 @@ impl AttributeKind {
StdInternalSymbol(..) => No,
TargetFeature(..) => No,
TrackCaller(..) => Yes,
TypeConst(..) => Yes,
Used { .. } => No,
// tidy-alphabetical-end
}

View file

@ -82,3 +82,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for CoinductiveParser {
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Coinductive;
}
pub(crate) struct TypeConstParser;
impl<S: Stage> NoArgsAttributeParser<S> for TypeConstParser {
const PATH: &[Symbol] = &[sym::type_const];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TypeConst;
}

View file

@ -45,7 +45,7 @@ use crate::attributes::stability::{
use crate::attributes::test_attrs::IgnoreParser;
use crate::attributes::traits::{
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
SkipDuringMethodDispatchParser,
SkipDuringMethodDispatchParser, TypeConstParser,
};
use crate::attributes::transparency::TransparencyParser;
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@ -169,6 +169,7 @@ attribute_parsers!(
Single<WithoutArgs<PubTransparentParser>>,
Single<WithoutArgs<StdInternalSymbolParser>>,
Single<WithoutArgs<TrackCallerParser>>,
Single<WithoutArgs<TypeConstParser>>,
// tidy-alphabetical-end
];
);

View file

@ -1,9 +1,10 @@
use rustc_attr_data_structures::{AttributeKind, find_attr};
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace};
use rustc_hir::def_id::DefId;
use rustc_macros::{Decodable, Encodable, HashStable};
use rustc_span::{Ident, Symbol, sym};
use rustc_span::{Ident, Symbol};
use super::{TyCtxt, Visibility};
use crate::ty;
@ -160,7 +161,7 @@ impl AssocItem {
// Inherent impl but this attr is only applied to trait assoc items.
(AssocItemContainer::Impl, None) => return true,
};
tcx.has_attr(def_id, sym::type_const)
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
}
}

View file

@ -287,6 +287,7 @@ pub fn check_builtin_meta_item(
| sym::rustc_do_not_implement_via_object
| sym::rustc_coinductive
| sym::const_trait
| sym::type_const
| sym::repr
| sym::align
| sym::deprecated

View file

@ -129,6 +129,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
) => {
self.check_must_be_applied_to_trait(*attr_span, span, target);
}
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
self.check_type_const(hir_id, attr_span, target)
}
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
self.check_confusables(*first_span, target);
}
@ -338,9 +341,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::coroutine, ..] => {
self.check_coroutine(attr, target);
}
[sym::type_const, ..] => {
self.check_type_const(hir_id,attr, target);
}
[sym::linkage, ..] => self.check_linkage(attr, span, target),
[
// ok
@ -2513,7 +2513,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
fn check_type_const(&self, hir_id: HirId, attr: &Attribute, target: Target) {
fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
let tcx = self.tcx;
if target == Target::AssocConst
&& let parent = tcx.parent(hir_id.expect_owner().to_def_id())
@ -2523,7 +2523,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
} else {
self.dcx()
.struct_span_err(
attr.span(),
attr_span,
"`#[type_const]` must only be applied to trait associated constants",
)
.emit();