Skip to content

Type-inference breaks depending on the order of arguments in a function call #39181

Open
@gnzlbg

Description

@gnzlbg

Given (playground link):

pub trait Int { fn zero() -> Self; }

impl Int for u8 { fn zero() -> u8 { 0 } }
impl Int for i8 { fn zero() -> i8 { 0 } }

pub trait FromSigned<T> {
    fn from_signed(T) -> Self;
}

impl<T: Int, U: Int> FromSigned<T> for U {
    fn from_signed(x: T) -> U {
        U::zero()
    }
}

pub fn from_signed<To, Fr>(x: Fr) -> To
    where Fr: Int,
          To: FromSigned<Fr>
{
    To::from_signed(x)
}

I ran into the following:

fn main() {
    // This type-checks:
    255u8 == from_signed(-1i8); 
    // This doesn't type checkL
    from_signed(-1i8) == 255u8; 
}

In particular, my intuition says that a == b desugars into equals(a, b). That equals(a, b) type-checks but equals(b, a) doesn't is extremely weird, in particular since equals expects both arguments to be of the same type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions