Skip to content

Commit c52a13d

Browse files
authored
Rollup merge of rust-lang#56761 - estebank:path-display, r=zackmdavis
Suggest using `.display()` when trying to print a `Path` Fix rust-lang#38997.
2 parents af3f907 + 33a34b0 commit c52a13d

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/libcore/fmt/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,15 @@ pub trait Debug {
609609
/// println!("The origin is: {}", origin);
610610
/// ```
611611
#[rustc_on_unimplemented(
612+
on(
613+
_Self="std::path::Path",
614+
label="`{Self}` cannot be formatted with the default formatter; call `.display()` on it",
615+
note="call `.display()` or `.to_string_lossy()` to safely print paths, \
616+
as they may contain non-Unicode data"
617+
),
612618
message="`{Self}` doesn't implement `{Display}`",
613619
label="`{Self}` cannot be formatted with the default formatter",
614-
note="in format strings you may be able to use `{{:?}}` \
615-
(or {{:#?}} for pretty-print) instead",
620+
note="in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead",
616621
)]
617622
#[doc(alias = "{}")]
618623
#[stable(feature = "rust1", since = "1.0.0")]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::path::Path;
2+
3+
fn main() {
4+
let path = Path::new("/tmp/foo/bar.txt");
5+
println!("{}", path);
6+
//~^ ERROR E0277
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: `std::path::Path` doesn't implement `std::fmt::Display`
2+
--> $DIR/path-display.rs:5:20
3+
|
4+
LL | println!("{}", path);
5+
| ^^^^ `std::path::Path` cannot be formatted with the default formatter; call `.display()` on it
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `std::path::Path`
8+
= note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
9+
= note: required because of the requirements on the impl of `std::fmt::Display` for `&std::path::Path`
10+
= note: required by `std::fmt::Display::fmt`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)