Skip to content

Commit 06b2f62

Browse files
authored
Rollup merge of #136490 - Skepfyr:no-field-rest-pattern-attrs, r=compiler-errors
Do not allow attributes on struct field rest patterns Fixes #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).
2 parents 522c8f7 + 3f09a20 commit 06b2f62

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

Diff for: compiler/rustc_parse/src/parser/pat.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1472,17 +1472,6 @@ impl<'a> Parser<'a> {
14721472
let mut last_non_comma_dotdot_span = None;
14731473

14741474
while self.token != token::CloseDelim(Delimiter::Brace) {
1475-
let attrs = match self.parse_outer_attributes() {
1476-
Ok(attrs) => attrs,
1477-
Err(err) => {
1478-
if let Some(delayed) = delayed_err {
1479-
delayed.emit();
1480-
}
1481-
return Err(err);
1482-
}
1483-
};
1484-
let lo = self.token.span;
1485-
14861475
// check that a comma comes after every field
14871476
if !ate_comma {
14881477
let err = if self.token == token::At {
@@ -1585,6 +1574,17 @@ impl<'a> Parser<'a> {
15851574
}
15861575
}
15871576

1577+
let attrs = match self.parse_outer_attributes() {
1578+
Ok(attrs) => attrs,
1579+
Err(err) => {
1580+
if let Some(delayed) = delayed_err {
1581+
delayed.emit();
1582+
}
1583+
return Err(err);
1584+
}
1585+
};
1586+
let lo = self.token.span;
1587+
15881588
let field = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
15891589
let field = match this.parse_pat_field(lo, attrs) {
15901590
Ok(field) => Ok(field),

Diff for: tests/ui/parser/attribute/attr-pat-struct-rest.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// #81282: Attributes are not allowed on struct field rest patterns (the ..).
2+
3+
struct S {}
4+
5+
fn main() {
6+
let S { #[cfg(any())] .. } = S {};
7+
//~^ ERROR expected identifier, found `..`
8+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected identifier, found `..`
2+
--> $DIR/attr-pat-struct-rest.rs:6:27
3+
|
4+
LL | let S { #[cfg(any())] .. } = S {};
5+
| - ^^ expected identifier
6+
| |
7+
| while parsing the fields for this pattern
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)