Files
rust/tests/ui/traits/explicit-reference-cast.rs
Esteban Küber 3ad92c9f23 On E0277 tweak help when single type impls traits
When encountering an unmet predicate, when we point at the trait impls that do exist, if they are all for the same self type, tweak the wording to make it less verbose.
2026-03-21 02:24:36 +00:00

51 lines
1.6 KiB
Rust

// compile-fail
use std::convert::TryFrom;
use std::path::{Path, PathBuf};
pub struct ToolA(PathBuf);
//~^ HELP the trait `From<&PathBuf>` is not implemented for `ToolA`
impl From<&Path> for ToolA {
//~^ HELP `ToolA` implements trait `From<T>`
fn from(p: &Path) -> ToolA {
ToolA(p.to_path_buf())
}
}
// Add a different From<T> impl to ensure we suggest the correct cast
impl From<&str> for ToolA {
fn from(s: &str) -> ToolA {
ToolA(PathBuf::from(s))
}
}
pub struct ToolB(PathBuf);
//~^ HELP the trait `From<&PathBuf>` is not implemented for `ToolB`
//~| HELP the trait `From<&PathBuf>` is not implemented for `ToolB`
impl TryFrom<&Path> for ToolB {
//~^ HELP the trait `TryFrom<&PathBuf>` is not implemented for `ToolB`
//~| HELP the trait `TryFrom<&PathBuf>` is not implemented for `ToolB`
type Error = ();
fn try_from(p: &Path) -> Result<ToolB, ()> {
Ok(ToolB(p.to_path_buf()))
}
}
fn main() {
let path = PathBuf::new();
let _ = ToolA::from(&path);
//~^ ERROR the trait bound `ToolA: From<&PathBuf>` is not satisfied
//~| HELP consider casting the `&PathBuf` value to `&Path`
let _ = ToolB::try_from(&path);
//~^ ERROR the trait bound `ToolB: TryFrom<&PathBuf>` is not satisfied
//~| ERROR the trait bound `ToolB: From<&PathBuf>` is not satisfied
//~| HELP consider casting the `&PathBuf` value to `&Path`
//~| HELP consider casting the `&PathBuf` value to `&Path`
//~| HELP for that trait implementation, expected `Path`, found `PathBuf`
//~| HELP for that trait implementation, expected `Path`, found `PathBuf`
}