Skip to content

Attributes on struct rest patterns are ignored #136108

@Skepfyr

Description

@Skepfyr

Attributes on struct field rest patterns are entirely ignored by the compiler, in fact they're dropped by the parser, so they can't generate any errors (beyond parse errors) but are accepted by the parser.

Specifically, this bit of the AST doesn't store the attributes:

/// Whether the `..` is present in a struct fields pattern.
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
pub enum PatFieldsRest {
/// `module::StructName { field, ..}`
Rest,
/// `module::StructName { field, syntax error }`
Recovered(ErrorGuaranteed),
/// `module::StructName { field }`
None,
}

The parser drops them on the floor here.

This means that the following code doesn't produce any errors:

let Foo { #[bad] .. } = Foo {};

And entertainingly the attribute is deleted by rustfmt...I assume that's because it's not represented in the AST.

Now, as far as I know there aren't any attributes that would be reasonable to use in this position, which is probably why the bug still exists.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.T-compilerRelevant to the compiler 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