give a helpful diagnostic even when the next struct field has an attribute

This commit is contained in:
yukang 2022-08-13 12:50:53 +08:00
parent 1603a70f82
commit 52a15180d2
4 changed files with 50 additions and 2 deletions

View File

@ -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",

View File

@ -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() { }

View File

@ -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() { }

View File

@ -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