Skip to content

incompatible type for trait E0053 with specialization #32519

Closed
@amarant

Description

@amarant

Hi,
When compiling this it results in :

error: method `mul` has an incompatible type for trait:expected associated type, found struct `Mat2f32` [[E0053](https://doc.rust-lang.org/error-index.html#E0053)]

I expected that to compile, because Mat2f32 is the associated type.
Instead it seems the new feature specialization interacts strangely with default or associated type.

Error:

 <anon>:54:5: 61:6 error: method `mul` has an incompatible type for trait:
  expected associated type,
     found struct `Mat2f32` [E0053]
 <anon>:54     default fn mul(self, rhs: T) -> Mat2f32 {
 <anon>:55         Mat2f32 {
 <anon>:56             e00: self.e00_rhs.e00() + self.e01_rhs.e10(),
 <anon>:57             e01: self.e00_rhs.e10() + self.e01_rhs.e11(),
 <anon>:58             e10: self.e10_rhs.e00() + self.e11_rhs.e10(),
 <anon>:59             e11: self.e10_rhs.e10() + self.e11_rhs.e11(),
           ...

Here is the code if play goes down:

#![feature(specialization)]

use std::ops::Mul;

struct Mat2f32 {
    e00: f32,
    e01: f32,
    e10: f32,
    e11: f32,
}

struct IdMatf32;

trait Mat2 : Sized {
    fn e00(&self) -> f32;
    fn e01(&self) -> f32;
    fn e10(&self) -> f32;
    fn e11(&self) -> f32;
}

impl Mat2 for Mat2f32 {
    fn e00(&self) -> f32 {
        self.e00
    }
    fn e01(&self) -> f32 {
        self.e01
    }
    fn e10(&self) -> f32 {
        self.e10
    }
    fn e11(&self) -> f32 {
        self.e11
    }
}


impl Mat2 for IdMatf32 {
    fn e00(&self) -> f32 {
        1f32
    }
    fn e01(&self) -> f32 {
        0f32
    }
    fn e10(&self) -> f32 {
        0f32
    }
    fn e11(&self) -> f32 {
        1f32
    }
}

impl<T: Mat2> Mul<T> for Mat2f32 {
    default type Output = Mat2f32;
    default fn mul(self, rhs: T) -> Mat2f32 {
        Mat2f32 {
            e00: self.e00*rhs.e00() + self.e01*rhs.e10(),
            e01: self.e00*rhs.e10() + self.e01*rhs.e11(),
            e10: self.e10*rhs.e00() + self.e11*rhs.e10(),
            e11: self.e10*rhs.e10() + self.e11*rhs.e11(),
        }
    }
}

impl Mul<IdMatf32> for Mat2f32 {
    type Output = Mat2f32;
    fn mul(self, _: IdMatf32) -> Mat2f32 {
        self
    }
}

fn main() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions