Skip to content

Do not allow attributes on struct field rest patterns #136490

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

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,17 +1472,6 @@ impl<'a> Parser<'a> {
let mut last_non_comma_dotdot_span = None;

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
if !ate_comma {
let err = if self.token == token::At {
Expand Down Expand Up @@ -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 = match this.parse_pat_field(lo, attrs) {
Ok(field) => Ok(field),
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/parser/attribute/attr-pat-struct-rest.rs
Original file line number Diff line number Diff line change
@@ -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 `..`
}
10 changes: 10 additions & 0 deletions tests/ui/parser/attribute/attr-pat-struct-rest.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading