From b3a78c1d094eb978ec876643c3c4d0e5349b1073 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 13 May 2024 13:34:33 +0200 Subject: [PATCH] Add test for dynamic dispatch + Pin::new soundness --- tests/ui/coercion/pin-dyn-dispatch-sound.rs | 19 +++++++++++++++++++ .../ui/coercion/pin-dyn-dispatch-sound.stderr | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/ui/coercion/pin-dyn-dispatch-sound.rs create mode 100644 tests/ui/coercion/pin-dyn-dispatch-sound.stderr diff --git a/tests/ui/coercion/pin-dyn-dispatch-sound.rs b/tests/ui/coercion/pin-dyn-dispatch-sound.rs new file mode 100644 index 0000000000000..b9d43ebac8bf4 --- /dev/null +++ b/tests/ui/coercion/pin-dyn-dispatch-sound.rs @@ -0,0 +1,19 @@ +use std::marker::PhantomPinned; +use std::pin::Pin; + +trait MyUnpinTrait { + fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut PhantomPinned>; +} +impl MyUnpinTrait for PhantomPinned { + fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut PhantomPinned> { + self + } +} +impl Unpin for dyn MyUnpinTrait {} //~ ERROR E0321 + +// It would be unsound for this function to compile. +fn pin_it(not_yet_pinned: &mut PhantomPinned) -> Pin<&mut PhantomPinned> { + Pin::new(not_yet_pinned as &mut dyn MyUnpinTrait).into_pinned_type() +} + +fn main() {} diff --git a/tests/ui/coercion/pin-dyn-dispatch-sound.stderr b/tests/ui/coercion/pin-dyn-dispatch-sound.stderr new file mode 100644 index 0000000000000..45860bfcfc760 --- /dev/null +++ b/tests/ui/coercion/pin-dyn-dispatch-sound.stderr @@ -0,0 +1,9 @@ +error[E0321]: cross-crate traits with a default impl, like `Unpin`, can only be implemented for a struct/enum type, not `(dyn MyUnpinTrait + 'static)` + --> $DIR/pin-dyn-dispatch-sound.rs:12:1 + | +LL | impl Unpin for dyn MyUnpinTrait {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0321`.