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 parameters can be constrained only as an associated type #26262

Closed
arielb1 opened this issue Jun 12, 2015 · 3 comments
Closed

Type parameters can be constrained only as an associated type #26262

arielb1 opened this issue Jun 12, 2015 · 3 comments
Labels
A-associated-items Area: Associated items such as associated types and consts.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Jun 12, 2015

You can create an inherent impl where the impl substs are not constrained by the impl signature. If the parameters are constrained by the method signature, you can even get code that usefully runs (otherwise you get "can't resolve" errors):

struct S<T>(T);

trait Tr { type Assoc; fn test(); }

impl Tr for u32 {
    type Assoc = u32;
    fn test() { println!("test for u32!"); }
}

impl Tr for u64 {
    type Assoc = u32;
    fn test() { println!("test for u64!"); }
}

impl<T: Tr> S<T::Assoc> {
    fn foo(self, _: T) {
        T::test();
    }
}

fn main() {
    <S<u32>>::foo(S(4u32),4u32);
}

Note that you need to use UFCS notation to avoid #25679 (which is an issue with method lookup).

RFC447 is silent with regards to this issue, but this allows you to create an impl that is not resolvable by its trait-ref.

@arielb1
Copy link
Contributor Author

arielb1 commented Jun 12, 2015

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

Heh, I was just telling @aturon in IRC the other day that this case was broken. I agree, the current logic is too simplistic (it's also something I've been writing up in this RFC I keep talking about...). Basically, it currently considers all input/type parameters which appear as constrianed, but it ought to stop at projections. I made this gist, which is a similar example: https://gist.github.com/0c562768aeb9412269ce

@nikomatsakis
Copy link
Contributor

To be clear, I don't think that fixing this issue requires an RFC (it's just a bug, I think). I just meant that I was writing up various text that referenced the notion of "constrained type parameters" and tried to give a definition, which is how I realized this problem.

@steveklabnik steveklabnik added the A-associated-items Area: Associated items such as associated types and consts. label Jun 15, 2015
bors added a commit that referenced this issue Jun 15, 2015
Fixes #26262

Because this rejects code that previously compiled, this is a [breaking-change]

r? @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts.
Projects
None yet
Development

No branches or pull requests

3 participants