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();

View file

@ -116,12 +116,6 @@ error: malformed `cfi_encoding` attribute input
LL | #[cfi_encoding]
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
error: malformed `type_const` attribute input
--> $DIR/malformed-attrs.rs:143:5
|
LL | #[type_const = 1]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
error: malformed `marker` attribute input
--> $DIR/malformed-attrs.rs:155:1
|
@ -555,6 +549,15 @@ LL | #[non_exhaustive = 1]
| | didn't expect any arguments here
| help: must be of the form: `#[non_exhaustive]`
error[E0565]: malformed `type_const` attribute input
--> $DIR/malformed-attrs.rs:143:5
|
LL | #[type_const = 1]
| ^^^^^^^^^^^^^---^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[type_const]`
error: attribute should be applied to `const fn`
--> $DIR/malformed-attrs.rs:34:1
|

View file

@ -1,9 +1,3 @@
error: malformed `type_const` attribute input
--> $DIR/bad-type_const-syntax.rs:2:5
|
LL | #[type_const()]
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
error[E0658]: the `#[type_const]` attribute is an experimental feature
--> $DIR/bad-type_const-syntax.rs:2:5
|
@ -24,6 +18,15 @@ LL | #[type_const]
= help: add `#![feature(min_generic_const_args)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0565]: malformed `type_const` attribute input
--> $DIR/bad-type_const-syntax.rs:2:5
|
LL | #[type_const()]
| ^^^^^^^^^^^^--^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[type_const]`
error: `#[type_const]` must only be applied to trait associated constants
--> $DIR/bad-type_const-syntax.rs:11:5
|
@ -32,4 +35,5 @@ LL | #[type_const]
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.
Some errors have detailed explanations: E0565, E0658.
For more information about an error, try `rustc --explain E0565`.