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

Type inference of binary operators' operand is still not perfect #22001

Closed
edwardw opened this Issue Feb 6, 2015 · 3 comments

Comments

Projects
None yet
5 participants
@edwardw
Copy link
Contributor

edwardw commented Feb 6, 2015

#21817 tried to solve a type inference quirk for the binary operators. But as @Byron reported, it is still not perfect:

use std::num::Float;

#[derive(Debug, PartialEq, Eq, Copy)]
pub struct Vector<T: Float> {
    x: T,
    y: T,
    z: T,
}

impl<T: Float> Vector<T> {
    #[inline(always)]
    fn mulfed(&self, m: T) -> Vector<T> {
        Vector { x: self.x * m, y: self.y * m, z: self.z * m }
    }

    fn dot(&self, r: &Vector<T>) -> T {
        self.x * r.x + self.y * r.y + self.z * r.z
    }

    fn len(&self) -> T {
        self.dot(self).sqrt()
    }

    fn normalized(&self) -> Vector<T> {
        self.mulfed(Float::one() / self.dot(self).sqrt())
    }

    fn normalized_(&self) -> Vector<T> {
        self.mulfed(Float::one() / self.len())
    }
}

fn main() {}

Notice that there are two versions of normalized, one can compile whereas the other can't. This is a bug.

@edwardw edwardw changed the title Type inference of binary operators Type inference of binary operators' operand is still not perfect Feb 6, 2015

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Feb 8, 2015

Do you know if #22099 might be a regression?

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Mar 4, 2016

Triage: std::num::Float doesn't exist any more. I'm not sure of the best way to reproduce this. Using the num crate, I get

src/lib.rs:27:21: 27:31 error: the trait `num::traits::Float` cannot be made into an object [E0038]
src/lib.rs:27         self.mulfed(Float::one() / self.dot(self).sqrt())
                                  ^~~~~~~~~~

and it doesn't look like one is implemented for Float.

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented May 13, 2017

Closing since we can't reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.