diff options
| author | bors <bors@rust-lang.org> | 2018-11-03 02:37:29 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-11-03 02:37:29 +0000 |
| commit | 3fc70e8d4686190891b575204326c9b9dcd2d3ed (patch) | |
| tree | 3780984da4d2269cfcf6a71ddc54abe478fde944 /src/libcore | |
| parent | 8b096314a6381e28834ddb1e662ce90e5b32736b (diff) | |
| parent | 192e7c4b51ab35f4cfbbacde9eeffa8e12dba873 (diff) | |
| download | rust-3fc70e8d4686190891b575204326c9b9dcd2d3ed.tar.gz rust-3fc70e8d4686190891b575204326c9b9dcd2d3ed.zip | |
Auto merge of #54383 - mikeyhew:custom-receivers-object-safety, r=nikomatsakis
Take 2: Implement object-safety and dynamic dispatch for arbitrary_self_types
This replaces #50173. Over the months that that PR was open, we made a lot of changes to the way this was going to be implemented, and the long, meandering comment thread and commit history would have been confusing to people reading it in the future. So I decided to package everything up with new, straighforward commits and open a new PR.
Here are the main points. Please read the commit messages for details.
- To simplify codegen, we only support receivers that have the ABI of a pointer. That means they are builtin pointer types, or newtypes thereof.
- We introduce a new trait: `DispatchFromDyn<T>`, similar to `CoerceUnsized<T>`. `DispatchFromDyn` has extra requirements that `CoerceUnsized` does not: when you implement `DispatchFromDyn` for a struct, there cannot be any extra fields besides the field being coerced and `PhantomData` fields. This ensures that the struct's ABI is the same as a pointer.
- For a method's receiver (e.g. `self: Rc<Self>`) to be object-safe, it needs to have the following property:
- let `DynReceiver` be the receiver when `Self = dyn Trait`
- let `ConcreteReceiver` be the receiver when `Self = T`, where `T` is some unknown `Sized` type that implements `Trait`, and is the erased type of the trait object.
- `ConcreteReceiver` must implement `DispatchFromDyn<DynReceiver>`
In the case of `Rc<Self>`, this requires `Rc<T>: DispatchFromDyn<Rc<dyn Trait>>`
These rules are explained more thoroughly in the doc comment on `receiver_is_dispatchable` in object_safety.rs.
r? @nikomatsakis and @eddyb
cc @arielb1 @cramertj @withoutboats
Special thanks to @nikomatsakis for getting me un-stuck when implementing the object-safety checks, and @eddyb for helping with the codegen parts.
EDIT 2018-11-01: updated because CoerceSized has been replaced with DispatchFromDyn
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/nonzero.rs | 4 | ||||
| -rw-r--r-- | src/libcore/ops/mod.rs | 3 | ||||
| -rw-r--r-- | src/libcore/ops/unsize.rs | 36 | ||||
| -rw-r--r-- | src/libcore/pin.rs | 8 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 8 |
5 files changed, 55 insertions, 4 deletions
diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 118e75e1ee7..436cd1fc057 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -10,7 +10,7 @@ //! Exposes the NonZero lang item which provides optimization hints. -use ops::CoerceUnsized; +use ops::{CoerceUnsized, DispatchFromDyn}; /// A wrapper type for raw pointers and integers that will never be /// NULL or 0 that might allow certain optimizations. @@ -20,3 +20,5 @@ use ops::CoerceUnsized; pub(crate) struct NonZero<T>(pub(crate) T); impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {} + +impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<NonZero<U>> for NonZero<T> {} diff --git a/src/libcore/ops/mod.rs b/src/libcore/ops/mod.rs index ce4f45762de..edfa6df11ac 100644 --- a/src/libcore/ops/mod.rs +++ b/src/libcore/ops/mod.rs @@ -201,3 +201,6 @@ pub use self::generator::{Generator, GeneratorState}; #[unstable(feature = "coerce_unsized", issue = "27732")] pub use self::unsize::CoerceUnsized; + +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +pub use self::unsize::DispatchFromDyn; diff --git a/src/libcore/ops/unsize.rs b/src/libcore/ops/unsize.rs index da72f374842..4d9a40a1b90 100644 --- a/src/libcore/ops/unsize.rs +++ b/src/libcore/ops/unsize.rs @@ -43,7 +43,7 @@ use marker::Unsize; /// [nomicon-coerce]: ../../nomicon/coercions.html #[unstable(feature = "coerce_unsized", issue = "27732")] #[lang = "coerce_unsized"] -pub trait CoerceUnsized<T> { +pub trait CoerceUnsized<T: ?Sized> { // Empty. } @@ -77,3 +77,37 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {} // *const T -> *const U #[unstable(feature = "coerce_unsized", issue = "27732")] impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {} + + +/// This is used for object safety, to check that a method's receiver type can be dispatched on. +/// +/// example impl: +/// +/// ``` +/// # #![feature(dispatch_from_dyn, unsize)] +/// # use std::{ops::DispatchFromDyn, marker::Unsize}; +/// # struct Rc<T: ?Sized>(::std::rc::Rc<T>); +/// impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T> +/// where +/// T: Unsize<U>, +/// {} +/// ``` +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +#[cfg_attr(not(stage0), lang = "dispatch_from_dyn")] +pub trait DispatchFromDyn<T> { + // Empty. +} + +// &T -> &U +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {} +// &mut T -> &mut U +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {} +// *const T -> *const U +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {} +// *mut T -> *mut U +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {} + diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index a03c080fb3f..68de82d2945 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -91,7 +91,7 @@ use fmt; use marker::Sized; -use ops::{Deref, DerefMut, CoerceUnsized}; +use ops::{Deref, DerefMut, CoerceUnsized, DispatchFromDyn}; #[doc(inline)] pub use marker::Unpin; @@ -325,4 +325,10 @@ where {} #[unstable(feature = "pin", issue = "49150")] +impl<'a, P, U> DispatchFromDyn<Pin<U>> for Pin<P> +where + P: DispatchFromDyn<U>, +{} + +#[unstable(feature = "pin", issue = "49150")] impl<P> Unpin for Pin<P> {} diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 36852b10fac..62ccf6c865c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -75,7 +75,7 @@ use convert::From; use intrinsics; -use ops::CoerceUnsized; +use ops::{CoerceUnsized, DispatchFromDyn}; use fmt; use hash; use marker::{PhantomData, Unsize}; @@ -2796,6 +2796,9 @@ impl<T: ?Sized> Copy for Unique<T> { } impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> { } #[unstable(feature = "ptr_internals", issue = "0")] +impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> { } + +#[unstable(feature = "ptr_internals", issue = "0")] impl<T: ?Sized> fmt::Pointer for Unique<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&self.as_ptr(), f) @@ -2951,6 +2954,9 @@ impl<T: ?Sized> Copy for NonNull<T> { } #[unstable(feature = "coerce_unsized", issue = "27732")] impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { } +#[unstable(feature = "dispatch_from_dyn", issue = "0")] +impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> { } + #[stable(feature = "nonnull", since = "1.25.0")] impl<T: ?Sized> fmt::Debug for NonNull<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
