Skip to content

Commit 77d08ac

Browse files
committed
NoArgsAttributeParser
1 parent 5e749eb commit 77d08ac

File tree

8 files changed

+100
-114
lines changed

8 files changed

+100
-114
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use rustc_feature::{AttributeTemplate, template};
33
use rustc_session::parse::feature_err;
44
use rustc_span::{Span, Symbol, sym};
55

6-
use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser};
6+
use super::{
7+
AcceptMapping, AttributeOrder, AttributeParser, NoArgsAttributeParser, OnDuplicate,
8+
SingleAttributeParser,
9+
};
710
use crate::context::{AcceptContext, FinalizeContext, Stage};
811
use crate::parser::ArgParser;
912
use crate::session_diagnostics::{NakedFunctionIncompatibleAttribute, NullOnExport};
@@ -43,19 +46,12 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
4346

4447
pub(crate) struct ColdParser;
4548

46-
impl<S: Stage> SingleAttributeParser<S> for ColdParser {
49+
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
4750
const PATH: &[Symbol] = &[sym::cold];
48-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
4951
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
50-
const TEMPLATE: AttributeTemplate = template!(Word);
51-
52-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
53-
if let Err(span) = args.no_args() {
54-
cx.expected_no_args(span);
55-
return None;
56-
}
5752

58-
Some(AttributeKind::Cold(cx.attr_span))
53+
fn create(span: Span) -> AttributeKind {
54+
AttributeKind::Cold(span)
5955
}
6056
}
6157

@@ -194,38 +190,22 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
194190
}
195191

196192
pub(crate) struct TrackCallerParser;
197-
198-
impl<S: Stage> SingleAttributeParser<S> for TrackCallerParser {
193+
impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
199194
const PATH: &[Symbol] = &[sym::track_caller];
200-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
201195
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
202-
const TEMPLATE: AttributeTemplate = template!(Word);
203196

204-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
205-
if let Err(span) = args.no_args() {
206-
cx.expected_no_args(span);
207-
return None;
208-
}
209-
210-
Some(AttributeKind::TrackCaller(cx.attr_span))
197+
fn create(span: Span) -> AttributeKind {
198+
AttributeKind::TrackCaller(span)
211199
}
212200
}
213201

214202
pub(crate) struct NoMangleParser;
215-
216-
impl<S: Stage> SingleAttributeParser<S> for NoMangleParser {
217-
const PATH: &[rustc_span::Symbol] = &[sym::no_mangle];
218-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
203+
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
204+
const PATH: &[Symbol] = &[sym::no_mangle];
219205
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
220-
const TEMPLATE: AttributeTemplate = template!(Word);
221-
222-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
223-
if let Err(span) = args.no_args() {
224-
cx.expected_no_args(span);
225-
return None;
226-
}
227206

228-
Some(AttributeKind::NoMangle(cx.attr_span))
207+
fn create(span: Span) -> AttributeKind {
208+
AttributeKind::NoMangle(span)
229209
}
230210
}
231211

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,25 @@
11
use rustc_attr_data_structures::AttributeKind;
2-
use rustc_feature::{AttributeTemplate, template};
3-
use rustc_span::{Symbol, sym};
2+
use rustc_span::{Span, Symbol, sym};
43

5-
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6-
use crate::context::{AcceptContext, Stage};
7-
use crate::parser::ArgParser;
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
86

97
pub(crate) struct AsPtrParser;
10-
11-
impl<S: Stage> SingleAttributeParser<S> for AsPtrParser {
8+
impl<S: Stage> NoArgsAttributeParser<S> for AsPtrParser {
129
const PATH: &[Symbol] = &[sym::rustc_as_ptr];
13-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
1410
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
15-
const TEMPLATE: AttributeTemplate = template!(Word);
1611

17-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18-
if let Err(span) = args.no_args() {
19-
cx.expected_no_args(span);
20-
}
21-
Some(AttributeKind::AsPtr(cx.attr_span))
12+
fn create(span: Span) -> AttributeKind {
13+
AttributeKind::AsPtr(span)
2214
}
2315
}
2416

2517
pub(crate) struct PubTransparentParser;
26-
impl<S: Stage> SingleAttributeParser<S> for PubTransparentParser {
18+
impl<S: Stage> NoArgsAttributeParser<S> for PubTransparentParser {
2719
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
28-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
2920
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
30-
const TEMPLATE: AttributeTemplate = template!(Word);
3121

32-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
33-
if let Err(span) = args.no_args() {
34-
cx.expected_no_args(span);
35-
}
36-
Some(AttributeKind::PubTransparent(cx.attr_span))
22+
fn create(span: Span) -> AttributeKind {
23+
AttributeKind::PubTransparent(span)
3724
}
3825
}
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
use rustc_attr_data_structures::AttributeKind;
2-
use rustc_feature::{AttributeTemplate, template};
3-
use rustc_span::{Symbol, sym};
2+
use rustc_span::{Span, Symbol, sym};
43

5-
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6-
use crate::context::{AcceptContext, Stage};
7-
use crate::parser::ArgParser;
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
86

97
pub(crate) struct LoopMatchParser;
10-
impl<S: Stage> SingleAttributeParser<S> for LoopMatchParser {
8+
impl<S: Stage> NoArgsAttributeParser<S> for LoopMatchParser {
119
const PATH: &[Symbol] = &[sym::loop_match];
12-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
1310
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
14-
const TEMPLATE: AttributeTemplate = template!(Word);
1511

16-
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
17-
Some(AttributeKind::LoopMatch(cx.attr_span))
12+
fn create(span: Span) -> AttributeKind {
13+
AttributeKind::LoopMatch(span)
1814
}
1915
}
2016

2117
pub(crate) struct ConstContinueParser;
22-
impl<S: Stage> SingleAttributeParser<S> for ConstContinueParser {
18+
impl<S: Stage> NoArgsAttributeParser<S> for ConstContinueParser {
2319
const PATH: &[Symbol] = &[sym::const_continue];
24-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
2520
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
26-
const TEMPLATE: AttributeTemplate = template!(Word);
2721

28-
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
29-
Some(AttributeKind::ConstContinue(cx.attr_span))
22+
fn create(span: Span) -> AttributeKind {
23+
AttributeKind::ConstContinue(span)
3024
}
3125
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use std::marker::PhantomData;
1818

1919
use rustc_attr_data_structures::AttributeKind;
20-
use rustc_feature::AttributeTemplate;
20+
use rustc_feature::{AttributeTemplate, template};
2121
use rustc_span::{Span, Symbol};
2222
use thin_vec::ThinVec;
2323

@@ -228,6 +228,41 @@ pub(crate) enum AttributeOrder {
228228
KeepLast,
229229
}
230230

231+
/// An even simpler version of [`SingleAttributeParser`]:
232+
/// now automatically check that there are no arguments provided to the attribute.
233+
///
234+
/// [`WithoutArgs<T> where T: NoArgsAttributeParser`](WithoutArgs) implements [`SingleAttributeParser`].
235+
//
236+
pub(crate) trait NoArgsAttributeParser<S: Stage>: 'static {
237+
const PATH: &[Symbol];
238+
const ON_DUPLICATE: OnDuplicate<S>;
239+
240+
/// Create the [`AttributeKind`] given attribute's [`Span`].
241+
fn create(span: Span) -> AttributeKind;
242+
}
243+
244+
pub(crate) struct WithoutArgs<T: NoArgsAttributeParser<S>, S: Stage>(PhantomData<(S, T)>);
245+
246+
impl<T: NoArgsAttributeParser<S>, S: Stage> Default for WithoutArgs<T, S> {
247+
fn default() -> Self {
248+
Self(Default::default())
249+
}
250+
}
251+
252+
impl<T: NoArgsAttributeParser<S>, S: Stage> SingleAttributeParser<S> for WithoutArgs<T, S> {
253+
const PATH: &[Symbol] = T::PATH;
254+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
255+
const ON_DUPLICATE: OnDuplicate<S> = T::ON_DUPLICATE;
256+
const TEMPLATE: AttributeTemplate = template!(Word);
257+
258+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
259+
if let Err(span) = args.no_args() {
260+
cx.expected_no_args(span);
261+
}
262+
Some(T::create(cx.attr_span))
263+
}
264+
}
265+
231266
type ConvertFn<E> = fn(ThinVec<E>) -> AttributeKind;
232267

233268
/// Alternative to [`AttributeParser`] that automatically handles state management.
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
use rustc_attr_data_structures::AttributeKind;
2-
use rustc_feature::{AttributeTemplate, template};
3-
use rustc_span::{Symbol, sym};
2+
use rustc_span::{Span, Symbol, sym};
43

5-
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6-
use crate::context::{AcceptContext, Stage};
7-
use crate::parser::ArgParser;
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
86

97
pub(crate) struct MayDangleParser;
10-
impl<S: Stage> SingleAttributeParser<S> for MayDangleParser {
8+
impl<S: Stage> NoArgsAttributeParser<S> for MayDangleParser {
119
const PATH: &[Symbol] = &[sym::may_dangle];
12-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
1310
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
14-
const TEMPLATE: AttributeTemplate = template!(Word);
1511

16-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
17-
if let Err(span) = args.no_args() {
18-
cx.expected_no_args(span);
19-
}
20-
Some(AttributeKind::MayDangle(cx.attr_span))
12+
fn create(span: Span) -> AttributeKind {
13+
AttributeKind::MayDangle(span)
2114
}
2215
}

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use rustc_attr_data_structures::{
55
StableSince, UnstableReason, VERSION_PLACEHOLDER,
66
};
77
use rustc_errors::ErrorGuaranteed;
8-
use rustc_feature::{AttributeTemplate, template};
8+
use rustc_feature::template;
99
use rustc_span::{Ident, Span, Symbol, sym};
1010

1111
use super::util::parse_version;
12-
use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser};
12+
use super::{AcceptMapping, AttributeParser, OnDuplicate};
13+
use crate::attributes::NoArgsAttributeParser;
1314
use crate::context::{AcceptContext, FinalizeContext, Stage};
1415
use crate::parser::{ArgParser, MetaItemParser};
1516
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
@@ -132,18 +133,12 @@ impl<S: Stage> AttributeParser<S> for BodyStabilityParser {
132133
}
133134

134135
pub(crate) struct ConstStabilityIndirectParser;
135-
// FIXME(jdonszelmann): single word attribute group when we have these
136-
impl<S: Stage> SingleAttributeParser<S> for ConstStabilityIndirectParser {
136+
impl<S: Stage> NoArgsAttributeParser<S> for ConstStabilityIndirectParser {
137137
const PATH: &[Symbol] = &[sym::rustc_const_stable_indirect];
138-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
139138
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
140-
const TEMPLATE: AttributeTemplate = template!(Word);
141139

142-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
143-
if let Err(span) = args.no_args() {
144-
cx.expected_no_args(span);
145-
}
146-
Some(AttributeKind::ConstStabilityIndirect)
140+
fn create(_: Span) -> AttributeKind {
141+
AttributeKind::ConstStabilityIndirect
147142
}
148143
}
149144

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::attributes::stability::{
3333
};
3434
use crate::attributes::traits::SkipDuringMethodDispatchParser;
3535
use crate::attributes::transparency::TransparencyParser;
36-
use crate::attributes::{AttributeParser as _, Combine, Single};
36+
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
3737
use crate::parser::{ArgParser, MetaItemParser, PathParser};
3838
use crate::session_diagnostics::{AttributeParseError, AttributeParseErrorReason, UnknownMetaItem};
3939

@@ -54,13 +54,15 @@ macro_rules! attribute_parsers {
5454
use super::*;
5555
type Combine<T> = super::Combine<T, Early>;
5656
type Single<T> = super::Single<T, Early>;
57+
type WithoutArgs<T> = super::WithoutArgs<T, Early>;
5758

5859
attribute_parsers!(@[Early] pub(crate) static $name = [$($names),*];);
5960
}
6061
mod late {
6162
use super::*;
6263
type Combine<T> = super::Combine<T, Late>;
6364
type Single<T> = super::Single<T, Late>;
65+
type WithoutArgs<T> = super::WithoutArgs<T, Late>;
6466

6567
attribute_parsers!(@[Late] pub(crate) static $name = [$($names),*];);
6668
}
@@ -115,24 +117,24 @@ attribute_parsers!(
115117
// tidy-alphabetical-end
116118

117119
// tidy-alphabetical-start
118-
Single<AsPtrParser>,
119-
Single<ColdParser>,
120-
Single<ConstContinueParser>,
121-
Single<ConstStabilityIndirectParser>,
122120
Single<DeprecationParser>,
123121
Single<ExportNameParser>,
124122
Single<InlineParser>,
125123
Single<LinkNameParser>,
126-
Single<LoopMatchParser>,
127-
Single<MayDangleParser>,
128124
Single<MustUseParser>,
129-
Single<NoMangleParser>,
130125
Single<OptimizeParser>,
131-
Single<PubTransparentParser>,
132126
Single<RustcForceInlineParser>,
133127
Single<SkipDuringMethodDispatchParser>,
134-
Single<TrackCallerParser>,
135128
Single<TransparencyParser>,
129+
Single<WithoutArgs<AsPtrParser>>,
130+
Single<WithoutArgs<ColdParser>>,
131+
Single<WithoutArgs<ConstContinueParser>>,
132+
Single<WithoutArgs<ConstStabilityIndirectParser>>,
133+
Single<WithoutArgs<LoopMatchParser>>,
134+
Single<WithoutArgs<MayDangleParser>>,
135+
Single<WithoutArgs<NoMangleParser>>,
136+
Single<WithoutArgs<PubTransparentParser>>,
137+
Single<WithoutArgs<TrackCallerParser>>,
136138
// tidy-alphabetical-end
137139
];
138140
);

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,6 @@ LL | #![link_section = "1800"]
395395
|
396396
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
397397

398-
warning: attribute should be applied to a function definition
399-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:62:1
400-
|
401-
LL | #![cold]
402-
| ^^^^^^^^ cannot be applied to crates
403-
|
404-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
405-
406398
warning: attribute should be applied to a foreign function or static
407399
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:66:1
408400
|
@@ -417,6 +409,14 @@ warning: `#[must_use]` has no effect when applied to a module
417409
LL | #![must_use]
418410
| ^^^^^^^^^^^^
419411

412+
warning: attribute should be applied to a function definition
413+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:62:1
414+
|
415+
LL | #![cold]
416+
| ^^^^^^^^ cannot be applied to crates
417+
|
418+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
419+
420420
warning: `#[macro_use]` only has an effect on `extern crate` and modules
421421
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:176:5
422422
|

0 commit comments

Comments
 (0)