Skip to content

Rust incorrectly concludes that trait is not implemented #82252

Description

@survived

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.

Here's a link on playground

Meta

Reproduces at Rust 1.50.0 stable, beta, nightly

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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