1
Fork 0

Do not allow attributes on struct field rest patterns

This removes support for attributes on struct field rest patterns (the `..`) from the parser.
Previously they were being parsed but dropped from the AST, so didn't work and were deleted by rustfmt.
This commit is contained in:
Jack Rickard 2025-02-03 13:56:20 +00:00
parent 01a26c026d
commit 3f09a20549
No known key found for this signature in database
GPG key ID: 88084D7D08A72C8A
3 changed files with 29 additions and 11 deletions

View file

@ -1472,17 +1472,6 @@ impl<'a> Parser<'a> {
let mut last_non_comma_dotdot_span = None; let mut last_non_comma_dotdot_span = None;
while self.token != token::CloseDelim(Delimiter::Brace) { while self.token != token::CloseDelim(Delimiter::Brace) {
let attrs = match self.parse_outer_attributes() {
Ok(attrs) => attrs,
Err(err) => {
if let Some(delayed) = delayed_err {
delayed.emit();
}
return Err(err);
}
};
let lo = self.token.span;
// check that a comma comes after every field // check that a comma comes after every field
if !ate_comma { if !ate_comma {
let err = if self.token == token::At { let err = if self.token == token::At {
@ -1585,6 +1574,17 @@ impl<'a> Parser<'a> {
} }
} }
let attrs = match self.parse_outer_attributes() {
Ok(attrs) => attrs,
Err(err) => {
if let Some(delayed) = delayed_err {
delayed.emit();
}
return Err(err);
}
};
let lo = self.token.span;
let field = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| { let field = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
let field = match this.parse_pat_field(lo, attrs) { let field = match this.parse_pat_field(lo, attrs) {
Ok(field) => Ok(field), Ok(field) => Ok(field),

View file

@ -0,0 +1,8 @@
// #81282: Attributes are not allowed on struct field rest patterns (the ..).
struct S {}
fn main() {
let S { #[cfg(any())] .. } = S {};
//~^ ERROR expected identifier, found `..`
}

View file

@ -0,0 +1,10 @@
error: expected identifier, found `..`
--> $DIR/attr-pat-struct-rest.rs:6:27
|
LL | let S { #[cfg(any())] .. } = S {};
| - ^^ expected identifier
| |
| while parsing the fields for this pattern
error: aborting due to 1 previous error