Generic trait bound hides concrete type associated type
Open
Nobody has claimed this yet.
A-trait-system
C-bug
T-types
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
trait Tr<'x> {
type Out;
fn build() -> Self::Out;
}
struct Foo;
impl<'x> Tr<'x> for Foo {
type Out = i32;
fn build() -> Self::Out {
42
}
}
fn test<'x>() -> i32
where
Foo: Tr<'x>, // commenting out this line fixes the error
{
Foo::build()
}
I expect this to compile, but it gives the following error:
error[E0308]: mismatched types
--> src/lib.rs:19:5
|
15 | fn test<'x>() -> i32
| --- expected `i32` because of return type
...
19 | Foo::build()
| ^^^^^^^^^^^^ expected `i32`, found associated type
|
= note: expected type `i32`
found associated type `<Foo as Tr<'_>>::Out`
= help: consider constraining the associated type `<Foo as Tr<'_>>::Out` to `i32`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
The code also works if the trait is made non-generic.
Meta
rustc --version --verbose:
rustc 1.86.0-nightly (ad211ced8 2025-01-07)
binary: rustc
commit-hash: ad211ced81509462cdfe4c29ed10f97279a0acae
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6
same result on stable
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproduction in src/lib.rs and compile it with rustc, comparing the generic-trait-bound case with the non-generic version. Trace how the compiler resolves Foo::build() and its associated type; done means the reported example compiles as expected without an explicit associated-type constraint 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
- Clearly specified
- Newbie friendliness
- 35/100