-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Attributes are silently ignored on ..
(rest) in struct pattern (StructPatternEtCetera)
#81282
Comments
..
in struct pattern..
in struct pattern
Technically |
The reference states that |
Actually disregard my comment, it's true that I'd say they should not despite what the reference says. struct S {}
fn main() {
let s = S {};
let z = S { #[nonexistent] ..s }; // error: expected identifier, found `..`
} and we should be able to remove it from patterns too because it's not implemented correctly (the attributes on If someone provides good motivation, then it can always be added in the future. |
I have a use case for this: Allowing exhaustive matching of conceptually non-exhaustive structs, like what syn does for enums: let Foo {
field_a,
field_b,
field_c,
#[cfg(test)]
__test_exhaustive: _,
#[cfg(not(test))]
..
} = foo; or alternatively with the lint suggested in #44109 (comment): let Foo {
field_a,
field_b,
field_c,
// could also be warn(reachable) + `-D warnings` in CI
#[cfg_attr(test, deny(reachable))]
..
} = foo; With that, you have an automated way of being notified (through test failure) when a struct got additional fields without failing compilation entirely, in the second case for anything that's |
@jplatte I believe the |
Heh yes, I'm well aware of it ^^ |
Yeahh, I opted against |
..
in struct pattern..
(rest) in struct pattern (StructPatternEtCetera)
… r=<try> Do not allow attributes on struct field rest patterns Fixes rust-lang#81282. This removes support for attributes on struct field rest patterns (the `..` bit) from the parser. Previously any attributes were being parsed but dropped from the AST, so didn't work and were deleted by rustfmt. This needs an equivalent change to the reference but I wanted to see how this PR is received first. The error message it produces isn't great, however it does match the error you get if you try to add attributes to .. in struct expressions atm, although I can understand wanting to do better given this was previously accepted. I think I could move attribute parsing back up to where it was and then emit a specific new error for this case, however I might need some guidance as this is the first time I've messed around inside the compiler. While this is technically breaking I don't think it's much of an issue: attributes in this position don't currently do anything and rustfmt outright deletes them, meaning it's incredibly unlikely to affect anyone. I have already made the equivalent change to *add* support for attributes (mostly) but the conversation in the linked issue suggested it would be more reasonable to just remove them (and pointed out it's much easier to add support later if we realise we need them).
Rollup merge of rust-lang#136490 - Skepfyr:no-field-rest-pattern-attrs, r=compiler-errors Do not allow attributes on struct field rest patterns Fixes rust-lang#81282. This removes support for attributes on struct field rest patterns (the `..` bit) from the parser. Previously any attributes were being parsed but dropped from the AST, so didn't work and were deleted by rustfmt. This needs an equivalent change to the reference but I wanted to see how this PR is received first. The error message it produces isn't great, however it does match the error you get if you try to add attributes to .. in struct expressions atm, although I can understand wanting to do better given this was previously accepted. I think I could move attribute parsing back up to where it was and then emit a specific new error for this case, however I might need some guidance as this is the first time I've messed around inside the compiler. While this is technically breaking I don't think it's much of an issue: attributes in this position don't currently do anything and rustfmt outright deletes them, meaning it's incredibly unlikely to affect anyone. I have already made the equivalent change to *add* support for attributes (mostly) but the conversation in the linked issue suggested it would be more reasonable to just remove them (and pointed out it's much easier to add support later if we realise we need them).
The following code compiles successfully:
However, placing attributes on a normal field pattern:
produces the following 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).The text was updated successfully, but these errors were encountered: