Skip to content

Commit fa1eb67

Browse files
Port #[ignore] to the new attribute parsing infrastructure
1 parent f191420 commit fa1eb67

File tree

11 files changed

+107
-36
lines changed

11 files changed

+107
-36
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ pub enum AttributeKind {
250250
span: Span,
251251
},
252252

253+
/// Represents `#[ignore]`
254+
Ignore {
255+
span: Span,
256+
/// ignore can optionally have a reason: `#[ignore = "reason this is ignored"]`
257+
reason: Option<Symbol>,
258+
},
259+
253260
/// Represents `#[inline]` and `#[rustc_force_inline]`.
254261
Inline(InlineAttr, Span),
255262

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl AttributeKind {
2323
Deprecation { .. } => Yes,
2424
DocComment { .. } => Yes,
2525
ExportName { .. } => Yes,
26+
Ignore { .. } => No,
2627
Inline(..) => No,
2728
MacroTransparency(..) => Yes,
2829
Repr(..) => No,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) mod must_use;
3838
pub(crate) mod repr;
3939
pub(crate) mod semantics;
4040
pub(crate) mod stability;
41+
pub(crate) mod test_attrs;
4142
pub(crate) mod traits;
4243
pub(crate) mod transparency;
4344
pub(crate) mod util;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_attr_data_structures::lints::AttributeLintKind;
3+
use rustc_feature::{AttributeTemplate, template};
4+
use rustc_span::{Symbol, sym};
5+
6+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7+
use crate::context::{AcceptContext, Stage};
8+
use crate::parser::ArgParser;
9+
10+
pub(crate) struct IgnoreParser;
11+
12+
impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
13+
const PATH: &[Symbol] = &[sym::ignore];
14+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
16+
const TEMPLATE: AttributeTemplate = template!(Word, NameValueStr: "reason");
17+
18+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
19+
Some(AttributeKind::Ignore {
20+
span: cx.attr_span,
21+
reason: match args {
22+
ArgParser::NoArgs => None,
23+
ArgParser::NameValue(name_value) => name_value.value_as_str(),
24+
ArgParser::List(_) => {
25+
let suggestions =
26+
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
27+
let span = cx.attr_span;
28+
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
29+
return None;
30+
}
31+
},
32+
})
33+
}
34+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::attributes::semantics::MayDangleParser;
3131
use crate::attributes::stability::{
3232
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
3333
};
34+
use crate::attributes::test_attrs::IgnoreParser;
3435
use crate::attributes::traits::SkipDuringMethodDispatchParser;
3536
use crate::attributes::transparency::TransparencyParser;
3637
use crate::attributes::{AttributeParser as _, Combine, Single};
@@ -121,6 +122,7 @@ attribute_parsers!(
121122
Single<ConstStabilityIndirectParser>,
122123
Single<DeprecationParser>,
123124
Single<ExportNameParser>,
125+
Single<IgnoreParser>,
124126
Single<InlineParser>,
125127
Single<LinkNameParser>,
126128
Single<LoopMatchParser>,

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,7 @@ impl AttributeExt for Attribute {
13031303
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
13041304
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13051305
Attribute::Parsed(AttributeKind::MayDangle(span)) => *span,
1306+
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => *span,
13061307
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13071308
}
13081309
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ fn emit_malformed_attribute(
303303
| sym::must_use
304304
| sym::track_caller
305305
| sym::link_name
306+
| sym::ignore
306307
) {
307308
return;
308309
}
309310

310311
// Some of previously accepted forms were used in practice,
311312
// report them as warnings for now.
312-
let should_warn =
313-
|name| matches!(name, sym::doc | sym::ignore | sym::link | sym::test | sym::bench);
313+
let should_warn = |name| matches!(name, sym::doc | sym::link | sym::test | sym::bench);
314314

315315
let error_msg = format!("malformed `{name}` attribute input");
316316
let mut suggestions = vec![];

compiler/rustc_passes/src/check_attr.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
197197
Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
198198
self.check_may_dangle(hir_id, *attr_span)
199199
}
200+
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => {
201+
self.check_generic_attr(hir_id, sym::ignore, *span, target, Target::Fn)
202+
}
200203
Attribute::Parsed(AttributeKind::MustUse { span, .. }) => {
201204
self.check_must_use(hir_id, *span, target)
202205
}
@@ -290,16 +293,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
290293
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
291294
self.check_macro_use(hir_id, attr, target)
292295
}
293-
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
296+
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
294297
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
295-
[sym::ignore, ..] | [sym::should_panic, ..] => {
296-
self.check_generic_attr(hir_id, attr, target, Target::Fn)
298+
[sym::should_panic, ..] => {
299+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
297300
}
298301
[sym::automatically_derived, ..] => {
299-
self.check_generic_attr(hir_id, attr, target, Target::Impl)
302+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
300303
}
301304
[sym::no_implicit_prelude, ..] => {
302-
self.check_generic_attr(hir_id, attr, target, Target::Mod)
305+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod)
303306
}
304307
[sym::rustc_object_lifetime_default, ..] => self.check_object_lifetime_default(hir_id),
305308
[sym::proc_macro, ..] => {
@@ -309,7 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
309312
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
310313
}
311314
[sym::proc_macro_derive, ..] => {
312-
self.check_generic_attr(hir_id, attr, target, Target::Fn);
315+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
313316
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
314317
}
315318
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
@@ -618,7 +621,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
618621
}
619622
}
620623

621-
fn check_generic_attr(
624+
/// FIXME: Remove when all attributes are ported to the new parser
625+
fn check_generic_attr_unparsed(
622626
&self,
623627
hir_id: HirId,
624628
attr: &Attribute,
@@ -641,6 +645,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
641645
}
642646
}
643647

648+
fn check_generic_attr(
649+
&self,
650+
hir_id: HirId,
651+
attr_name: Symbol,
652+
attr_span: Span,
653+
target: Target,
654+
allowed_target: Target,
655+
) {
656+
if target != allowed_target {
657+
self.tcx.emit_node_span_lint(
658+
UNUSED_ATTRIBUTES,
659+
hir_id,
660+
attr_span,
661+
errors::OnlyHasEffectOn {
662+
attr_name: attr_name.to_string(),
663+
target_name: allowed_target.name().replace(' ', "_"),
664+
},
665+
);
666+
}
667+
}
668+
644669
/// Checks if `#[naked]` is applied to a function definition.
645670
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
646671
match target {

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
@@ -367,12 +367,6 @@ warning: `#[should_panic]` only has an effect on functions
367367
LL | #![should_panic]
368368
| ^^^^^^^^^^^^^^^^
369369

370-
warning: `#[ignore]` only has an effect on functions
371-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:54:1
372-
|
373-
LL | #![ignore]
374-
| ^^^^^^^^^^
375-
376370
warning: `#[proc_macro_derive]` only has an effect on functions
377371
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:60:1
378372
|
@@ -403,6 +397,12 @@ LL | #![cold]
403397
|
404398
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
405399

400+
warning: `#[ignore]` only has an effect on functions
401+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:54:1
402+
|
403+
LL | #![ignore]
404+
| ^^^^^^^^^^
405+
406406
warning: attribute should be applied to a foreign function or static
407407
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:66:1
408408
|

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4141
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4242

43-
error: unused attribute
44-
--> $DIR/unused-attr-duplicate.rs:53:1
45-
|
46-
LL | #[ignore = "some text"]
47-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
48-
|
49-
note: attribute also specified here
50-
--> $DIR/unused-attr-duplicate.rs:52:1
51-
|
52-
LL | #[ignore]
53-
| ^^^^^^^^^
54-
5543
error: unused attribute
5644
--> $DIR/unused-attr-duplicate.rs:55:1
5745
|
@@ -189,6 +177,18 @@ note: attribute also specified here
189177
LL | #[macro_export]
190178
| ^^^^^^^^^^^^^^^
191179

180+
error: unused attribute
181+
--> $DIR/unused-attr-duplicate.rs:53:1
182+
|
183+
LL | #[ignore = "some text"]
184+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
185+
|
186+
note: attribute also specified here
187+
--> $DIR/unused-attr-duplicate.rs:52:1
188+
|
189+
LL | #[ignore]
190+
| ^^^^^^^^^
191+
192192
error: unused attribute
193193
--> $DIR/unused-attr-duplicate.rs:60:1
194194
|

tests/ui/malformed/malformed-regressions.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ LL | #[doc]
88
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
99
= note: `#[deny(ill_formed_attribute_input)]` on by default
1010

11-
error: valid forms for the attribute are `#[ignore]` and `#[ignore = "reason"]`
12-
--> $DIR/malformed-regressions.rs:3:1
13-
|
14-
LL | #[ignore()]
15-
| ^^^^^^^^^^^
16-
|
17-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
18-
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
19-
2011
error: attribute must be of the form `#[link(name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...", /*opt*/ import_name_type = "decorated|noprefix|undecorated")]`
2112
--> $DIR/malformed-regressions.rs:7:1
2213
|
@@ -35,6 +26,15 @@ LL | #[link = ""]
3526
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3627
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
3728

29+
error: valid forms for the attribute are `#[ignore = "reason"]` and `#[ignore]`
30+
--> $DIR/malformed-regressions.rs:3:1
31+
|
32+
LL | #[ignore()]
33+
| ^^^^^^^^^^^
34+
|
35+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
36+
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
37+
3838
error: valid forms for the attribute are `#[inline(always|never)]` and `#[inline]`
3939
--> $DIR/malformed-regressions.rs:5:1
4040
|

0 commit comments

Comments
 (0)