Skip to content

Commit 7e1c00d

Browse files
committed
Prevent downstream impl DerefMut for Pin<LocalType>
1 parent e2c96cc commit 7e1c00d

7 files changed

+146
-69
lines changed

library/core/src/pin.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,9 +1689,66 @@ impl<Ptr: [const] Deref> const Deref for Pin<Ptr> {
16891689
}
16901690
}
16911691

1692+
mod helper {
1693+
/// Helper that prevents downstream crates from implementing `DerefMut` for `Pin`.
1694+
///
1695+
/// This type is not `#[fundamental]`, so it's possible to relax its `DerefMut` impl bounds in
1696+
/// the future, so the orphan rules reject downstream impls of `DerefMut` of `Pin`.
1697+
#[repr(transparent)]
1698+
#[unstable(feature = "pin_derefmut_internals", issue = "none")]
1699+
#[allow(missing_debug_implementations)]
1700+
pub struct Pin<Ptr> {
1701+
pointer: Ptr,
1702+
}
1703+
1704+
#[unstable(feature = "pin_derefmut_internals", issue = "none")]
1705+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1706+
pub const trait DerefMut {
1707+
type Target: ?Sized;
1708+
fn deref_mut(&mut self) -> &mut Self::Target;
1709+
}
1710+
1711+
#[unstable(feature = "pin_derefmut_internals", issue = "none")]
1712+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1713+
impl<Ptr: [const] super::DerefMut> const DerefMut for Pin<Ptr>
1714+
where
1715+
Ptr::Target: crate::marker::Unpin,
1716+
{
1717+
type Target = Ptr::Target;
1718+
1719+
#[inline(always)]
1720+
fn deref_mut(&mut self) -> &mut Ptr::Target {
1721+
&mut self.pointer
1722+
}
1723+
}
1724+
}
1725+
16921726
#[stable(feature = "pin", since = "1.33.0")]
16931727
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1694-
impl<Ptr: [const] DerefMut<Target: Unpin>> const DerefMut for Pin<Ptr> {
1728+
#[cfg(not(doc))]
1729+
impl<Ptr> const DerefMut for Pin<Ptr>
1730+
where
1731+
Ptr: [const] Deref,
1732+
helper::Pin<Ptr>: [const] helper::DerefMut<Target = Self::Target>,
1733+
{
1734+
#[inline]
1735+
fn deref_mut(&mut self) -> &mut Ptr::Target {
1736+
// SAFETY: Pin and helper::Pin have the same layout, so this is equivalent to
1737+
// `&mut self.pointer` which is safe because `Target: Unpin`.
1738+
helper::DerefMut::deref_mut(unsafe {
1739+
&mut *(self as *mut Pin<Ptr> as *mut helper::Pin<Ptr>)
1740+
})
1741+
}
1742+
}
1743+
1744+
#[stable(feature = "pin", since = "1.33.0")]
1745+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1746+
#[cfg(doc)]
1747+
impl<Ptr> const DerefMut for Pin<Ptr>
1748+
where
1749+
Ptr: [const] DerefMut,
1750+
Ptr::Target: Unpin,
1751+
{
16951752
fn deref_mut(&mut self) -> &mut Ptr::Target {
16961753
Pin::get_mut(Pin::as_mut(self))
16971754
}

tests/mir-opt/inline_coroutine_body.run2-{closure#0}.Inline.panic-abort.diff

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,25 @@
6363
+ let mut _44: &mut std::future::Ready<()>;
6464
+ let mut _45: &mut std::pin::Pin<&mut std::future::Ready<()>>;
6565
+ scope 14 (inlined <Pin<&mut std::future::Ready<()>> as DerefMut>::deref_mut) {
66-
+ scope 15 (inlined Pin::<&mut std::future::Ready<()>>::as_mut) {
67-
+ let mut _46: &mut &mut std::future::Ready<()>;
68-
+ scope 16 (inlined Pin::<&mut std::future::Ready<()>>::new_unchecked) {
66+
+ let mut _46: *mut std::pin::helper::Pin<&mut std::future::Ready<()>>;
67+
+ let mut _47: *mut std::pin::Pin<&mut std::future::Ready<()>>;
68+
+ scope 15 (inlined <pin::helper::Pin<&mut std::future::Ready<()>> as pin::helper::DerefMut>::deref_mut) {
69+
+ let mut _48: &mut &mut std::future::Ready<()>;
70+
+ scope 16 (inlined <&mut std::future::Ready<()> as DerefMut>::deref_mut) {
6971
+ }
70-
+ scope 18 (inlined <&mut std::future::Ready<()> as DerefMut>::deref_mut) {
71-
+ }
72-
+ }
73-
+ scope 17 (inlined Pin::<&mut std::future::Ready<()>>::get_mut) {
7472
+ }
7573
+ }
76-
+ scope 19 (inlined Option::<()>::take) {
77-
+ let mut _47: std::option::Option<()>;
78-
+ scope 20 (inlined std::mem::replace::<Option<()>>) {
79-
+ scope 21 {
74+
+ scope 17 (inlined Option::<()>::take) {
75+
+ let mut _49: std::option::Option<()>;
76+
+ scope 18 (inlined std::mem::replace::<Option<()>>) {
77+
+ scope 19 {
8078
+ }
8179
+ }
8280
+ }
83-
+ scope 22 (inlined #[track_caller] Option::<()>::expect) {
84-
+ let mut _48: isize;
85-
+ let mut _49: !;
86-
+ scope 23 {
81+
+ scope 20 (inlined #[track_caller] Option::<()>::expect) {
82+
+ let mut _50: isize;
83+
+ let mut _51: !;
84+
+ scope 21 {
8785
+ }
8886
+ }
8987
+ }
@@ -217,18 +215,23 @@
217215
+ _22 = &mut (*_23);
218216
+ StorageDead(_24);
219217
+ StorageLive(_44);
220-
+ StorageLive(_49);
218+
+ StorageLive(_46);
219+
+ StorageLive(_51);
221220
+ StorageLive(_41);
222221
+ StorageLive(_42);
223-
+ _44 = copy (_19.0: &mut std::future::Ready<()>);
224222
+ StorageLive(_47);
225-
+ _47 = Option::<()>::None;
226-
+ _42 = copy ((*_44).0: std::option::Option<()>);
227-
+ ((*_44).0: std::option::Option<()>) = copy _47;
223+
+ _47 = &raw mut _19;
224+
+ _46 = copy _47 as *mut std::pin::helper::Pin<&mut std::future::Ready<()>> (PtrToPtr);
228225
+ StorageDead(_47);
229-
+ StorageLive(_48);
230-
+ _48 = discriminant(_42);
231-
+ switchInt(move _48) -> [0: bb11, 1: bb12, otherwise: bb5];
226+
+ _44 = copy ((*_46).0: &mut std::future::Ready<()>);
227+
+ StorageLive(_49);
228+
+ _49 = Option::<()>::None;
229+
+ _42 = copy ((*_44).0: std::option::Option<()>);
230+
+ ((*_44).0: std::option::Option<()>) = copy _49;
231+
+ StorageDead(_49);
232+
+ StorageLive(_50);
233+
+ _50 = discriminant(_42);
234+
+ switchInt(move _50) -> [0: bb11, 1: bb12, otherwise: bb5];
232235
}
233236
+
234237
+ bb5: {
@@ -291,16 +294,17 @@
291294
+ }
292295
+
293296
+ bb11: {
294-
+ _49 = option::expect_failed(const "`Ready` polled after completion") -> unwind unreachable;
297+
+ _51 = option::expect_failed(const "`Ready` polled after completion") -> unwind unreachable;
295298
+ }
296299
+
297300
+ bb12: {
298301
+ _41 = move ((_42 as Some).0: ());
299-
+ StorageDead(_48);
302+
+ StorageDead(_50);
300303
+ StorageDead(_42);
301304
+ _18 = Poll::<()>::Ready(move _41);
302305
+ StorageDead(_41);
303-
+ StorageDead(_49);
306+
+ StorageDead(_51);
307+
+ StorageDead(_46);
304308
+ StorageDead(_44);
305309
+ StorageDead(_22);
306310
+ StorageDead(_19);

tests/mir-opt/inline_coroutine_body.run2-{closure#0}.Inline.panic-unwind.diff

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,25 @@
6565
+ let mut _46: &mut std::future::Ready<()>;
6666
+ let mut _47: &mut std::pin::Pin<&mut std::future::Ready<()>>;
6767
+ scope 14 (inlined <Pin<&mut std::future::Ready<()>> as DerefMut>::deref_mut) {
68-
+ scope 15 (inlined Pin::<&mut std::future::Ready<()>>::as_mut) {
69-
+ let mut _48: &mut &mut std::future::Ready<()>;
70-
+ scope 16 (inlined Pin::<&mut std::future::Ready<()>>::new_unchecked) {
68+
+ let mut _48: *mut std::pin::helper::Pin<&mut std::future::Ready<()>>;
69+
+ let mut _49: *mut std::pin::Pin<&mut std::future::Ready<()>>;
70+
+ scope 15 (inlined <pin::helper::Pin<&mut std::future::Ready<()>> as pin::helper::DerefMut>::deref_mut) {
71+
+ let mut _50: &mut &mut std::future::Ready<()>;
72+
+ scope 16 (inlined <&mut std::future::Ready<()> as DerefMut>::deref_mut) {
7173
+ }
72-
+ scope 18 (inlined <&mut std::future::Ready<()> as DerefMut>::deref_mut) {
73-
+ }
74-
+ }
75-
+ scope 17 (inlined Pin::<&mut std::future::Ready<()>>::get_mut) {
7674
+ }
7775
+ }
78-
+ scope 19 (inlined Option::<()>::take) {
79-
+ let mut _49: std::option::Option<()>;
80-
+ scope 20 (inlined std::mem::replace::<Option<()>>) {
81-
+ scope 21 {
76+
+ scope 17 (inlined Option::<()>::take) {
77+
+ let mut _51: std::option::Option<()>;
78+
+ scope 18 (inlined std::mem::replace::<Option<()>>) {
79+
+ scope 19 {
8280
+ }
8381
+ }
8482
+ }
85-
+ scope 22 (inlined #[track_caller] Option::<()>::expect) {
86-
+ let mut _50: isize;
87-
+ let mut _51: !;
88-
+ scope 23 {
83+
+ scope 20 (inlined #[track_caller] Option::<()>::expect) {
84+
+ let mut _52: isize;
85+
+ let mut _53: !;
86+
+ scope 21 {
8987
+ }
9088
+ }
9189
+ }
@@ -234,18 +232,23 @@
234232
+ _22 = &mut (*_23);
235233
+ StorageDead(_24);
236234
+ StorageLive(_46);
237-
+ StorageLive(_51);
235+
+ StorageLive(_48);
236+
+ StorageLive(_53);
238237
+ StorageLive(_43);
239238
+ StorageLive(_44);
240-
+ _46 = copy (_19.0: &mut std::future::Ready<()>);
241239
+ StorageLive(_49);
242-
+ _49 = Option::<()>::None;
243-
+ _44 = copy ((*_46).0: std::option::Option<()>);
244-
+ ((*_46).0: std::option::Option<()>) = copy _49;
240+
+ _49 = &raw mut _19;
241+
+ _48 = copy _49 as *mut std::pin::helper::Pin<&mut std::future::Ready<()>> (PtrToPtr);
245242
+ StorageDead(_49);
246-
+ StorageLive(_50);
247-
+ _50 = discriminant(_44);
248-
+ switchInt(move _50) -> [0: bb16, 1: bb17, otherwise: bb7];
243+
+ _46 = copy ((*_48).0: &mut std::future::Ready<()>);
244+
+ StorageLive(_51);
245+
+ _51 = Option::<()>::None;
246+
+ _44 = copy ((*_46).0: std::option::Option<()>);
247+
+ ((*_46).0: std::option::Option<()>) = copy _51;
248+
+ StorageDead(_51);
249+
+ StorageLive(_52);
250+
+ _52 = discriminant(_44);
251+
+ switchInt(move _52) -> [0: bb16, 1: bb17, otherwise: bb7];
249252
}
250253

251254
- bb6 (cleanup): {
@@ -332,16 +335,17 @@
332335
+ }
333336
+
334337
+ bb16: {
335-
+ _51 = option::expect_failed(const "`Ready` polled after completion") -> bb11;
338+
+ _53 = option::expect_failed(const "`Ready` polled after completion") -> bb11;
336339
+ }
337340
+
338341
+ bb17: {
339342
+ _43 = move ((_44 as Some).0: ());
340-
+ StorageDead(_50);
343+
+ StorageDead(_52);
341344
+ StorageDead(_44);
342345
+ _18 = Poll::<()>::Ready(move _43);
343346
+ StorageDead(_43);
344-
+ StorageDead(_51);
347+
+ StorageDead(_53);
348+
+ StorageDead(_48);
345349
+ StorageDead(_46);
346350
+ StorageDead(_22);
347351
+ StorageDead(_19);

tests/ui/deref/pin-impl-deref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ impl MyPinType {
2222
fn impl_deref_mut(_: impl DerefMut) {}
2323
fn unpin_impl_ref(r_unpin: Pin<&MyUnpinType>) {
2424
impl_deref_mut(r_unpin)
25-
//~^ ERROR: the trait bound `Pin<&MyUnpinType>: DerefMut` is not satisfied
25+
//~^ ERROR: the trait bound `&MyUnpinType: DerefMut` is not satisfied
2626
}
2727
fn unpin_impl_mut(r_unpin: Pin<&mut MyUnpinType>) {
2828
impl_deref_mut(r_unpin)
2929
}
3030
fn pin_impl_ref(r_pin: Pin<&MyPinType>) {
3131
impl_deref_mut(r_pin)
3232
//~^ ERROR: `PhantomPinned` cannot be unpinned
33-
//~| ERROR: the trait bound `Pin<&MyPinType>: DerefMut` is not satisfied
33+
//~| ERROR: the trait bound `&MyPinType: DerefMut` is not satisfied
3434
}
3535
fn pin_impl_mut(r_pin: Pin<&mut MyPinType>) {
3636
impl_deref_mut(r_pin)

tests/ui/deref/pin-impl-deref.stderr

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
error[E0277]: the trait bound `Pin<&MyUnpinType>: DerefMut` is not satisfied
1+
error[E0277]: the trait bound `&MyUnpinType: DerefMut` is not satisfied
22
--> $DIR/pin-impl-deref.rs:24:20
33
|
44
LL | impl_deref_mut(r_unpin)
5-
| -------------- ^^^^^^^ the trait `DerefMut` is not implemented for `Pin<&MyUnpinType>`
5+
| -------------- ^^^^^^^ the trait `DerefMut` is not implemented for `&MyUnpinType`
66
| |
77
| required by a bound introduced by this call
88
|
9+
= note: `DerefMut` is implemented for `&mut MyUnpinType`, but not for `&MyUnpinType`
10+
= note: required for `pin::helper::Pin<&MyUnpinType>` to implement `pin::helper::DerefMut`
911
= note: required for `Pin<&MyUnpinType>` to implement `DerefMut`
1012
note: required by a bound in `impl_deref_mut`
1113
--> $DIR/pin-impl-deref.rs:22:27
1214
|
1315
LL | fn impl_deref_mut(_: impl DerefMut) {}
1416
| ^^^^^^^^ required by this bound in `impl_deref_mut`
15-
help: consider mutably borrowing here
16-
|
17-
LL | impl_deref_mut(&mut r_unpin)
18-
| ++++
1917

20-
error[E0277]: the trait bound `Pin<&MyPinType>: DerefMut` is not satisfied
18+
error[E0277]: the trait bound `&MyPinType: DerefMut` is not satisfied
2119
--> $DIR/pin-impl-deref.rs:31:20
2220
|
2321
LL | impl_deref_mut(r_pin)
24-
| -------------- ^^^^^ the trait `DerefMut` is not implemented for `Pin<&MyPinType>`
22+
| -------------- ^^^^^ the trait `DerefMut` is not implemented for `&MyPinType`
2523
| |
2624
| required by a bound introduced by this call
2725
|
26+
= note: `DerefMut` is implemented for `&mut MyPinType`, but not for `&MyPinType`
27+
= note: required for `pin::helper::Pin<&MyPinType>` to implement `pin::helper::DerefMut`
2828
= note: required for `Pin<&MyPinType>` to implement `DerefMut`
2929
note: required by a bound in `impl_deref_mut`
3030
--> $DIR/pin-impl-deref.rs:22:27
3131
|
3232
LL | fn impl_deref_mut(_: impl DerefMut) {}
3333
| ^^^^^^^^ required by this bound in `impl_deref_mut`
34-
help: consider mutably borrowing here
35-
|
36-
LL | impl_deref_mut(&mut r_pin)
37-
| ++++
3834

3935
error[E0277]: `PhantomPinned` cannot be unpinned
4036
--> $DIR/pin-impl-deref.rs:31:20
@@ -51,6 +47,7 @@ note: required because it appears within the type `MyPinType`
5147
|
5248
LL | struct MyPinType(core::marker::PhantomPinned);
5349
| ^^^^^^^^^
50+
= note: required for `pin::helper::Pin<&MyPinType>` to implement `pin::helper::DerefMut`
5451
= note: required for `Pin<&MyPinType>` to implement `DerefMut`
5552
note: required by a bound in `impl_deref_mut`
5653
--> $DIR/pin-impl-deref.rs:22:27
@@ -73,6 +70,7 @@ note: required because it appears within the type `MyPinType`
7370
|
7471
LL | struct MyPinType(core::marker::PhantomPinned);
7572
| ^^^^^^^^^
73+
= note: required for `pin::helper::Pin<&mut MyPinType>` to implement `pin::helper::DerefMut`
7674
= note: required for `Pin<&mut MyPinType>` to implement `DerefMut`
7775
note: required by a bound in `impl_deref_mut`
7876
--> $DIR/pin-impl-deref.rs:22:27

tests/ui/typeck/pin-unsound-issue-85099-derefmut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//@ check-pass
2-
//@ known-bug: #85099
1+
//@ check-fail
32

43
// Should fail. Can coerce `Pin<T>` into `Pin<U>` where
54
// `T: Deref<Target: Unpin>` and `U: Deref<Target: !Unpin>`, using the
@@ -43,6 +42,7 @@ impl<'a, Fut: Future<Output = ()>> SomeTrait<'a, Fut> for Fut {
4342
}
4443

4544
impl<'b, 'a, Fut> DerefMut for Pin<&'b dyn SomeTrait<'a, Fut>> {
45+
//~^ ERROR: conflicting implementations of trait `DerefMut`
4646
fn deref_mut<'c>(
4747
self: &'c mut Pin<&'b dyn SomeTrait<'a, Fut>>,
4848
) -> &'c mut (dyn SomeTrait<'a, Fut> + 'b) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0119]: conflicting implementations of trait `DerefMut` for type `Pin<&dyn SomeTrait<'_, _>>`
2+
--> $DIR/pin-unsound-issue-85099-derefmut.rs:44:1
3+
|
4+
LL | impl<'b, 'a, Fut> DerefMut for Pin<&'b dyn SomeTrait<'a, Fut>> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: conflicting implementation in crate `core`:
8+
- impl<Ptr> DerefMut for Pin<Ptr>
9+
where <pin::helper::Pin<Ptr> as pin::helper::DerefMut>::Target == <Pin<Ptr> as Deref>::Target, Ptr: Deref, pin::helper::Pin<Ptr>: pin::helper::DerefMut, pin::helper::Pin<Ptr>: ?Sized;
10+
= note: upstream crates may add a new impl of trait `std::pin::helper::DerefMut` for type `std::pin::helper::Pin<&dyn SomeTrait<'_, _>>` in future versions
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)