Skip to content

A path towards erroring on nonsense attributes #142838

Open
@mejrs

Description

@mejrs

Description

There are quite a few attributes that have been accidentally allowed in positions that make no sense. For example:

#[windows_subsystem = "windows"]
pub struct Bar {
    #[no_link]
    pub field: u32,
}
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
 --> src/lib.rs:1:1
  |
1 | #[windows_subsystem = "windows"]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default

warning: `#[no_link]` is ignored on struct fields, match arms and macro defs
 --> src/lib.rs:3:5
  |
3 |     #[no_link]
  |     ^^^^^^^^^^

In check_attr.rs there are various comments along the lines of "we accidentally allowed this". Some attributes are even not checked at all. In almost all cases, the attribute does nothing and is simply ignored.

Proposal

Once the attribute rework is completed we can consistently and reliably handle these cases. I propose that once that is done, we start on a path to turning these into errors.

I'd like to hear the lang team's thoughts on this. In particular,

  • For the cases where the attribute does nothing and there is no behavioral change to consider, that we turn them into a hard error over a future edition.
  • That we start emitting "this will be an error in a future release" diagnostics for these cases.

#142836 is adjacent and related to this issue, but different; that one asks for a decision about behavior in ambiguous situations, this one is about getting rid of obvious nonsense.

@rustbot labels +I-lang-nominated

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-lang-nominatedNominated for discussion during a lang team meeting.P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions