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

Confusing suggestions for error[E0599]: cannot write into String #139051

Open
jjl9807 opened this issue Mar 28, 2025 · 1 comment · May be fixed by #139371
Open

Confusing suggestions for error[E0599]: cannot write into String #139051

jjl9807 opened this issue Mar 28, 2025 · 1 comment · May be fixed by #139371
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jjl9807
Copy link

jjl9807 commented Mar 28, 2025

Code

fn main() {
    let mut s = String::new();
    writeln!(s,"{}", true).unwrap();
}

Current output

error[E0599]: cannot write into `String`
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    |
   ::: C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\fmt\mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |
help: there is a method `write_str` with a similar name
   --> C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\macros\mod.rs:688:14
    |
688 |         $dst.write_str($crate::format_args_nl!($($arg)*))
    |              ~~~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.

Desired output

error[E0599]: cannot write into `String`
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    |
   ::: C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\fmt\mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.

Rationale and extra context

The second suggestion in the error message is confusing. The content at line 688 of the file rustlib/src/rust\library\core\src\macros\mod.rs is $dst.write_fmt($crate::format_args_nl!($($arg)*)), which does not correspond to the definition of the write_str method.

Other cases

Rust Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-msvc
release: 1.85.1
LLVM version: 19.1.7

Anything else?

No response

@jjl9807 jjl9807 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 Mar 28, 2025
@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 4, 2025
@fmease
Copy link
Member

fmease commented Apr 4, 2025

Potential fix for this category of issues: Suppress the suggestion if the span is .in_external_macro(map).

@reez12g reez12g linked a pull request Apr 4, 2025 that will close this issue
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

2 participants