Skip to content

Attributes are silently ignored on .. (rest) in struct pattern (StructPatternEtCetera) #81282

Closed
@Aaron1011

Description

@Aaron1011

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-parserArea: The lexing & parsing of Rust source code to an ASTA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.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