1
Fork 0

Rollup merge of #125048 - dingxiangfei2009:stable-deref, r=amanieu

PinCoerceUnsized trait into core

cc ``@Darksonn`` ``@wedsonaf`` ``@ojeda``

This is a PR to introduce a `PinCoerceUnsized` trait in order to make trait impls generated by the proc-macro `#[derive(SmartPointer)]`, proposed by [RFC](e17e19ac7a/text/3621-derive-smart-pointer.md (pincoerceunsized-1)), sound. There you may find explanation, justification and discussion about the alternatives.

Note that we do not seek stabilization of this `PinCoerceUnsized` trait in the near future. The stabilisation of this trait does not block the eventual stabilization process of the `#[derive(SmartPointer)]` macro. Ideally, use of `DerefPure` is more preferrable except this will actually constitute a breaking change. `PinCoerceUnsized` emerges as a solution to the said soundness hole while avoiding the breaking change. More details on the `DerefPure` option have been described in this [section](e17e19ac7a/text/3621-derive-smart-pointer.md (derefpure)) of the RFC linked above.

Earlier discussion can be found in this [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/Pin.20and.20soundness.20of.20unsizing.20coercions) and [rust-for-linux thread](https://rust-lang.zulipchat.com/#narrow/stream/425075-rust-for-linux/topic/.23.5Bderive.28SmartPointer.29.5D.20and.20pin.20unsoundness.20rfc.233621).

try-job: dist-various-2
This commit is contained in:
Matthias Krüger 2024-08-07 00:34:11 +02:00 committed by GitHub
commit 16b251be10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 218 additions and 4 deletions

View file

@ -29,3 +29,49 @@ fn pin_const() {
pin_mut_const();
}
#[allow(unused)]
mod pin_coerce_unsized {
use core::cell::{Cell, RefCell, UnsafeCell};
use core::pin::Pin;
use core::ptr::NonNull;
pub trait MyTrait {}
impl MyTrait for String {}
// These Pins should continue to compile.
// Do note that these instances of Pin types cannot be used
// meaningfully because all methods require a Deref/DerefMut
// bounds on the pointer type and Cell, RefCell and UnsafeCell
// do not implement Deref/DerefMut.
pub fn cell(arg: Pin<Cell<Box<String>>>) -> Pin<Cell<Box<dyn MyTrait>>> {
arg
}
pub fn ref_cell(arg: Pin<RefCell<Box<String>>>) -> Pin<RefCell<Box<dyn MyTrait>>> {
arg
}
pub fn unsafe_cell(arg: Pin<UnsafeCell<Box<String>>>) -> Pin<UnsafeCell<Box<dyn MyTrait>>> {
arg
}
// These sensible Pin coercions are possible.
pub fn pin_mut_ref(arg: Pin<&mut String>) -> Pin<&mut dyn MyTrait> {
arg
}
pub fn pin_ref(arg: Pin<&String>) -> Pin<&dyn MyTrait> {
arg
}
pub fn pin_ptr(arg: Pin<*const String>) -> Pin<*const dyn MyTrait> {
arg
}
pub fn pin_ptr_mut(arg: Pin<*mut String>) -> Pin<*mut dyn MyTrait> {
arg
}
pub fn pin_non_null(arg: Pin<NonNull<String>>) -> Pin<NonNull<dyn MyTrait>> {
arg
}
pub fn nesting_pins(arg: Pin<Pin<&String>>) -> Pin<Pin<&dyn MyTrait>> {
arg
}
}