Improve diagnostics for functions in struct
definitions
This commit is contained in:
parent
377d1a984c
commit
cecb3be49a
2 changed files with 43 additions and 20 deletions
|
@ -1402,7 +1402,7 @@ impl<'a> Parser<'a> {
|
||||||
vis: Visibility,
|
vis: Visibility,
|
||||||
attrs: Vec<Attribute>,
|
attrs: Vec<Attribute>,
|
||||||
) -> PResult<'a, FieldDef> {
|
) -> PResult<'a, FieldDef> {
|
||||||
let name = self.parse_ident_common(false)?;
|
let name = self.parse_field_ident(lo)?;
|
||||||
self.expect(&token::Colon)?;
|
self.expect(&token::Colon)?;
|
||||||
let ty = self.parse_ty()?;
|
let ty = self.parse_ty()?;
|
||||||
Ok(FieldDef {
|
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.
|
/// Parses a declarative macro 2.0 definition.
|
||||||
/// The `macro` keyword has already been parsed.
|
/// The `macro` keyword has already been parsed.
|
||||||
/// ```
|
/// ```
|
||||||
|
|
|
@ -522,9 +522,17 @@ impl<'a> Parser<'a> {
|
||||||
self.parse_ident_common(true)
|
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> {
|
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
|
||||||
match self.token.ident() {
|
let (ident, is_raw) = self.ident_or_err()?;
|
||||||
Some((ident, is_raw)) => {
|
|
||||||
if !is_raw && ident.is_reserved() {
|
if !is_raw && ident.is_reserved() {
|
||||||
let mut err = self.expected_ident_found();
|
let mut err = self.expected_ident_found();
|
||||||
if recover {
|
if recover {
|
||||||
|
@ -536,14 +544,6 @@ impl<'a> Parser<'a> {
|
||||||
self.bump();
|
self.bump();
|
||||||
Ok(ident)
|
Ok(ident)
|
||||||
}
|
}
|
||||||
_ => Err(match self.prev_token.kind {
|
|
||||||
TokenKind::DocComment(..) => {
|
|
||||||
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
|
|
||||||
}
|
|
||||||
_ => self.expected_ident_found(),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if the next token is `tok`, and returns `true` if so.
|
/// Checks if the next token is `tok`, and returns `true` if so.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue