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
Closed

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

edwardw opened this issue Feb 6, 2015 · 3 comments
Labels
A-type-system Area: Type system

Comments

@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
@kmcallister kmcallister added the A-type-system Area: Type system label Feb 6, 2015
@bluss
Copy link
Member

bluss commented Feb 8, 2015

Do you know if #22099 might be a regression?

@steveklabnik
Copy link
Member

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
Copy link
Member

Closing since we can't reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

5 participants