-
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
Open
reez12g
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
reez12g:issue-139051
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Issue #139051 - Test for the case where io::Write would be more appropriate | ||
// | ||
// Test that when using write! on a type that doesn't implement std::io::Write trait, | ||
// we get a clear error message suggesting to import the appropriate trait. | ||
// | ||
// edition:2021 | ||
// ignore-msvc | ||
// ignore-emscripten | ||
// run-fail | ||
// check-pass | ||
|
||
fn main() { | ||
// Simple struct that doesn't implement std::io::Write | ||
struct MyIoStruct { | ||
value: i32, | ||
} | ||
|
||
let mut s = MyIoStruct { value: 42 }; | ||
|
||
// This should generate E0599 with the improved error message | ||
// suggesting io::Write instead | ||
write!(s, "Hello, world!"); //~ ERROR cannot write into `MyIoStruct` | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/ui/E0599-write-macro/io-write-fmt-method-error.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0599]: cannot write into `MyIoStruct` | ||
--> $DIR/io-write-fmt-method-error.rs:22:5 | ||
| | ||
LL | write!(s, "Hello, world!"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Issue #139051 - Ensure we don't get confusing suggestions for E0599 | ||
// on write!/writeln! macros | ||
// | ||
// Test that when using write!/writeln! macros with a type that doesn't implement | ||
// std::fmt::Write trait, we get a clear error message without irrelevant suggestions. | ||
// | ||
// edition:2021 | ||
// ignore-msvc | ||
// ignore-emscripten | ||
// run-fail | ||
// check-pass | ||
|
||
fn main() { | ||
// Simple struct that doesn't implement std::fmt::Write | ||
struct MyStruct { | ||
value: i32, | ||
} | ||
|
||
let mut s = MyStruct { value: 42 }; | ||
|
||
// This should generate E0599 with the improved error message | ||
// and not suggest irrelevant methods like write_str or push_str | ||
write!(s, "Hello, world!"); //~ ERROR cannot write into `MyStruct` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0599]: cannot write into `MyStruct` | ||
--> $DIR/write-fmt-method-error.rs:23:5 | ||
| | ||
LL | write!(s, "Hello, world!"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Issue #139051 - Ensure we don't get confusing suggestions for E0599 | ||
// on write!/writeln! macros | ||
// | ||
// Test that when using write!/writeln! macros with a type that doesn't implement | ||
// std::fmt::Write trait, we get a clear error message without irrelevant suggestions. | ||
// | ||
// edition:2021 | ||
// ignore-msvc | ||
// ignore-emscripten | ||
// run-fail | ||
// check-pass | ||
|
||
fn main() { | ||
// Simple struct that doesn't implement std::fmt::Write | ||
struct MyStruct { | ||
value: i32, | ||
} | ||
|
||
let mut s = MyStruct { value: 42 }; | ||
|
||
// This should generate E0599 with the improved error message | ||
// and not suggest irrelevant methods like write_str or push_str | ||
writeln!(s, "Hello, world!"); //~ ERROR cannot write into `MyStruct` | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/ui/E0599-write-macro/writeln-fmt-method-error.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0599]: cannot write into `MyStruct` | ||
--> $DIR/writeln-fmt-method-error.rs:23:5 | ||
| | ||
LL | writeln!(s, "Hello, world!"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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 `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,38 +21,24 @@ LL | writeln!("{}_{}", "{} {}", x, y); | |
| ++++++++ | ||
|
||
error[E0599]: cannot write into `&'static str` | ||
--> $DIR/missing-writer.rs:5:12 | ||
--> $DIR/missing-writer.rs:5:5 | ||
| | ||
LL | write!("{}_{}", x, y); | ||
| -------^^^^^^^------- method not found in `&str` | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
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) | ||
Comment on lines
-29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
error[E0599]: cannot write into `&'static str` | ||
--> $DIR/missing-writer.rs:11:14 | ||
--> $DIR/missing-writer.rs:11:5 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ---------^^^^^^^------- method not found in `&str` | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
--> $DIR/missing-writer.rs:11:14 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ^^^^^^^ | ||
help: a writer is needed before this format string | ||
--> $DIR/missing-writer.rs:11:14 | ||
| | ||
LL | writeln!("{}_{}", 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 `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 eitherWrite
, 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.