Compiler fails to see an existing trait implementation for an indirectly referred type #132913
Open
Description
opened on Nov 11, 2024
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