Skip to content

Commit

Permalink
Improve diagnostics for functions in struct definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed May 7, 2021
1 parent 377d1a9 commit cecb3be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
25 changes: 24 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Expand Up @@ -1402,7 +1402,7 @@ impl<'a> Parser<'a> {
vis: Visibility,
attrs: Vec<Attribute>,
) -> PResult<'a, FieldDef> {
let name = self.parse_ident_common(false)?;
let name = self.parse_field_ident(lo)?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
Ok(FieldDef {
Expand All @@ -1416,6 +1416,29 @@ impl<'a> Parser<'a> {
})
}

/// Parses a field identifier. Specialized version of `parse_ident_common`
/// for better diagnostics and suggestions.
fn parse_field_ident(&mut self, lo: Span) -> PResult<'a, Ident> {
let (ident, is_raw) = self.ident_or_err()?;
if !is_raw && ident.is_reserved() {
let err = if self.check_fn_front_matter(false) {
let _ = self.parse_fn(&mut Vec::new(), |_| true, lo);
let mut err = self.struct_span_err(
lo.to(self.prev_token.span),
"functions are not allowed in struct definitions",
);
err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
err
} else {
self.expected_ident_found()
};
return Err(err);
}
self.bump();
Ok(ident)
}

/// Parses a declarative macro 2.0 definition.
/// The `macro` keyword has already been parsed.
/// ```
Expand Down
36 changes: 18 additions & 18 deletions compiler/rustc_parse/src/parser/mod.rs
Expand Up @@ -522,27 +522,27 @@ impl<'a> Parser<'a> {
self.parse_ident_common(true)
}

fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
self.token.ident().ok_or_else(|| match self.prev_token.kind {
TokenKind::DocComment(..) => {
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
}
_ => self.expected_ident_found(),
})
}

fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
match self.token.ident() {
Some((ident, is_raw)) => {
if !is_raw && ident.is_reserved() {
let mut err = self.expected_ident_found();
if recover {
err.emit();
} else {
return Err(err);
}
}
self.bump();
Ok(ident)
let (ident, is_raw) = self.ident_or_err()?;
if !is_raw && ident.is_reserved() {
let mut err = self.expected_ident_found();
if recover {
err.emit();
} else {
return Err(err);
}
_ => Err(match self.prev_token.kind {
TokenKind::DocComment(..) => {
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
}
_ => self.expected_ident_found(),
}),
}
self.bump();
Ok(ident)
}

/// Checks if the next token is `tok`, and returns `true` if so.
Expand Down

0 comments on commit cecb3be

Please sign in to comment.