Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic trait bound hides concrete type associated type #135254

Open
LHolten opened this issue Jan 8, 2025 · 0 comments
Open

Generic trait bound hides concrete type associated type #135254

LHolten opened this issue Jan 8, 2025 · 0 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@LHolten
Copy link
Contributor

LHolten commented Jan 8, 2025

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

@LHolten LHolten added the C-bug Category: This is a bug. label Jan 8, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 8, 2025
@LHolten LHolten changed the title Trait bound hides concrete type associated type Generic trait bound hides concrete type associated type Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants