Skip to content

Commit f12c250

Browse files
committed
Replace CoerceSized trait with DispatchFromDyn
Rename `CoerceSized` to `DispatchFromDyn`, and reverse the direction so that, for example, you write ``` impl<T: Unsize<U>, U> DispatchFromDyn<*const U> for *const T {} ``` instead of ``` impl<T: Unsize<U>, U> DispatchFromDyn<*const T> for *const U {} ``` this way the trait is really just a subset of `CoerceUnsized`. The checks in object_safety.rs are updated for the new trait, and some documentation and method names in there are updated for the new trait name — e.g. `receiver_is_coercible` is now called `receiver_is_dispatchable`. Since the trait now works in the opposite direction, some code had to updated here for that too. I did not update the error messages for invalid `CoerceSized` (now `DispatchFromDyn`) implementations, except to find/replace `CoerceSized` with `DispatchFromDyn`. Will ask for suggestions in the PR thread.
1 parent c29641e commit f12c250

17 files changed

+164
-190
lines changed

src/liballoc/boxed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use core::iter::FusedIterator;
7777
use core::marker::{Unpin, Unsize};
7878
use core::mem;
7979
use core::pin::Pin;
80-
use core::ops::{CoerceUnsized, CoerceSized, Deref, DerefMut, Generator, GeneratorState};
80+
use core::ops::{CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Generator, GeneratorState};
8181
use core::ptr::{self, NonNull, Unique};
8282
use core::task::{LocalWaker, Poll};
8383

@@ -696,8 +696,8 @@ impl<'a, A, R> FnOnce<A> for Box<dyn FnBox<A, Output = R> + Send + 'a> {
696696
#[unstable(feature = "coerce_unsized", issue = "27732")]
697697
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
698698

699-
#[unstable(feature = "coerce_sized", issue = "0")]
700-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Box<T>> for Box<U> {}
699+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
700+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
701701

702702
#[stable(feature = "box_slice_clone", since = "1.3.0")]
703703
impl<T: Clone> Clone for Box<[T]> {

src/liballoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#![feature(box_syntax)]
8787
#![feature(cfg_target_has_atomic)]
8888
#![feature(coerce_unsized)]
89-
#![feature(coerce_sized)]
89+
#![feature(dispatch_from_dyn)]
9090
#![feature(core_intrinsics)]
9191
#![feature(custom_attribute)]
9292
#![feature(dropck_eyepatch)]

src/liballoc/rc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ use core::marker;
255255
use core::marker::{Unpin, Unsize, PhantomData};
256256
use core::mem::{self, align_of_val, forget, size_of_val};
257257
use core::ops::Deref;
258-
use core::ops::{CoerceUnsized, CoerceSized};
258+
use core::ops::{CoerceUnsized, DispatchFromDyn};
259259
use core::pin::Pin;
260260
use core::ptr::{self, NonNull};
261261
use core::convert::From;
@@ -297,8 +297,8 @@ impl<T: ?Sized> !marker::Sync for Rc<T> {}
297297
#[unstable(feature = "coerce_unsized", issue = "27732")]
298298
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
299299

300-
#[unstable(feature = "coerce_sized", issue = "0")]
301-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Rc<T>> for Rc<U> {}
300+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
301+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T> {}
302302

303303
impl<T> Rc<T> {
304304
/// Constructs a new `Rc<T>`.
@@ -1179,8 +1179,8 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
11791179
#[unstable(feature = "coerce_unsized", issue = "27732")]
11801180
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
11811181

1182-
#[unstable(feature = "coerce_sized", issue = "0")]
1183-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Weak<T>> for Weak<U> {}
1182+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
1183+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
11841184

11851185
impl<T> Weak<T> {
11861186
/// Constructs a new `Weak<T>`, without allocating any memory.

src/liballoc/sync.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::cmp::Ordering;
2525
use core::intrinsics::abort;
2626
use core::mem::{self, align_of_val, size_of_val};
2727
use core::ops::Deref;
28-
use core::ops::{CoerceUnsized, CoerceSized};
28+
use core::ops::{CoerceUnsized, DispatchFromDyn};
2929
use core::pin::Pin;
3030
use core::ptr::{self, NonNull};
3131
use core::marker::{Unpin, Unsize, PhantomData};
@@ -214,8 +214,8 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
214214
#[unstable(feature = "coerce_unsized", issue = "27732")]
215215
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
216216

217-
#[unstable(feature = "coerce_sized", issue = "0")]
218-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Arc<T>> for Arc<U> {}
217+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
218+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {}
219219

220220
/// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
221221
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
@@ -257,8 +257,8 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Weak<T> {}
257257

258258
#[unstable(feature = "coerce_unsized", issue = "27732")]
259259
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
260-
#[unstable(feature = "coerce_sized", issue = "0")]
261-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Weak<T>> for Weak<U> {}
260+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
261+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
262262

263263
#[stable(feature = "arc_weak", since = "1.4.0")]
264264
impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {

src/libcore/nonzero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Exposes the NonZero lang item which provides optimization hints.
1212
13-
use ops::{CoerceUnsized, CoerceSized};
13+
use ops::{CoerceUnsized, DispatchFromDyn};
1414

1515
/// A wrapper type for raw pointers and integers that will never be
1616
/// NULL or 0 that might allow certain optimizations.
@@ -21,4 +21,4 @@ pub(crate) struct NonZero<T>(pub(crate) T);
2121

2222
impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {}
2323

24-
impl<T: CoerceUnsized<U>, U: CoerceSized<T>> CoerceSized<NonZero<T>> for NonZero<U> {}
24+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<NonZero<U>> for NonZero<T> {}

src/libcore/ops/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,5 @@ pub use self::generator::{Generator, GeneratorState};
202202
#[unstable(feature = "coerce_unsized", issue = "27732")]
203203
pub use self::unsize::CoerceUnsized;
204204

205-
#[unstable(feature = "coerce_sized", issue = "0")]
206-
pub use self::unsize::CoerceSized;
205+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
206+
pub use self::unsize::DispatchFromDyn;

src/libcore/ops/unsize.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,32 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
7979
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
8080

8181

82-
/// Pointers to unsized types that can be coerced to a pointer to a sized type,
83-
/// as long as pointee is actually a value of that sized type. This is used for
84-
/// object safety, to check that a method's receiver type can be coerced from the version
85-
/// where `Self = dyn Trait` to the version where `Self = T`, the erased, sized type
86-
/// of the underlying object.
82+
/// This is used for object safety, to check that a method's receiver type can be dispatched on.
8783
///
88-
/// `CoerceSized` is implemented for:
89-
/// - `&[T]` is `CoerceSized<&[T; N]>` for any `N`
90-
/// - `&Trait` is `CoerceSized<&T>` for any `T: Trait`
91-
/// - and similarly for `&mut T`, `*const T`, `*mut T`, `Box<T>`, `Rc<T>`, `Arc<T>`
92-
#[unstable(feature = "coerce_sized", issue = "0")]
93-
#[cfg_attr(not(stage0), lang = "coerce_sized")]
94-
pub trait CoerceSized<T> where T: CoerceUnsized<Self> {
84+
/// example impl:
85+
///
86+
/// ```
87+
/// impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T>
88+
/// where
89+
/// T: Unsize<U>,
90+
/// {}
91+
/// ```
92+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
93+
#[cfg_attr(not(stage0), lang = "dispatch_from_dyn")]
94+
pub trait DispatchFromDyn<T> {
9595
// Empty.
9696
}
9797

98-
// &U -> &T
99-
#[unstable(feature = "coerce_sized", issue = "0")]
100-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<&'a T> for &'a U {}
101-
// &mut U -> &mut T
102-
#[unstable(feature = "coerce_sized", issue = "0")]
103-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<&'a mut T> for &'a mut U {}
104-
// *const U -> *const T
105-
#[unstable(feature = "coerce_sized", issue = "0")]
106-
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<*const T> for *const U {}
107-
// *mut U -> *mut T
108-
#[unstable(feature = "coerce_sized", issue = "0")]
109-
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<*mut T> for *mut U {}
98+
// &T -> &U
99+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
100+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
101+
// &mut T -> &mut U
102+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
103+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
104+
// *const T -> *const U
105+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
106+
impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
107+
// *mut T -> *mut U
108+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
109+
impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
110110

src/libcore/pin.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
use fmt;
9393
use marker::Sized;
94-
use ops::{Deref, DerefMut, CoerceUnsized, CoerceSized};
94+
use ops::{Deref, DerefMut, CoerceUnsized, DispatchFromDyn};
9595

9696
#[doc(inline)]
9797
pub use marker::Unpin;
@@ -325,10 +325,9 @@ where
325325
{}
326326

327327
#[unstable(feature = "pin", issue = "49150")]
328-
impl<'a, P, U> CoerceSized<Pin<P>> for Pin<U>
328+
impl<'a, P, U> DispatchFromDyn<Pin<U>> for Pin<P>
329329
where
330-
P: CoerceUnsized<U>,
331-
U: CoerceSized<P>,
330+
P: DispatchFromDyn<U>,
332331
{}
333332

334333
#[unstable(feature = "pin", issue = "49150")]

src/libcore/ptr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
use convert::From;
7777
use intrinsics;
78-
use ops::{CoerceUnsized, CoerceSized};
78+
use ops::{CoerceUnsized, DispatchFromDyn};
7979
use fmt;
8080
use hash;
8181
use marker::{PhantomData, Unsize};
@@ -2796,7 +2796,7 @@ impl<T: ?Sized> Copy for Unique<T> { }
27962796
impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> { }
27972797

27982798
#[unstable(feature = "ptr_internals", issue = "0")]
2799-
impl<T: ?Sized, U: ?Sized> CoerceSized<Unique<T>> for Unique<U> where T: Unsize<U> { }
2799+
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> { }
28002800

28012801
#[unstable(feature = "ptr_internals", issue = "0")]
28022802
impl<T: ?Sized> fmt::Pointer for Unique<T> {
@@ -2954,8 +2954,8 @@ impl<T: ?Sized> Copy for NonNull<T> { }
29542954
#[unstable(feature = "coerce_unsized", issue = "27732")]
29552955
impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
29562956

2957-
#[unstable(feature = "coerce_sized", issue = "0")]
2958-
impl<T: ?Sized, U: ?Sized> CoerceSized<NonNull<T>> for NonNull<U> where T: Unsize<U> { }
2957+
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
2958+
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
29592959

29602960
#[stable(feature = "nonnull", since = "1.25.0")]
29612961
impl<T: ?Sized> fmt::Debug for NonNull<T> {

src/librustc/middle/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ language_item_table! {
271271
DropTraitLangItem, "drop", drop_trait, Target::Trait;
272272

273273
CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait;
274-
CoerceSizedTraitLangItem, "coerce_sized", coerce_sized_trait, Target::Trait;
274+
DispatchFromDynTraitLangItem,"dispatch_from_dyn", dispatch_from_dyn_trait, Target::Trait;
275275

276276
AddTraitLangItem, "add", add_trait, Target::Trait;
277277
SubTraitLangItem, "sub", sub_trait, Target::Trait;

0 commit comments

Comments
 (0)