Skip to content

Commit 64d5cb6

Browse files
committed
Auto merge of #149218 - theemathas:pin-coerce-unsized, r=oli-obk
Make PinCoerceUnsized require Deref Also, delete impls on non-Deref types. Pin doesn't do anything useful for non-Deref types, so PinCoerceUnsized on such types makes no sense. This is a breaking change, since stable code can observe the deleted `PinCoerceUnsized` impls by uselessly coercing between such types inside a `Pin`. There is still some strange behavior, such as `Pin<&mut i32>` being able to coerce to `Pin<&dyn Send>`, but not being able to coerce to `Pin<&i32>`. However, I don't think it's possible to fix this. Fixes #145081
2 parents 8a70352 + f55c8cc commit 64d5cb6

File tree

8 files changed

+5
-65
lines changed

8 files changed

+5
-65
lines changed

library/alloc/src/rc.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,9 +2424,6 @@ unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Rc<T, A> {}
24242424
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
24252425
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for UniqueRc<T, A> {}
24262426

2427-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2428-
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Weak<T, A> {}
2429-
24302427
#[unstable(feature = "deref_pure_trait", issue = "87121")]
24312428
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
24322429

library/alloc/src/sync.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,9 +2426,6 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
24262426
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
24272427
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
24282428

2429-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2430-
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Weak<T, A> {}
2431-
24322429
#[unstable(feature = "deref_pure_trait", issue = "87121")]
24332430
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
24342431

library/core/src/cell.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,18 +2718,6 @@ fn assert_coerce_unsized(
27182718
let _: RefCell<&dyn Send> = d;
27192719
}
27202720

2721-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2722-
unsafe impl<T: ?Sized> PinCoerceUnsized for UnsafeCell<T> {}
2723-
2724-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2725-
unsafe impl<T: ?Sized> PinCoerceUnsized for SyncUnsafeCell<T> {}
2726-
2727-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2728-
unsafe impl<T: ?Sized> PinCoerceUnsized for Cell<T> {}
2729-
2730-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2731-
unsafe impl<T: ?Sized> PinCoerceUnsized for RefCell<T> {}
2732-
27332721
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
27342722
unsafe impl<'b, T: ?Sized> PinCoerceUnsized for Ref<'b, T> {}
27352723

library/core/src/pin.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,9 +1829,10 @@ where
18291829
///
18301830
/// # Safety
18311831
///
1832-
/// If this type implements `Deref`, then the concrete type returned by `deref`
1833-
/// and `deref_mut` must not change without a modification. The following
1834-
/// operations are not considered modifications:
1832+
/// Given a pointer of this type, the concrete type returned by its
1833+
/// `deref` method and (if it implements `DerefMut`) its `deref_mut` method
1834+
/// must be the same type and must not change without a modification.
1835+
/// The following operations are not considered modifications:
18351836
///
18361837
/// * Moving the pointer.
18371838
/// * Performing unsizing coercions on the pointer.
@@ -1842,7 +1843,7 @@ where
18421843
/// to. The concrete type of a slice is an array of the same element type and
18431844
/// the length specified in the metadata. The concrete type of a sized type
18441845
/// is the type itself.
1845-
pub unsafe trait PinCoerceUnsized {}
1846+
pub unsafe trait PinCoerceUnsized: Deref {}
18461847

18471848
#[stable(feature = "pin", since = "1.33.0")]
18481849
unsafe impl<'a, T: ?Sized> PinCoerceUnsized for &'a T {}
@@ -1853,12 +1854,6 @@ unsafe impl<'a, T: ?Sized> PinCoerceUnsized for &'a mut T {}
18531854
#[stable(feature = "pin", since = "1.33.0")]
18541855
unsafe impl<T: PinCoerceUnsized> PinCoerceUnsized for Pin<T> {}
18551856

1856-
#[stable(feature = "pin", since = "1.33.0")]
1857-
unsafe impl<T: ?Sized> PinCoerceUnsized for *const T {}
1858-
1859-
#[stable(feature = "pin", since = "1.33.0")]
1860-
unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
1861-
18621857
/// Constructs a <code>[Pin]<[&mut] T></code>, by pinning a `value: T` locally.
18631858
///
18641859
/// Unlike [`Box::pin`], this does not create a new heap allocation. As explained

library/core/src/ptr/non_null.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::marker::{Destruct, PointeeSized, Unsize};
44
use crate::mem::{MaybeUninit, SizedTypeProperties, transmute};
55
use crate::num::NonZero;
66
use crate::ops::{CoerceUnsized, DispatchFromDyn};
7-
use crate::pin::PinCoerceUnsized;
87
use crate::ptr::Unique;
98
use crate::slice::{self, SliceIndex};
109
use crate::ub_checks::assert_unsafe_precondition;
@@ -1692,9 +1691,6 @@ impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<NonNull<U>> for NonNull<T>
16921691
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
16931692
impl<T: PointeeSized, U: PointeeSized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
16941693

1695-
#[stable(feature = "pin", since = "1.33.0")]
1696-
unsafe impl<T: PointeeSized> PinCoerceUnsized for NonNull<T> {}
1697-
16981694
#[stable(feature = "nonnull", since = "1.25.0")]
16991695
impl<T: PointeeSized> fmt::Debug for NonNull<T> {
17001696
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

library/core/src/ptr/unique.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::clone::TrivialClone;
22
use crate::fmt;
33
use crate::marker::{PhantomData, PointeeSized, Unsize};
44
use crate::ops::{CoerceUnsized, DispatchFromDyn};
5-
use crate::pin::PinCoerceUnsized;
65
use crate::ptr::NonNull;
76

87
/// A wrapper around a raw non-null `*mut T` that indicates that the possessor
@@ -176,9 +175,6 @@ impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<Unique<U>> for Unique<T> wh
176175
#[unstable(feature = "ptr_internals", issue = "none")]
177176
impl<T: PointeeSized, U: PointeeSized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {}
178177

179-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
180-
unsafe impl<T: PointeeSized> PinCoerceUnsized for Unique<T> {}
181-
182178
#[unstable(feature = "ptr_internals", issue = "none")]
183179
impl<T: PointeeSized> fmt::Debug for Unique<T> {
184180
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

library/coretests/tests/pin.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,13 @@ mod pin_coerce_unsized {
4545
pub trait MyTrait {}
4646
impl MyTrait for String {}
4747

48-
// These Pins should continue to compile.
49-
// Do note that these instances of Pin types cannot be used
50-
// meaningfully because all methods require a Deref/DerefMut
51-
// bounds on the pointer type and Cell, RefCell and UnsafeCell
52-
// do not implement Deref/DerefMut.
53-
54-
pub fn cell(arg: Pin<Cell<Box<String>>>) -> Pin<Cell<Box<dyn MyTrait>>> {
55-
arg
56-
}
57-
pub fn ref_cell(arg: Pin<RefCell<Box<String>>>) -> Pin<RefCell<Box<dyn MyTrait>>> {
58-
arg
59-
}
60-
pub fn unsafe_cell(arg: Pin<UnsafeCell<Box<String>>>) -> Pin<UnsafeCell<Box<dyn MyTrait>>> {
61-
arg
62-
}
63-
6448
// These sensible Pin coercions are possible.
6549
pub fn pin_mut_ref(arg: Pin<&mut String>) -> Pin<&mut dyn MyTrait> {
6650
arg
6751
}
6852
pub fn pin_ref(arg: Pin<&String>) -> Pin<&dyn MyTrait> {
6953
arg
7054
}
71-
pub fn pin_ptr(arg: Pin<*const String>) -> Pin<*const dyn MyTrait> {
72-
arg
73-
}
74-
pub fn pin_ptr_mut(arg: Pin<*mut String>) -> Pin<*mut dyn MyTrait> {
75-
arg
76-
}
77-
pub fn pin_non_null(arg: Pin<NonNull<String>>) -> Pin<NonNull<dyn MyTrait>> {
78-
arg
79-
}
8055
pub fn nesting_pins(arg: Pin<Pin<&String>>) -> Pin<Pin<&dyn MyTrait>> {
8156
arg
8257
}

library/std/src/sys/pal/sgx/abi/usercalls/alloc.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::cell::UnsafeCell;
88
use crate::convert::TryInto;
99
use crate::mem::{self, ManuallyDrop, MaybeUninit};
1010
use crate::ops::{CoerceUnsized, Deref, DerefMut, Index, IndexMut};
11-
use crate::pin::PinCoerceUnsized;
1211
use crate::ptr::{self, NonNull};
1312
use crate::slice::SliceIndex;
1413
use crate::{cmp, intrinsics, slice};
@@ -773,9 +772,6 @@ where
773772
#[unstable(feature = "sgx_platform", issue = "56975")]
774773
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UserRef<U>> for UserRef<T> {}
775774

776-
#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
777-
unsafe impl<T: ?Sized> PinCoerceUnsized for UserRef<T> {}
778-
779775
#[unstable(feature = "sgx_platform", issue = "56975")]
780776
impl<T, I> Index<I> for UserRef<[T]>
781777
where

0 commit comments

Comments
 (0)