Skip to content

Commit 29f905b

Browse files
Rollup merge of #100475 - chenyukang:fix-100461, r=fee1-dead
Give a helpful diagnostic when the next struct field has an attribute Fixes #100461
2 parents 7a34d39 + 52a1518 commit 29f905b

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,12 @@ impl<'a> Parser<'a> {
15441544
}
15451545
}
15461546

1547-
if self.token.is_ident() {
1548-
// This is likely another field; emit the diagnostic and keep going
1547+
if self.token.is_ident()
1548+
|| (self.token.kind == TokenKind::Pound
1549+
&& (self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Bracket))))
1550+
{
1551+
// This is likely another field, TokenKind::Pound is used for `#[..]` attribute for next field,
1552+
// emit the diagnostic and keep going
15491553
err.span_suggestion(
15501554
sp,
15511555
"try adding a comma",

Diff for: src/test/ui/parser/struct-filed-with-attr.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
2+
// run-rustfix
3+
4+
struct Feelings {
5+
owo: bool,
6+
//~^ ERROR expected `,`, or `}`, found `#`
7+
#[allow(unused)]
8+
uwu: bool,
9+
}
10+
11+
impl Feelings {
12+
#[allow(unused)]
13+
fn hmm(&self) -> bool {
14+
self.owo
15+
}
16+
}
17+
18+
fn main() { }

Diff for: src/test/ui/parser/struct-filed-with-attr.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
2+
// run-rustfix
3+
4+
struct Feelings {
5+
owo: bool
6+
//~^ ERROR expected `,`, or `}`, found `#`
7+
#[allow(unused)]
8+
uwu: bool,
9+
}
10+
11+
impl Feelings {
12+
#[allow(unused)]
13+
fn hmm(&self) -> bool {
14+
self.owo
15+
}
16+
}
17+
18+
fn main() { }

Diff for: src/test/ui/parser/struct-filed-with-attr.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected `,`, or `}`, found `#`
2+
--> $DIR/struct-filed-with-attr.rs:5:14
3+
|
4+
LL | owo: bool
5+
| ^ help: try adding a comma: `,`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)