rust-lang / rust-lang/rust

Compiler error when using a const generic alias in an associated type bound in a super trait

Open
#149,420 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-const-eval 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

This codes fails to compile:

trait Iter {
    type Item;
}

trait TryIter: Iter<Item = Self::ResItem> {
    type ResItem;
}

const N: usize = 4;

trait Dyn: TryIter<ResItem = [u8; N]> {}

with this error:

error[E0284]: type annotations needed
  --> src/main.rs:12:1
   |
12 | trait Dyn: TryIter<ResItem = [u8; N]> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `<Self as main::TryIter>::ResItem == _`

error[E0284]: type annotations needed
  --> src/main.rs:12:12
   |
12 | trait Dyn: TryIter<ResItem = [u8; N]> {}
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `<Self as main::TryIter>::ResItem == _`

For more information about this error, try `rustc --explain E0284`.

However, replacing N with 4 in the last line compiles fine.

Meta

This is on rustc 1.91.1 (ed61e7d7e 2025-11-07)

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

The reproducer is the Rust snippet in the issue body; compile it with rustc 1.91.1, then compare [u8; N] with [u8; 4] while tracing associated-type and const-generic inference. Done means the const-generic form compiles like the literal form and the behavior is covered by a regression test.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.