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

Using a macro as the tail expression in a function that doesn't return a value should prompt to add a semicolon #100889

Open
shepmaster opened this issue Aug 22, 2022 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@shepmaster
Copy link
Member

shepmaster commented Aug 22, 2022

Given the following code:

fn demo() -> u8 {
    1
}

fn good() {
    demo()
}

fn subpar() {
    dbg!(demo())
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
6 |     demo()
  |     ^^^^^^ expected `()`, found `u8`
  |
help: consider using a semicolon here
  |
6 |     demo();
  |           +
help: try adding a return type
  |
5 | fn good() -> u8 {
  |           +++++

error[E0308]: mismatched types
  --> src/lib.rs:10:5
   |
9  | fn subpar() {
   |             - help: try adding a return type: `-> u8`
10 |     dbg!(demo())
   |     ^^^^^^^^^^^^ expected `()`, found `u8`
   |
   = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

Note that the subpar function does not have help for adding a semicolon.

Ideally the output should look like:

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
6 |     demo()
  |     ^^^^^^ expected `()`, found `u8`
  |
help: consider using a semicolon here
  |
6 |     demo();
  |           +
help: try adding a return type
  |
5 | fn good() -> u8 {
  |           +++++

error[E0308]: mismatched types
  --> src/lib.rs:10:5
   |
help: consider using a semicolon here
   |
10 |     dbg!(demo());
   |                 +
9  | fn subpar() {
   |             - help: try adding a return type: `-> u8`
10 |     dbg!(demo())
   |     ^^^^^^^^^^^^ expected `()`, found `u8`
   |
   = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

Note that the subpar function now has help for adding a semicolon.

@shepmaster shepmaster added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 22, 2022
@vincenzopalazzo
Copy link
Member

vincenzopalazzo commented Aug 22, 2022

@rustbot claim

@rustbot
Copy link
Collaborator

rustbot commented Aug 22, 2022

Error: Parsing assign command in comment failed: ...'bot assign' | error: specify user to assign to at >| ''...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@gimbling-away
Copy link
Contributor

gimbling-away commented Aug 25, 2022

Note that this also happens on simple expressions, there is no "add semicolon" diagnostic here either

fn demo() {
    1
}
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/lib.rs:2:5
  |
1 | fn demo() {
  |           - help: try adding a return type: `-> i32`
2 |     1
  |     ^ expected `()`, found integer

Also, ideally ideally we should really show that the different help suggestions can not be used together, rather the developer can pick one of them, making an issue for this :)

@vincenzopalazzo vincenzopalazzo removed their assignment Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants