Skip to content

Commit 52a1518

Browse files
committed
give a helpful diagnostic even when the next struct field has an attribute
1 parent 1603a70 commit 52a1518

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

compiler/rustc_parse/src/parser/item.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,12 @@ impl<'a> Parser<'a> {
15391539
}
15401540
}
15411541

1542-
if self.token.is_ident() {
1543-
// This is likely another field; emit the diagnostic and keep going
1542+
if self.token.is_ident()
1543+
|| (self.token.kind == TokenKind::Pound
1544+
&& (self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Bracket))))
1545+
{
1546+
// This is likely another field, TokenKind::Pound is used for `#[..]` attribute for next field,
1547+
// emit the diagnostic and keep going
15441548
err.span_suggestion(
15451549
sp,
15461550
"try adding a comma",
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() { }
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() { }
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)