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

Imperfect diagnostic when attempting to print PathBuf #94210

Closed
maghoff opened this issue Feb 21, 2022 · 1 comment · Fixed by #94240
Closed

Imperfect diagnostic when attempting to print PathBuf #94210

maghoff opened this issue Feb 21, 2022 · 1 comment · Fixed by #94240
Assignees
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

@maghoff
Copy link

maghoff commented Feb 21, 2022

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=175c4f812be74f3fdab4a225687bfb23

use std::path::*;

fn main() {
    let p = PathBuf::new();
    println!("{}", p);           // rustc suggests using {:?} or {:#?}
    println!("{}", p.as_path()); // rustc suggests using .display() or .to_string_lossy()
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): `std::path::PathBuf` doesn't implement `std::fmt::Display`
 [--> src/main.rs:5:20
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
5 |     println!("{}", p);           // rustc suggests using {:?} or {:#?}
  |                    ^ `std::path::PathBuf` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `std::path::PathBuf`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): `Path` doesn't implement `std::fmt::Display`
 [--> src/main.rs:6:20
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
6 |     println!("{}", p.as_path()); // rustc suggests using .display() or .to_string_lossy()
  |                    ^^^^^^^^^^^ `Path` cannot be formatted with the default formatter; call `.display()` on it
  |
  = help: the trait `std::fmt::Display` is not implemented for `Path`
  = note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
  = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look like:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): `std::path::PathBuf` doesn't implement `std::fmt::Display`
 [--> src/main.rs:5:20
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
5 |     println!("{}", p);           // rustc suggests using {:?} or {:#?}
  |                    ^ `std::path::PathBuf` cannot be formatted with the default formatter; call `.display()` on it
  |
  = help: the trait `std::fmt::Display` is not implemented for `std::path::PathBuf`
  = note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
  = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): `Path` doesn't implement `std::fmt::Display`
 [--> src/main.rs:6:20
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
6 |     println!("{}", p.as_path()); // rustc suggests using .display() or .to_string_lossy()
  |                    ^^^^^^^^^^^ `Path` cannot be formatted with the default formatter; call `.display()` on it
  |
  = help: the trait `std::fmt::Display` is not implemented for `Path`
  = note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
  = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors

The change in output is only to make sure the tip to use .display() or .to_string_lossy() shows up when attempting to print a PathBuf. It currently only shows up when attempting to print a Path. The tips shows up as a note in addition to showing up in the – uhh... – error message that points at the problem.

@maghoff maghoff 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 Feb 21, 2022
@compiler-errors
Copy link
Member

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 23, 2022
…lcnr

Suggest calling .display() on `PathBuf` too

Fixes rust-lang#94210
@bors bors closed this as completed in 40afbdd Feb 23, 2022
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.

2 participants