-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Coercible and HasPrefix for Zero Cost Coercions #91
Conversation
vtable, and is considered a built-in "kind" alongside `Copy`, `Send`, etc. | ||
|
||
Where single inheritance and subtyping conflate many different ideas, among them transparenta | ||
ccess to superstruct fields, zero-cost conversion from sub- to supertypes, and these conversions |
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.
Typo ("transparenta ccess").
|
||
The trait is wired-in to the compiler, and user-defined impls of it are highly restricted as | ||
described in the implementation section talking about roles. `coerce()` would coerce between | ||
any two types where the target type "is a proper subtype of" the input type. Note that `coerce` |
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.
Why does it need to be a proper subtype? This would prevent coercing between two types that are considered equivalent. For example, between a newtype and the underlying uint
(which is to say, you could define the coercion in one direction, but not the other).
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.
Good point - I'm not sure why I kept the word proper in there. I'll remove it.
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 think my original intent here with "proper subtype" was just to emphasize the distinction from merely "has prefix". For "strictly fewer inhabitants" I would have said "strict subtype".
Huge thanks for taking this up. I'll try to leave a few comments. |
## Existing `transmute`s | ||
|
||
There are various calls to `transmute` scattered throughout the Rust codebase, and a | ||
number of them are perfectly safe. It would be beneficial to write these in a way that |
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 would hope that all of them are perfectly safe, because otherwise it's a bug! I think you just meant to say that the could compiler could verify their safety under this proposal. :)
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.
They aren't all safe, and it is a bug: rust-lang/rust#13933. I think there are others, but those are the most blatant.
FWIW if I were writing this proposal today, I would go with this naming scheme:
In other words, rename the existing This is more consistent with Rust's existing terminology which uses "transmute" to mean the thing we are doing here, and "coerce" to mean something different (not-necessarily-free compiler-inserted casts). It also happens to directly mirror GHC's (quite logical) scheme of I still don't have a better idea for what to call |
Ergonomics in general is an area we want to focus on leading up to 1.0, and coercions probably have a role. We're not ready to make any decisions on the subject yet. Closing, but tagging with 'postponed' so it can be referenced later. |
This is much more about expressiveness, safety and performance than about ergonomics. The name "coercible" was misleading in this context, because Rust uses it to mean something different (see previous comment). I agree that implementing this is not urgent, just wanted to clarify. |
So... I thought I made a discovery, inspired by an email from Edward Kmett, and then I re-read the RFC, and realized that it already does things exactly the way I was going to suggest, following my "discovery". The discovery was that we can completely avoid the need to have explicit role/variance annotations on type parameters (e.g.
But, as I mentioned above, the RFC already specifies exactly this:
So I commend @gereeter's brilliance for realizing this before anyone else. I think I was mislead by the earlier statement that:
which I unthinkingly assumed implied explicit (Which is a very good thing!) |
How does this RFC look through the eyes of 2017 Rust? |
WeakMap RFC
…experimental-hooks Promote buildInstrumentation to public API
This is largely based on (with lots of copied text) @glaebhoerl's proposal in rust-lang/rust#9912, but with a few changes:
rendered draft