Skip to content

Give a helpful diagnostic when the next struct field has an attribute #100475

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
Aug 14, 2022
Merged
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
give a helpful diagnostic even when the next struct field has an attr…
…ibute
  • Loading branch information
chenyukang committed Aug 13, 2022
commit 52a15180d2bc193aceea7302dd175f2c872df340
8 changes: 6 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
@@ -1539,8 +1539,12 @@ impl<'a> Parser<'a> {
}
}

if self.token.is_ident() {
// This is likely another field; emit the diagnostic and keep going
if self.token.is_ident()
|| (self.token.kind == TokenKind::Pound
&& (self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Bracket))))
{
// This is likely another field, TokenKind::Pound is used for `#[..]` attribute for next field,
// emit the diagnostic and keep going
err.span_suggestion(
sp,
"try adding a comma",
18 changes: 18 additions & 0 deletions src/test/ui/parser/struct-filed-with-attr.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
// run-rustfix

struct Feelings {
owo: bool,
//~^ ERROR expected `,`, or `}`, found `#`
#[allow(unused)]
uwu: bool,
}

impl Feelings {
#[allow(unused)]
fn hmm(&self) -> bool {
self.owo
}
}

fn main() { }
18 changes: 18 additions & 0 deletions src/test/ui/parser/struct-filed-with-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
// run-rustfix

struct Feelings {
owo: bool
//~^ ERROR expected `,`, or `}`, found `#`
#[allow(unused)]
uwu: bool,
}

impl Feelings {
#[allow(unused)]
fn hmm(&self) -> bool {
self.owo
}
}

fn main() { }
8 changes: 8 additions & 0 deletions src/test/ui/parser/struct-filed-with-attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected `,`, or `}`, found `#`
--> $DIR/struct-filed-with-attr.rs:5:14
|
LL | owo: bool
| ^ help: try adding a comma: `,`

error: aborting due to previous error