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

rustc shows expanded writeln! macro in code suggestion #112007

Closed
HayleyDeckers opened this issue May 26, 2023 · 2 comments · Fixed by #112043
Closed

rustc shows expanded writeln! macro in code suggestion #112007

HayleyDeckers opened this issue May 26, 2023 · 2 comments · Fixed by #112043
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

@HayleyDeckers
Copy link

HayleyDeckers commented May 26, 2023

Code

fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result {
    if true {
        writeln!(w, "`;?` here ->")?;
    } else {
        writeln!(w, "but not here")
    }
    //intended code was `if [...] { writeln!(...) } else {writeln!(...)}?;`
    Ok(())
}

Current output

error[E0308]: mismatched types
 --> bug_report\src\lib.rs:5:9
  |
2 | /     if true {
3 | |         writeln!(w, "`;?` here ->")?;
4 | |     } else {
5 | |         writeln!(w, "but not here")
  | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
6 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<(), std::fmt::Error>`
  = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a semicolon here
  |
6 |     };
  |      +
help: you might have meant to return this value
 --> C:\Users\hayle\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\macros\mod.rs:557:9
  |
55|         return $dst.write_fmt($crate::format_args_nl!($($arg)*));
  |         ++++++                                                  +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `bug_report` (lib) due to previous error

Desired output

error[E0308]: mismatched types
 --> bug_report\src\lib.rs:5:9
  |
2 | /     if true {
3 | |         writeln!(w, "`;?` here ->")?;
4 | |     } else {
5 | |         writeln!(w, "but not here")
  | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
6 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<(), std::fmt::Error>`
  = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a semicolon here
  |
6 |     };
  |      +
help: you might have meant to return this value
 --> bug_report\src\lib.rs:5:9
  |
55|         return writeln!(w, "but not here");
  |         ++++++                            +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `bug_report` (lib) due to previous error

Rationale and extra context

It might be better still if it suggest adding ?; in the second branch, same as in the first. Or even a single ?; for the whole if-else, but whatever it suggests it probably shouldn't output the macro internals.

Other cases

If you leave off the Ok(()) at the end, it will warn you of the extra ;? in the first branch, as expected, instead.

Anything else?

No response

@HayleyDeckers HayleyDeckers 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 May 26, 2023
@mu001999
Copy link
Contributor

It seems to be the same as #110017

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented May 28, 2023

This looks similar to #95919

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

Successfully merging a pull request may close this issue.

3 participants