-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed as duplicate of#81282
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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:
rust/compiler/rustc_ast/src/ast.rs
Lines 845 to 854 in 01a26c0
/// 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.
fmease
Metadata
Metadata
Assignees
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.