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

error: the trait Foo is not implemented for the type Box<Bar> #18179

Closed
tomaka opened this issue Oct 20, 2014 · 8 comments
Closed

error: the trait Foo is not implemented for the type Box<Bar> #18179

tomaka opened this issue Oct 20, 2014 · 8 comments

Comments

@tomaka
Copy link
Contributor

tomaka commented Oct 20, 2014

This code:

trait Foo {
    fn do_foo(&self);
}

trait Bar {
    fn do_bar(&self);
}

impl<T> Bar for T where T: Foo {
    fn do_bar(&self) {
        self.do_foo()
    }
}

struct Obj {
    obj: Box<Bar + 'static>
}

impl Obj {    
    fn process(&self) {
        self.obj.do_bar();
    }
}

(http://is.gd/hTCzJI)

Produces:

<anon>:21:9: 21:26 error: the trait `Foo` is not implemented for the type `Box<Bar>`
<anon>:21         self.obj.do_bar();

I think that this kind of code used to work in the past, so it could be a regression.
Even if this code is not supposed to work, the error message makes no sense.

@conradkleinespel
Copy link
Contributor

Hey @tomaka, thanks for taking the time to report this :-) I'm not sure how Box works, but could this be related to #18040 ?

@tomaka
Copy link
Contributor Author

tomaka commented Oct 20, 2014

The error message of #18040 is different from mine, and I'm confident that my code is actually supposed to work and not produce an error (contrary to #18040).

@conradkleinespel
Copy link
Contributor

Same with #18040, which is about a change in rustc as of 0.13.0.

May I ask what version of rustc you're compiling your code with ? If you're unsure, you can get it via rustc -v, I think.

@tomaka
Copy link
Contributor Author

tomaka commented Oct 20, 2014

I tried with today's nightly before reporting this. I've now installed an older nightly so I can't tell the exact version. This issue is already there in the 2014-10-16 and the 2014-10-10 nightlies, and the playpen.
Althought I'm not sure, I think that this code used to work with the 2014-10-06 nightlies.

What I mean by "I'm not sure" is that this issue is a simplification of a real code which used to work with 2014-10-06 (I'm sure about this), but no longer works with 2014-10-10, 2014-10-16, or 2014-10-19, and produces the same error message.

I suspect #17669 to be the cause.

@ftxqxd
Copy link
Contributor

ftxqxd commented Oct 21, 2014

I think this might have been caused by #17464. The code can easily be fixed by explicitly dereferencing the trait object:

trait Foo {
    fn do_foo(&self);
}

trait Bar {
    fn do_bar(&self);
}

impl<T> Bar for T where T: Foo {
    fn do_bar(&self) {
        self.do_foo()
    }
}

struct Obj {
    obj: Box<Bar + 'static>
}

impl Obj {    
    fn process(&self) {
        (*self.obj).do_bar();
    }
}

@nrc
Copy link
Member

nrc commented Oct 23, 2014

cc @nikomatsakis sounds like a multi-dispatch regression

@nikomatsakis
Copy link
Contributor

I agree with @P1start that this is likely caused by #17464. I don't think it's a multidispatch regression per se, though there is an interaction with the condtiional dispatch algorithm. In particular, the algorithm decides that it will use the trait method do_bar invoked on Box<Bar>, before it derefs to Bar and finds the object method. You might be surprised since you would expect that it would evaluate the T:Foo requirement before deciding on the impl, but in fact since there is only one potentially applicable impl, it forgoes evaluating the condition. This is an important part of the algorithm, since otherwise you wind up requiring a lot more type annotations to resolve methods.

My inclination is to close this as "working as expected".

@nikomatsakis
Copy link
Contributor

Closing.

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

5 participants