improve errors for incomplete functions in struct definitions

This commit is contained in:
Takayuki Maeda 2022-09-27 17:20:55 +09:00
parent 57ee5cf5a9
commit d7b6bd8f13
5 changed files with 42 additions and 16 deletions

View file

@ -1753,18 +1753,24 @@ impl<'a> Parser<'a> {
};
// We use `parse_fn` to get a span for the function
let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
if let Err(mut db) =
self.parse_fn(&mut AttrVec::new(), fn_parse_mode, lo, &inherited_vis)
{
db.delay_as_bug();
match self.parse_fn(&mut AttrVec::new(), fn_parse_mode, lo, &inherited_vis) {
Ok(_) => {
let mut err = self.struct_span_err(
lo.to(self.prev_token.span),
&format!("functions are not allowed in {adt_ty} 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
}
Err(err) => {
err.cancel();
self.restore_snapshot(snapshot);
self.expected_ident_found()
}
}
let mut err = self.struct_span_err(
lo.to(self.prev_token.span),
&format!("functions are not allowed in {adt_ty} 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 if self.eat_keyword(kw::Struct) {
match self.parse_item_struct() {
Ok((ident, _)) => {