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

Associated types should permit arbitrary bounds #18178

Closed
carllerche opened this issue Oct 20, 2014 · 3 comments
Closed

Associated types should permit arbitrary bounds #18178

carllerche opened this issue Oct 20, 2014 · 3 comments

Comments

@carllerche
Copy link
Member

Based on my understanding of the RFC, associated types should be able to specify bounds, yet the following does not work:

#![feature(associated_types)]

pub trait Cancel {
    fn cancel(self);
}

pub trait Foo {
    type C: Cancel;

    fn stuff(self) -> <Self as Foo>::C;
}

pub fn main() {
}
@nikomatsakis nikomatsakis changed the title Associated types cannot have bounds Associated types should permit arbitrary bounds Oct 29, 2014
@tomjakubowski
Copy link
Contributor

I'm not sure what the deal with that snippet was when the issue was opened, but it compiles today. The compiler also correctly rejects an impl of Foo which specifies a Foo::C type that does not fulfill Cancel, but you can't use that Cancel bound outside the trait (not sure if the function doit is supposed to be accepted):

#![feature(associated_types)]

pub trait Cancel {
    fn cancel(self);
}

pub trait Foo {
    type C: Cancel;

    fn stuff(self) -> <Self as Foo>::C;
    fn trait_doit(self) { // this method typechecks
        let cancel_me = self.stuff();
        cancel_me.cancel();
    }
}

fn doit<F: Foo>(f: F) { // this function doesn't typecheck
    let cancel_me = f.stuff();
    cancel_me.cancel();
}

pub fn main() {
}
<anon>:18:21: 18:30 error: the trait `Cancel` is not implemented for the type `C`
<anon>:18     let cancel_me = f.stuff();
                              ^~~~~~~~~
<anon>:19:15: 19:23 error: type `C` does not implement any method in scope named `cancel`
<anon>:19     cancel_me.cancel();
                        ^~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101

@carllerche
Copy link
Member Author

I'm pretty sure the fix for this landed.

@tomjakubowski
Copy link
Contributor

I think my grievance is #18434

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

2 participants