Skip to content

simple type inference fails depending on order of trait bounds #54121

Closed

Description

I can't be sure that this behavior is unintended, but it definitely seems very strange and undesirable to me.

This code works fine:

$ cat trash2.rs
trait Tr<A, B> {
    fn exec(a: A, b: B);
}

trait Q {
    type T: Tr<u16, u16> + Tr<u8, u8>;
}

#[allow(dead_code)]
fn f<S: Q>()
{
    <S as Q>::T::exec(0u8, 0u8)
}

fn main() {
}
$ rustc trash2.rs -o trash

But change the order of the trait bounds in the associated type T, and it fails:

$ cat trash2.rs
trait Tr<A, B> {
    fn exec(a: A, b: B);
}

trait Q {
    type T: Tr<u8, u8> + Tr<u16, u16>;
}

#[allow(dead_code)]
fn f<S: Q>()
{
    <S as Q>::T::exec(0u8, 0u8)
}

fn main() {
}
$ rustc trash2.rs -o trash
error[E0308]: mismatched types
  --> trash2.rs:12:23
   |
12 |     <S as Q>::T::exec(0u8, 0u8)
   |                       ^^^ expected u16, found u8
help: you can cast an `u8` to `u16`, which will zero-extend the source value
   |
12 |     <S as Q>::T::exec(0u8.into(), 0u8)
   |                       ^^^^^^^^^^

error[E0308]: mismatched types
  --> trash2.rs:12:28
   |
12 |     <S as Q>::T::exec(0u8, 0u8)
   |                            ^^^ expected u16, found u8
help: you can cast an `u8` to `u16`, which will zero-extend the source value
   |
12 |     <S as Q>::T::exec(0u8, 0u8.into())
   |                            ^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceA-traitsArea: Trait systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler 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