Skip to content

Compiler fails to see an existing trait implementation for an indirectly referred type #132913

Open
@newpavlov

Description

I get compilation errors for a typenum-based code which can be minimized to this snippet (playground):

use core::ops::Add;
use typenum::{Sum, Unsigned, U8};

pub type TagSize = U8;

pub type TagSizePlus1 = typenum::Add1<TagSize>; // does not work
// pub type TagSizePlus1 = typenum::U9; // works

pub fn foo<N>()
where
    N: Add<TagSizePlus1>,
    Sum<N, TagSizePlus1>: Unsigned,
{
    // code
}

Add1<A> is an alias for <A as Add<B1>>::Output; and Sum<A, B> is an alias for <A as Add<B>>::Output;

The snippet results in the following compilation error:

error[E0277]: cannot add `UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>` to `N`
  --> src/lib.rs:10:1
   |
10 | / pub fn foo<N>()
11 | | where
12 | |     N: Add<TagSizePlus1>,
13 | |     Sum<N, TagSizePlus1>: Unsigned,
   | |___________________________________^ no implementation for `N + UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>`

But replacing Sub1<SomeSize> with U9 (an alias for UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>) works just fine!

It looks like the compiler successfully resolves Add1<SomeSize> to UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, but then for some reason gets tripped while resolving the bounds and is unable to see that the bound on N which allows the addition.

Potentially relevant issue: #79629

Activity

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

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions