Skip to content

Port #[ignore] to the new attribute parsing infrastructure #143238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ pub enum AttributeKind {
span: Span,
},

/// Represents `#[ignore]`
Ignore {
span: Span,
/// ignore can optionally have a reason: `#[ignore = "reason this is ignored"]`
reason: Option<Symbol>,
},

/// Represents `#[inline]` and `#[rustc_force_inline]`.
Inline(InlineAttr, Span),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl AttributeKind {
Deprecation { .. } => Yes,
DocComment { .. } => Yes,
ExportName { .. } => Yes,
Ignore { .. } => No,
Inline(..) => No,
MacroTransparency(..) => Yes,
Repr(..) => No,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) mod must_use;
pub(crate) mod repr;
pub(crate) mod semantics;
pub(crate) mod stability;
pub(crate) mod test_attrs;
pub(crate) mod traits;
pub(crate) mod transparency;
pub(crate) mod util;
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/test_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use rustc_attr_data_structures::AttributeKind;
use rustc_attr_data_structures::lints::AttributeLintKind;
use rustc_feature::{AttributeTemplate, template};
use rustc_span::{Symbol, sym};

use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
use crate::context::{AcceptContext, Stage};
use crate::parser::ArgParser;

pub(crate) struct IgnoreParser;

impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
const PATH: &[Symbol] = &[sym::ignore];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
const TEMPLATE: AttributeTemplate = template!(Word, NameValueStr: "reason");

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
Some(AttributeKind::Ignore {
span: cx.attr_span,
reason: match args {
ArgParser::NoArgs => None,
ArgParser::NameValue(name_value) => name_value.value_as_str(),
ArgParser::List(_) => {
let suggestions =
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
let span = cx.attr_span;
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
return None;
}
},
})
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::attributes::semantics::MayDangleParser;
use crate::attributes::stability::{
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
};
use crate::attributes::test_attrs::IgnoreParser;
use crate::attributes::traits::SkipDuringMethodDispatchParser;
use crate::attributes::transparency::TransparencyParser;
use crate::attributes::{AttributeParser as _, Combine, Single};
Expand Down Expand Up @@ -121,6 +122,7 @@ attribute_parsers!(
Single<ConstStabilityIndirectParser>,
Single<DeprecationParser>,
Single<ExportNameParser>,
Single<IgnoreParser>,
Single<InlineParser>,
Single<LinkNameParser>,
Single<LoopMatchParser>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ impl AttributeExt for Attribute {
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
Attribute::Parsed(AttributeKind::MayDangle(span)) => *span,
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => *span,
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ fn emit_malformed_attribute(
| sym::must_use
| sym::track_caller
| sym::link_name
| sym::ignore
) {
return;
}

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

let error_msg = format!("malformed `{name}` attribute input");
let mut suggestions = vec![];
Expand Down
39 changes: 32 additions & 7 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
self.check_may_dangle(hir_id, *attr_span)
}
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => {
self.check_generic_attr(hir_id, sym::ignore, *span, target, Target::Fn)
}
Attribute::Parsed(AttributeKind::MustUse { span, .. }) => {
self.check_must_use(hir_id, *span, target)
}
Expand Down Expand Up @@ -290,16 +293,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
self.check_macro_use(hir_id, attr, target)
}
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
[sym::ignore, ..] | [sym::should_panic, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Fn)
[sym::should_panic, ..] => {
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
}
[sym::automatically_derived, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Impl)
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
}
[sym::no_implicit_prelude, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Mod)
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod)
}
[sym::rustc_object_lifetime_default, ..] => self.check_object_lifetime_default(hir_id),
[sym::proc_macro, ..] => {
Expand All @@ -309,7 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
}
[sym::proc_macro_derive, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Fn);
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
}
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
Expand Down Expand Up @@ -618,7 +621,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

fn check_generic_attr(
/// FIXME: Remove when all attributes are ported to the new parser
fn check_generic_attr_unparsed(
&self,
hir_id: HirId,
attr: &Attribute,
Expand All @@ -641,6 +645,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

fn check_generic_attr(
&self,
hir_id: HirId,
attr_name: Symbol,
attr_span: Span,
target: Target,
allowed_target: Target,
) {
if target != allowed_target {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr_span,
errors::OnlyHasEffectOn {
attr_name: attr_name.to_string(),
target_name: allowed_target.name().replace(' ', "_"),
},
);
}
}

/// Checks if `#[naked]` is applied to a function definition.
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
match target {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,6 @@ warning: `#[should_panic]` only has an effect on functions
LL | #![should_panic]
| ^^^^^^^^^^^^^^^^

warning: `#[ignore]` only has an effect on functions
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:54:1
|
LL | #![ignore]
| ^^^^^^^^^^

warning: `#[proc_macro_derive]` only has an effect on functions
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:60:1
|
Expand Down Expand Up @@ -403,6 +397,12 @@ LL | #![cold]
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[ignore]` only has an effect on functions
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:54:1
|
LL | #![ignore]
| ^^^^^^^^^^

warning: attribute should be applied to a foreign function or static
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:66:1
|
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/lint/unused/unused-attr-duplicate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ LL | #[path = "auxiliary/lint_unused_extern_crate.rs"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:53:1
|
LL | #[ignore = "some text"]
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:52:1
|
LL | #[ignore]
| ^^^^^^^^^

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:55:1
|
Expand Down Expand Up @@ -189,6 +177,18 @@ note: attribute also specified here
LL | #[macro_export]
| ^^^^^^^^^^^^^^^

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:53:1
|
LL | #[ignore = "some text"]
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:52:1
|
LL | #[ignore]
| ^^^^^^^^^

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:60:1
|
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/malformed/malformed-regressions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ LL | #[doc]
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` on by default

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

error: attribute must be of the form `#[link(name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...", /*opt*/ import_name_type = "decorated|noprefix|undecorated")]`
--> $DIR/malformed-regressions.rs:7:1
|
Expand All @@ -35,6 +26,15 @@ LL | #[link = ""]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

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

error: valid forms for the attribute are `#[inline(always|never)]` and `#[inline]`
--> $DIR/malformed-regressions.rs:5:1
|
Expand Down
Loading