Skip to content

Imperfect diagnostic when attempting to print PathBuf #94210

Closed
@maghoff

Description

@maghoff

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.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions