rust-lang / rust-lang/rust

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

Open
#132,913 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-trait-system C-bug fixed-by-next-solver T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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: https://github.com/rust-lang/rust/issues/79629

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimized example in src/lib.rs or the linked Rust Playground, comparing typenum::Add1 with the working U9 alias. Read the related issue #79629 and trace how the compiler resolves the Add and Sum bounds. Done means identifying why the indirect alias loses the existing trait implementation and confirming the behavior is corrected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.