Improve diagnostics for functions in struct
definitions
This commit is contained in:
parent
377d1a984c
commit
cecb3be49a
@ -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 {
|
||||
@ -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.
|
||||
/// ```
|
||||
|
@ -522,27 +522,27 @@ impl<'a> Parser<'a> {
|
||||
self.parse_ident_common(true)
|
||||
}
|
||||
|
||||
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)
|
||||
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> {
|
||||
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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user