Skip to content

Commit

Permalink
Rollup merge of #100475 - chenyukang:fix-100461, r=fee1-dead
Browse files Browse the repository at this point in the history
Give a helpful diagnostic when the next struct field has an attribute

Fixes #100461
  • Loading branch information
compiler-errors committed Aug 13, 2022
2 parents 7a34d39 + 52a1518 commit 29f905b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,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",
Expand Down
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

0 comments on commit 29f905b

Please sign in to comment.