Enhance error reporting for write!/writeln! macros#139371
Enhance error reporting for write!/writeln! macros#139371reez12g wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
@rustbot label -S-waiting-on-review +S-waiting-on-author |
This comment has been minimized.
This comment has been minimized.
| ); | ||
| *err.long_ty_path() = file; | ||
| err.note("type does not implement the `write_fmt` method"); | ||
| err.help("try adding `use std::fmt::Write;` to bring the trait into scope"); |
There was a problem hiding this comment.
The user could also have intended to use std::io::Write.
|
@rustbot ready |
mejrs
left a comment
There was a problem hiding this comment.
This doesn't fix the issue imo. This PR just causes a bunch of dead code further down that happens to include the diagnostic that points to the body of the macro. Also, the call to
is now unreachable - that's a really helpful error message that shouldn't be disabled.I think the problem is elsewhere, and the method that recommend similar names (or the caller(s) of it) isn't checking whether it's in a (foreign) macro expansion.
| note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
| --> $DIR/missing-writer.rs:5:12 | ||
| | | ||
| LL | write!("{}_{}", x, y); | ||
| | ^^^^^^^ | ||
| help: a writer is needed before this format string | ||
| --> $DIR/missing-writer.rs:5:12 | ||
| | | ||
| LL | write!("{}_{}", x, y); | ||
| | ^ | ||
| = note: type does not implement the `write_fmt` method | ||
| = help: try adding `use std::fmt::Write;` or `use std::io::Write;` to bring the appropriate trait into scope | ||
| = note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info) |
There was a problem hiding this comment.
This is a regression, bringing the Write trait into scope doesn't fix the problem because the problem is that the user forgot to even use a writer.
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: type does not implement the `write_fmt` method | ||
| = help: try adding `use std::fmt::Write;` or `use std::io::Write;` to bring the appropriate trait into scope |
There was a problem hiding this comment.
MyStruct doesn't implement either Write, so this doesn't help. The point of showing an error message that suggests importing traits is that we know the type implements the trait, but that the user cannot call its methods because the trait is not in scope. That's not the case here.
|
Thank you for the review. I'll reconsider the fix, including the points you mentioned. |
|
☔ The latest upstream changes (presumably #141545) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@reez12g any updates on this? thanks |
Closes #139051