Skip to content

Attributes on struct rest patterns are ignored #136108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Skepfyr opened this issue Jan 26, 2025 · 3 comments
Closed

Attributes on struct rest patterns are ignored #136108

Skepfyr opened this issue Jan 26, 2025 · 3 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Skepfyr
Copy link
Contributor

Skepfyr commented Jan 26, 2025

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.

@Skepfyr Skepfyr added the C-bug Category: This is a bug. label Jan 26, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 26, 2025
@ehuss
Copy link
Contributor

ehuss commented Jan 27, 2025

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.

I would expect #[cfg] to work there, but it is also ignored.

@fmease fmease added A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 27, 2025
@petrochenkov
Copy link
Contributor

Duplicate of #81282.
I think my answer in #81282 (comment) is still relevant.

@ehuss
Copy link
Contributor

ehuss commented Jan 27, 2025

Ah, I knew there was an issue somewhere, but I was searching for the word "rest" and wasn't finding that. Thanks!

@ehuss ehuss closed this as completed Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants