-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Enhance error reporting for write!/writeln! macros #139371
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
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user could also have intended to use std::io::Write
.
@rustbot ready |
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.
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
self.suggest_missing_writer(rcvr_ty, rcvr_expr) |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
Closes #139051