-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 upcasting #60900
Trait upcasting #60900
Conversation
@eddyb This isn't complete yet, but an initial review would be appreciated. Also, if you could elaborate slightly on what remains, that would be helpful. (At the moment I don't think upcast objects get the right vtable pointer still, i.e. it just points to the start of the overall vtable, not the "subtable".) |
This comment has been minimized.
This comment has been minimized.
src/librustc/ty/sty.rs
Outdated
/// | ||
/// A Rust trait object type consists (in addition to a lifetime bound) | ||
/// of a set of trait bounds, which are separated into any number | ||
/// of auto-trait bounds, and at most 1 non-auto-trait bound. The | ||
/// of auto-trait bounds, and at most one non-auto-trait bound. The | ||
/// non-auto-trait bound is called the "principal" of the trait |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we had done away with "principal"? (don't change it here... just... we should fix this sometime?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we kind of did, in my other PR (not yet merged), but it turns out there's a few more things. Easy to remove though.
Do you have some links to the related discussion, issues, RFCs? Am I right in assuming that the current unfinished implementation will allow casting |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I presume you meant Anyway, for background, @nikomatsakis and I have discussed this feature informally for some time now, and we had a conversation on Zulip a few weeks ago, which @eddyb also participated in – I believe Eddy tagged you in a question right at the end and you replied, so you might have seen some of it? He explains the vtable layout in any case (which I may have gotten wrong in this PR, but hopefully not). I believe the plan of action is to review & merge this PR under "experimentation in master", but write up an RFC simultaneously, if possible. I forget if anyone already volunteered for that (@Centril)? |
Yep; just need to find the time somehow =) |
I am not talking about multi-trait object, but about traits with multiple parents: trait A {
fn a(&self) {}
}
trait B {
fn b(&self) {}
}
trait Foo: A + B {}
impl A for () {}
impl B for () {}
impl Foo for () {}
fn main() {
let x: &Foo = &();
let y: &A = x;
} Right now this will be unsound with the implementation at hand.
Ah yes I remember, I mostly thought we were talking about deduplicating the vtable generation :D
Ok, can you open a tracking issue with some info about what's being done so PRs can reference it and we have a sort of central point to talk about things and collect links? |
src/librustc/traits/select.rs
Outdated
|
||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So basically right now all that needs doing is put a feature gate check around this code instead of uncommenting? (in order to get upcasting to parent traits if there is only a single parent trait?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, basically. I'll want to feature gate data_a.principal_def_id() == data_b.principal_def_id()
in assemble_candidates_for_unsizing
too, but that's probably about it.
That sounds good; the current discussion has been centered in https://rust-lang.zulipchat.com/#narrow/stream/144729-wg-traits/topic/object.20upcasting. |
This comment has been minimized.
This comment has been minimized.
I'm probably missing something obvious... could you elaborate?
Hah, yep, that was just a natural side-topic stemming from eddyb and I chatting about how to go about implementing this feature. :-)
Will do shortly. |
The code you commented out does this relating. I presume the result of the equality check not ending up in the |
This comment has been minimized.
This comment has been minimized.
Right, but shouldn't the subsequent bit beginning Edit: maybe it's as simple as keeping that code and changing |
The vtable of
The vtable of
The vtable of
or implementation defined as
(note the order of So you can only ever upcast to either The vtable of
The vtable of
The vtable of
or implementation defined as
(note the order of So you can only ever upcast to either In the zulip thread there was some discussion about doing something like
for Though the memory regression is probably very small, it may still have a bad impact on embedded devs. |
Oh, I thought that's what I did implement already... I'm pretty sure it is, in fact. Can you explain why it isn't? |
Ah, you're right. I misread the loop in I'm not sure how to measure the impact of this. This will have some effect on binary sizes I presume (since all vtables of traits with multiple parent traits will now be bigger by 24 bytes per parent trait beyond the first). |
@oli-obk Yeah... I think we (@nikomatsakis, @eddyb, myself) just implicitly discounted that as not so important during our conversation. If it's really a concern, maybe there's some sort of crater-like test we can run? I do think it's a very reasonable and small "hit" to take in any case. |
I think it would be fine to just check by how much libstd and maybe some other large crates like |
Sounds reasonable. |
This comment has been minimized.
This comment has been minimized.
Ping from Triage: @alexreg Hi, please resolve the conflicts. Thanks |
@joelpalmer Yep, resolve them locally... going to factor out some of these changes to a separate PR soon. |
Closing as "too big, needs to be broken up". |
Did this ever go anywhere after this PR was closed? I have a use-case for this and I'm wondering if there has been any progress on trait upcasting, even on nightly. |
I'm interested in reviving this - which parts would need to be split out? |
…omatsakis Trait upcasting coercion (part1) This revives the first part of earlier PR rust-lang#60900 . It's not very clear to me which parts of that pr was design decisions, so i decide to cut it into pieces and land them incrementally. This allows more eyes on the details. This is the first part, it adds feature gates, adds feature gates tests, and implemented the unsize conversion part. (I hope i have dealt with the `ExistentialTraitRef` values correctly...) The next part will be implementing the pointer casting.
Allows casting objects of type
dyn Foo
todyn Bar
wheneverFoo: Bar
.r? @eddyb
CC @nikomatsakis