Open
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