Skip to content

Type inference seems to pick up the wrong trait bound #77625

Closed as duplicate

Description

@orium

Hi. I apologize if this is a dup (I tried to search, but is it not easy to distill this into words to search for). If it's not a dup please update the title to something more meaningful.

When trying to compile (in play)

pub trait Distribution<T> {}

impl Distribution<u16> for () {}

fn gen<K>() -> K
where
    (): Distribution<K> {
    todo!()
}

fn foo<T>() -> u16
where
    (): Distribution<T>,
{
    gen()
}

I get the error:

error[E0308]: mismatched types
  --> src/lib.rs:15:5
   |
11 | fn foo<T>() -> u16
   |        -       --- expected `u16` because of return type
   |        |
   |        this type parameter
...
15 |     gen()
   |     ^^^^^ expected `u16`, found type parameter `T`
   |
   = note:        expected type `u16`
           found type parameter `T`

It it should be able to determine that the hole in gen::<_>() is u16 because that's the return type of foo(). Instead the compiler thinks the hole must be T, which is not true (in fact, if you change the call to gen() to gen::<u16>() the code compiles).

This happens in stable (1.46.0) as well as nightly 1.49.0-nightly (2020-10-05 a1dfd24).

Activity

  1. jonas-schievink commented on Oct 6, 2020

    @jonas-schievink
    Contributor

    Presumably this is just fallout from the fact that predicates in where-clauses having higher precedence than impls

  2. jonas-schievink commented on Oct 6, 2020

    @jonas-schievink
    Contributor

    Meaning: In foo, at the call to gen(), the compiler has to prove the (): Distribution<K> predicate in the where-clause on gen, which means proving (): Distribution<$0> where $0 is an inference variable. It eagerly selects the (): Distribution<T> predicate from the where-clause of foo to do that and thus unifies $0 with T.

  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Apr 5, 2023
  4. lcnr commented on Aug 1, 2025

    @lcnr
    Contributor

    Closing as duplicate of #24066

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions