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

Trait impl detection fails when using associated types aliased to eachother #54375

Closed
Osspial opened this issue Sep 20, 2018 · 2 comments
Closed

Comments

@Osspial
Copy link

Osspial commented Sep 20, 2018

When attempting to compile the following code, the compiler mistakenly rejects it, complaining that the Point type returned by max doesn't implement Sub<Self::D::Vector>. However, Point does implement the trait - it's just that EuclideanSpace::Diff is being aliased through Dimensionality::Vector.

Playground link

use std::ops::*;

pub trait Geometry {
    type D: Dimensionality;

    fn min(&self) -> <Self::D as Dimensionality>::Point {
        self.max() - self.dims()
    }

    fn max(&self) -> <Self::D as Dimensionality>::Point;
    
    fn dims(&self) -> <Self::D as Dimensionality>::Vector;
}

pub trait Dimensionality {
    type Point: EuclideanSpace<Diff=Self::Vector>;
    type Vector;
}

pub trait EuclideanSpace:
    Copy + Clone +
    Add<<Self as EuclideanSpace>::Diff, Output = Self> +
    Sub<<Self as EuclideanSpace>::Diff, Output = Self>
{
    type Diff;
}

Redefining fn dims as follows so that it directly returns the requested type also fails to compile, with the same error:

Playground link

fn dims(&self) -> <<Self::D as Dimensionality>::Point as EuclideanSpace>::Diff;

It seems the only way to make that code compile is to redefine Dimensionality as follows, removing the alias between EuclideanSpace::Diff and Dimensionality::Vector:

Playground link

pub trait Dimensionality {
    type Point: EuclideanSpace;
    type Vector;
}

This is happening on all versions of the compiler I've tested, one of those being stable 1.29.0.

@rkarp
Copy link
Contributor

rkarp commented Oct 7, 2018

This is another duplicate of #24159, which I know all too well by now. Here's how to fix your first example:

Playground Link

@jonas-schievink
Copy link
Contributor

Closing as a duplicate of #24159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants