Skip to content

Commit cbcbd88

Browse files
committed
NoArgsAttributeParser
1 parent aa80a2b commit cbcbd88

File tree

9 files changed

+88
-87
lines changed

9 files changed

+88
-87
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 10 additions & 21 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, 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;
@@ -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: &[rustc_span::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

@@ -168,18 +164,11 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
168164

169165
pub(crate) struct NoMangleParser;
170166

171-
impl<S: Stage> SingleAttributeParser<S> for NoMangleParser {
167+
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
172168
const PATH: &[rustc_span::Symbol] = &[sym::no_mangle];
173-
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
174169
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
175-
const TEMPLATE: AttributeTemplate = template!(Word);
176-
177-
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
178-
if let Err(span) = args.no_args() {
179-
cx.expected_no_args(span);
180-
return None;
181-
}
182170

183-
Some(AttributeKind::NoMangle(cx.attr_span))
171+
fn create(span: Span) -> AttributeKind {
172+
AttributeKind::NoMangle(span)
184173
}
185174
}
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
}

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

@@ -226,6 +226,41 @@ pub(crate) enum AttributeOrder {
226226
KeepLast,
227227
}
228228

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

231266
/// 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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::attributes::stability::{
2828
};
2929
use crate::attributes::traits::SkipDuringMethodDispatchParser;
3030
use crate::attributes::transparency::TransparencyParser;
31-
use crate::attributes::{AttributeParser as _, Combine, Single};
31+
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
3232
use crate::parser::{ArgParser, MetaItemParser, PathParser};
3333
use crate::session_diagnostics::{AttributeParseError, AttributeParseErrorReason, UnknownMetaItem};
3434

@@ -49,13 +49,15 @@ macro_rules! attribute_parsers {
4949
use super::*;
5050
type Combine<T> = super::Combine<T, Early>;
5151
type Single<T> = super::Single<T, Early>;
52+
type WithoutArgs<T> = super::WithoutArgs<T, Early>;
5253

5354
attribute_parsers!(@[Early] pub(crate) static $name = [$($names),*];);
5455
}
5556
mod late {
5657
use super::*;
5758
type Combine<T> = super::Combine<T, Late>;
5859
type Single<T> = super::Single<T, Late>;
60+
type WithoutArgs<T> = super::WithoutArgs<T, Late>;
5961

6062
attribute_parsers!(@[Late] pub(crate) static $name = [$($names),*];);
6163
}
@@ -109,19 +111,19 @@ attribute_parsers!(
109111
// tidy-alphabetical-end
110112

111113
// tidy-alphabetical-start
112-
Single<AsPtrParser>,
113-
Single<ColdParser>,
114-
Single<ConstStabilityIndirectParser>,
115114
Single<DeprecationParser>,
116115
Single<InlineParser>,
117-
Single<MayDangleParser>,
118116
Single<MustUseParser>,
119-
Single<NoMangleParser>,
120117
Single<OptimizeParser>,
121-
Single<PubTransparentParser>,
122118
Single<RustcForceInlineParser>,
123119
Single<SkipDuringMethodDispatchParser>,
124120
Single<TransparencyParser>,
121+
Single<WithoutArgs<AsPtrParser>>,
122+
Single<WithoutArgs<ColdParser>>,
123+
Single<WithoutArgs<ConstStabilityIndirectParser>>,
124+
Single<WithoutArgs<MayDangleParser>>,
125+
Single<WithoutArgs<NoMangleParser>>,
126+
Single<WithoutArgs<PubTransparentParser>>,
125127
// tidy-alphabetical-end
126128
];
127129
);

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

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

406+
warning: `#[must_use]` has no effect when applied to a module
407+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:72:1
408+
|
409+
LL | #![must_use]
410+
| ^^^^^^^^^^^^
411+
406412
warning: attribute should be applied to a function definition
407413
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:62:1
408414
|
@@ -411,12 +417,6 @@ LL | #![cold]
411417
|
412418
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
413419

414-
warning: `#[must_use]` has no effect when applied to a module
415-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:72:1
416-
|
417-
LL | #![must_use]
418-
| ^^^^^^^^^^^^
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
|

tests/ui/lint/unused/unused-attr-duplicate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl X {}
7373
#[inline(always)]
7474
#[inline(never)] //~ ERROR unused attribute
7575
//~^ WARN this was previously accepted
76-
#[cold]
7776
#[cold] //~ ERROR unused attribute
77+
#[cold]
7878
#[track_caller]
7979
#[track_caller] //~ ERROR unused attribute
8080
pub fn xyz() {}
@@ -94,8 +94,8 @@ extern "C" {
9494
#[export_name = "exported_symbol_name2"]
9595
pub fn export_test() {}
9696

97-
#[no_mangle]
9897
#[no_mangle] //~ ERROR unused attribute
98+
#[no_mangle]
9999
pub fn no_mangle_test() {}
100100

101101
#[used]

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,25 @@ LL | #[inline(always)]
266266
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
267267

268268
error: unused attribute
269-
--> $DIR/unused-attr-duplicate.rs:77:1
269+
--> $DIR/unused-attr-duplicate.rs:76:1
270270
|
271271
LL | #[cold]
272272
| ^^^^^^^ help: remove this attribute
273273
|
274274
note: attribute also specified here
275-
--> $DIR/unused-attr-duplicate.rs:76:1
275+
--> $DIR/unused-attr-duplicate.rs:77:1
276276
|
277277
LL | #[cold]
278278
| ^^^^^^^
279279

280280
error: unused attribute
281-
--> $DIR/unused-attr-duplicate.rs:98:1
281+
--> $DIR/unused-attr-duplicate.rs:97:1
282282
|
283283
LL | #[no_mangle]
284284
| ^^^^^^^^^^^^ help: remove this attribute
285285
|
286286
note: attribute also specified here
287-
--> $DIR/unused-attr-duplicate.rs:97:1
287+
--> $DIR/unused-attr-duplicate.rs:98:1
288288
|
289289
LL | #[no_mangle]
290290
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)