You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't cast self to trait object in default methods #7320
<anon>:2:24: 2:29 error: mismatched types: expected `@Foo` but found `@Self` (expected trait Foo but found @-ptr)
<anon>:2 fn foo(@self) { bar(self); }
^~~~~
error: aborting due to previous error
@Blei might be related, but I would not call it a duplicate; the type &S in fn<S:Trait>(s:&S) does not use dynamic dispatch (i.e. does not carry a virtual-method table) while @Trait in fn(x:@Trait) does.
(as in, the use-cases for the two are so different, I'd want to ensure they are addressed, independently if necessary. Though maybe we will be lucky and they will end up having the same root cause.)
../rust-things/default-self-object.rs:2:24: 2:36 error: cannot pack type `@Self`, which does not fulfill `'static`, as a trait bounded by 'static
../rust-things/default-self-object.rs:2 fn foo(@self) { bar(self as @Foo); }
Sully's error is legit, in the sense that the type system is operating as expected. The type Self may contain borrowed pointers, and the type @Foo is short for @Foo:'static, which says that the value does NOT contain borrowed pointers. The correct way to implement the example (which I think does not work yet) would be:
trait Foo : 'static { ... }
which would say that the trait Foo cannot be implemented except by types that do not contain borrowed pointers.
If we added the rule that @ boxes cannot contain managed pointers, as we have discussed, it is plausible that we could then extend the type system to know that Self has the bound 'static in an @self method (or any method that contains an @Self parameter). I described such a scheme in this blog post about the Sized kind.
@nikomatsakis FYI, when I attempted this morning to replicate the issues I was having with syntax::visit @Visitor refactoring, I ran into this problem (I think). Or at least, I could not pass @self to helper methods from the default method implementations in visit.rs. (There are ways to work-around this, such as not using default methods.)
test.rs:2:25: 2:32 error: cannot pack type `~Self`, which does not fulfill `Send`, as a trait bounded by Send
test.rs:2 fn foo(~self) { bar(self as ~Foo); }
^~~~~~~
error: aborting due to previous error
We no longer have the default bounds on ~ trait objects, but, in any case, the original code has worked for a while using ~Foo: to explicitly override the Send bound, i.e. this issue had been fixed. (The old ~Foo/new ~Foo:Send cast fails because Self isn't known to be Send in that context; if sendability is necessary, writing trait Foo: Send { ... } works).
Activity
Blei commentedon Jun 26, 2013
Related to (or duplicate of?) #7183
pnkfelix commentedon Jun 26, 2013
@Blei might be related, but I would not call it a duplicate; the type
&Sinfn<S:Trait>(s:&S)does not use dynamic dispatch (i.e. does not carry a virtual-method table) while@Traitinfn(x:@Trait)does.(as in, the use-cases for the two are so different, I'd want to ensure they are addressed, independently if necessary. Though maybe we will be lucky and they will end up having the same root cause.)
pnkfelix commentedon Jun 26, 2013
Regardless of whether this is a dupe of 7183, it is definitely part of #2794.
msullivan commentedon Jun 28, 2013
The code I think is not correct. It doesn't actually do a cast.
msullivan commentedon Jun 28, 2013
I feel like the code should be
This fails with
Hm.
Blei commentedon Jun 28, 2013
Yes, I tried multiple variants, both with and without cast. I meant to paste the one with a cast, but alas.
nikomatsakis commentedon Jun 28, 2013
Sully's error is legit, in the sense that the type system is operating as expected. The type
Selfmay contain borrowed pointers, and the type@Foois short for@Foo:'static, which says that the value does NOT contain borrowed pointers. The correct way to implement the example (which I think does not work yet) would be:which would say that the trait
Foocannot be implemented except by types that do not contain borrowed pointers.If we added the rule that
@boxes cannot contain managed pointers, as we have discussed, it is plausible that we could then extend the type system to know thatSelfhas the bound'staticin an@selfmethod (or any method that contains an@Selfparameter). I described such a scheme in this blog post about theSizedkind.pnkfelix commentedon Aug 12, 2013
@nikomatsakis FYI, when I attempted this morning to replicate the issues I was having with syntax::visit
@Visitorrefactoring, I ran into this problem (I think). Or at least, I could not pass@selfto helper methods from the default method implementations invisit.rs. (There are ways to work-around this, such as not using default methods.)flaper87 commentedon Jan 29, 2014
The following example works:
Since
@is being deprecated in favor ofGcandRcand the above example works, I think we can close this issue. @nikomatsakis @pnkfelix thoughts?huonw commentedon Jan 29, 2014
Does
~selfand~Foowork?flaper87 commentedon Jan 29, 2014
Yes, it works!
flaper87 commentedon Jan 29, 2014
Closing based on the above comments
flaper87 commentedon Jan 29, 2014
Reopening since the above example requires
Sendto be explicitly specifiedflaper87 commentedon Jan 29, 2014
To be more specific. The same thing that happens here can be reproduced w/
~Foo:huonw commentedon Apr 26, 2014
We no longer have the default bounds on
~trait objects, but, in any case, the original code has worked for a while using~Foo:to explicitly override theSendbound, i.e. this issue had been fixed. (The old~Foo/new~Foo:Sendcast fails becauseSelfisn't known to beSendin that context; if sendability is necessary, writingtrait Foo: Send { ... }works).Flagging as needstest.
test: Add tests for closed issues
auto merge of #13811 : alexcrichton/rust/closed-issues, r=sfackler
update: ```Sugg::not()``` replacing the comparison operator. rust-lan…
Auto merge of rust-lang#8117 - hotate29:issue7320, r=camsteffen
Merge rust-lang#7320