From 8bb7fdb236b50eb14a0b39abbb8cfaa4e2dc853f Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Wed, 2 Jul 2025 00:20:47 +0300 Subject: [PATCH] NoArgsAttributeParser: use an assoc const instead --- .../src/attributes/codegen_attrs.rs | 15 +++------------ .../src/attributes/lint_helpers.rs | 10 ++-------- .../src/attributes/loop_match.rs | 10 ++-------- compiler/rustc_attr_parsing/src/attributes/mod.rs | 4 ++-- .../src/attributes/semantics.rs | 5 +---- .../src/attributes/stability.rs | 5 +---- 6 files changed, 11 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index 360c28dafab9..1132402ea008 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -49,10 +49,7 @@ pub(crate) struct ColdParser; impl NoArgsAttributeParser for ColdParser { const PATH: &[Symbol] = &[sym::cold]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::Cold(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold; } pub(crate) struct ExportNameParser; @@ -193,20 +190,14 @@ pub(crate) struct TrackCallerParser; impl NoArgsAttributeParser for TrackCallerParser { const PATH: &[Symbol] = &[sym::track_caller]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::TrackCaller(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller; } pub(crate) struct NoMangleParser; impl NoArgsAttributeParser for NoMangleParser { const PATH: &[Symbol] = &[sym::no_mangle]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::NoMangle(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle; } #[derive(Default)] diff --git a/compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs b/compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs index 0dfcf3cb8990..5437803d781d 100644 --- a/compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs +++ b/compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs @@ -8,18 +8,12 @@ pub(crate) struct AsPtrParser; impl NoArgsAttributeParser for AsPtrParser { const PATH: &[Symbol] = &[sym::rustc_as_ptr]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; - - fn create(span: Span) -> AttributeKind { - AttributeKind::AsPtr(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::AsPtr; } pub(crate) struct PubTransparentParser; impl NoArgsAttributeParser for PubTransparentParser { const PATH: &[Symbol] = &[sym::rustc_pub_transparent]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; - - fn create(span: Span) -> AttributeKind { - AttributeKind::PubTransparent(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::PubTransparent; } diff --git a/compiler/rustc_attr_parsing/src/attributes/loop_match.rs b/compiler/rustc_attr_parsing/src/attributes/loop_match.rs index 1a5368c092f1..80808b90dc66 100644 --- a/compiler/rustc_attr_parsing/src/attributes/loop_match.rs +++ b/compiler/rustc_attr_parsing/src/attributes/loop_match.rs @@ -8,18 +8,12 @@ pub(crate) struct LoopMatchParser; impl NoArgsAttributeParser for LoopMatchParser { const PATH: &[Symbol] = &[sym::loop_match]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::LoopMatch(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::LoopMatch; } pub(crate) struct ConstContinueParser; impl NoArgsAttributeParser for ConstContinueParser { const PATH: &[Symbol] = &[sym::const_continue]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::ConstContinue(span) - } + const CREATE: fn(Span) -> AttributeKind = AttributeKind::ConstContinue; } diff --git a/compiler/rustc_attr_parsing/src/attributes/mod.rs b/compiler/rustc_attr_parsing/src/attributes/mod.rs index abddc75ab8bf..1755d2d74afe 100644 --- a/compiler/rustc_attr_parsing/src/attributes/mod.rs +++ b/compiler/rustc_attr_parsing/src/attributes/mod.rs @@ -238,7 +238,7 @@ pub(crate) trait NoArgsAttributeParser: 'static { const ON_DUPLICATE: OnDuplicate; /// Create the [`AttributeKind`] given attribute's [`Span`]. - fn create(span: Span) -> AttributeKind; + const CREATE: fn(Span) -> AttributeKind; } pub(crate) struct WithoutArgs, S: Stage>(PhantomData<(S, T)>); @@ -259,7 +259,7 @@ impl, S: Stage> SingleAttributeParser for Without if let Err(span) = args.no_args() { cx.expected_no_args(span); } - Some(T::create(cx.attr_span)) + Some(T::CREATE(cx.attr_span)) } } diff --git a/compiler/rustc_attr_parsing/src/attributes/semantics.rs b/compiler/rustc_attr_parsing/src/attributes/semantics.rs index c5e2bf6862fb..74fdff5d2e18 100644 --- a/compiler/rustc_attr_parsing/src/attributes/semantics.rs +++ b/compiler/rustc_attr_parsing/src/attributes/semantics.rs @@ -8,8 +8,5 @@ pub(crate) struct MayDangleParser; impl NoArgsAttributeParser for MayDangleParser { const PATH: &[Symbol] = &[sym::may_dangle]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; - - fn create(span: Span) -> AttributeKind { - AttributeKind::MayDangle(span) - } + const CREATE: fn(span: Span) -> AttributeKind = AttributeKind::MayDangle; } diff --git a/compiler/rustc_attr_parsing/src/attributes/stability.rs b/compiler/rustc_attr_parsing/src/attributes/stability.rs index ffe60f59cc47..6bccd0042a80 100644 --- a/compiler/rustc_attr_parsing/src/attributes/stability.rs +++ b/compiler/rustc_attr_parsing/src/attributes/stability.rs @@ -136,10 +136,7 @@ pub(crate) struct ConstStabilityIndirectParser; impl NoArgsAttributeParser for ConstStabilityIndirectParser { const PATH: &[Symbol] = &[sym::rustc_const_stable_indirect]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Ignore; - - fn create(_: Span) -> AttributeKind { - AttributeKind::ConstStabilityIndirect - } + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ConstStabilityIndirect; } #[derive(Default)]