Closed
Description
Suppose I have a function:
fn cast<T>(n: U64) -> T // U64 is my own defined type
where
for<'a> T: TryFrom<&'a U64>,
for<'a> <T as TryFrom<&'a U64>>::Error: Debug,
{
T::try_from(&n).unwrap()
}
Here, for some reason, Rust refuses to infer that <T as TryFrom<&'a U64>>::Error
implements Debug. Can be checked by adding more code:
struct U32(u32);
struct U64(u64);
#[derive(Debug)]
struct TooLargeErr;
impl TryFrom<&U64> for U32 {
type Error = TooLargeErr;
fn try_from(n: &U64) -> Result<U32, Self::Error> {
u32::try_from(n.0).map(U32).map_err(|_| TooLargeErr)
}
}
fn main() {
// This fails to compile because: `<_ as TryFrom<&'a U64>>::Error` ... doesn't implement `Debug`
// However, `TooLargeErr` does implement Debug.
let n: U32 = cast(U64(1));
assert_eq!(n.0, 1);
}
If we replace main function with:
fn main() {
let n = U32::try_from(&U64(1)).unwrap();
assert_eq!(n.0, 1);
}
then it will be compiled, so Rust is able to infer that TooLargeErr implements Debug, but it doesn't do the same in generic bounds.
Meta
Reproduces at Rust 1.50.0 stable, beta, nightly
Metadata
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Trait systemCategory: This is a bug.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.