-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve errors for incomplete functions in struct definitions #102350
Improve errors for incomplete functions in struct definitions #102350
Conversation
r? @fee1-dead (rust-highfive has picked a reviewer for you, use r? to override) |
= help: see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information | ||
help: escape `fn` to use it as an identifier | ||
| | ||
LL | inner : dyn r#fn () |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. This doesn't seem to be useful. They might have meant Fn
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In edition 2021, rustc seems suggests like the following:
error: expected identifier, found keyword `fn`
--> src/main.rs:4:17
|
4 | inner : dyn fn ()
| ^^ expected identifier, found keyword
|
help: escape `fn` to use it as an identifier
|
4 | inner : dyn r#fn ()
| ++
error[E0405]: cannot find trait `r#fn` in this scope
--> src/main.rs:4:17
|
4 | inner : dyn fn ()
| ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
|
::: /Users/maeda.takayuki/GitHub/rust/rust/library/core/src/ops/function.rs:75:1
|
75 | pub trait Fn<Args>: FnMut<Args> {
| ------------------------------- similarly named trait `Fn` defined here
For more information about this error, try `rustc --explain E0405`.
error: could not compile `debug_playground` due to 2 previous errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So could we not emit the first help if we know that it should be a trait? Or maybe just delay this error when we know that there will be path resolution anyways?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if that is doable though, it is fine to do it in a followup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if that is doable though, it is fine to do it in a followup
Sure. I will do it as a follow-up.
This comment has been minimized.
This comment has been minimized.
1679cb1
to
e665d20
Compare
Sorry for the late review. |
…finition, 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.
…iaskrgr Rollup of 5 pull requests Successful merges: - rust-lang#101075 (Migrate rustc_codegen_gcc to SessionDiagnostics ) - rust-lang#102350 (Improve errors for incomplete functions in struct definitions) - rust-lang#102481 (rustdoc: remove unneeded CSS `.rust-example-rendered { position }`) - rust-lang#102491 (rustdoc: remove no-op source sidebar `opacity`) - rust-lang#102499 (Adjust the s390x data layout for LLVM 16) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Given the following code:
playground
The current output is:
In this case, rustc should suggest escaping
fn
to use it as an identifier.