Rollup merge of #102350 - TaKO8Ki:incomplete-fn-in-struct-definition, r=fee1-dead
Improve errors for incomplete functions in struct definitions Given the following code: ```rust fn main() {} struct Foo { fn } ``` [playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=29139f870511f6918324be5ddc26c345) The current output is: ``` Compiling playground v0.0.1 (/playground) error: functions are not allowed in struct definitions --> src/main.rs:4:5 | 4 | fn | ^^ | = help: unlike in C++, Java, and C#, functions are declared in `impl` blocks = help: see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information error: could not compile `playground` due to previous error ``` In this case, rustc should suggest escaping `fn` to use it as an identifier.
This commit is contained in:
commit
6906e64c30
5 changed files with 44 additions and 16 deletions
|
@ -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, _)) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue