Skip to content

Attributes are silently ignored on .. in struct pattern #81282

Open

Description

The following code compiles successfully:

struct Foo {
    val: bool
}

fn main() {
    let data = Foo { val: true };
    match data {
        Foo { #[missing_attr] #[cfg(FALSE)] .. } => {}
    }
}

However, placing attributes on a normal field pattern:

struct Foo {
    val: bool
}

fn main() {
    let data = Foo { val: true };
    match data {
        Foo { #[missing_attr] #[cfg(FALSE)] val } => {}
    }
}

produces the following error:

error[E0027]: pattern does not mention field `val`
 --> src/main.rs:8:9
  |
8 |         Foo { #[missing_attr] #[cfg(FALSE)] val } => {}
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `val`
  |
help: include the missing field in the pattern
  |
8 |         Foo { val } => {}
  |             ^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
  |
8 |         Foo { .. } => {}
  |             ^^^^^^

error: aborting due to previous error

We should either handle attributes consistently across field patterns and .. patterns, or emit an error when any attributes are present on .. patterns. This is technically a breaking change, but there's precedent here (we've made misplaced #[inline] attributes into hard errors).

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-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions