Skip to content

Inconsistent handling of #[derive] on invalid targets #78331

Closed

Description

The following code:

fn main() {
	struct Foo {}
	#[derive(Copy)] impl Foo {}
}

produces the following error

error[E0774]: `derive` may only be applied to structs, enums and unions
 --> src/main.rs:3:2
  |
3 |     #[derive(Copy)] impl Foo {}
  |     ^^^^^^^^^^^^^^^

However, applying #[derive] to a let statement just gives an "unused attribute" warning:

fn main() {
    #[derive(Copy)] let _ = 1;
}
warning: unused attribute
 --> src/main.rs:2:5
  |
2 |     #[derive(Copy)] let _ = 1;
  |     ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default

A #[derive] attribute doesn't make any more sense on a statement than it does on an item. I think we should handle these two cases in the same way.

There's been a move towards forbidding attributes on targets that don't make sense: #77015. Unless anyone objects, I think we should consistently forbid #[derive] on all invalid targets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions