Skip to content
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

Merged
merged 2 commits into from
Sep 30, 2022

Conversation

TaKO8Ki
Copy link
Member

@TaKO8Ki TaKO8Ki commented Sep 27, 2022

Given the following code:

fn main() {}

struct Foo {
    fn
}

playground

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.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 27, 2022
@rust-highfive
Copy link
Collaborator

r? @fee1-dead

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 27, 2022
= 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 ()
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member

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

Copy link
Member Author

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.

src/test/ui/structs/incomplete-fn-in-struct-definition.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@TaKO8Ki TaKO8Ki force-pushed the incomplete-fn-in-struct-definition branch from 1679cb1 to e665d20 Compare September 28, 2022 02:44
@TaKO8Ki TaKO8Ki requested a review from fee1-dead September 28, 2022 03:55
@fee1-dead
Copy link
Member

Sorry for the late review.
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 30, 2022

📌 Commit e665d20 has been approved by fee1-dead

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 30, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 30, 2022
…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.
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 30, 2022
…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
@bors bors merged commit 6906e64 into rust-lang:master Sep 30, 2022
@rustbot rustbot added this to the 1.66.0 milestone Sep 30, 2022
@TaKO8Ki TaKO8Ki deleted the incomplete-fn-in-struct-definition branch September 30, 2022 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants