Skip to content

Commit b3a78c1

Browse files
committed
Add test for dynamic dispatch + Pin::new soundness
1 parent 6be7b0c commit b3a78c1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::marker::PhantomPinned;
2+
use std::pin::Pin;
3+
4+
trait MyUnpinTrait {
5+
fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut PhantomPinned>;
6+
}
7+
impl MyUnpinTrait for PhantomPinned {
8+
fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut PhantomPinned> {
9+
self
10+
}
11+
}
12+
impl Unpin for dyn MyUnpinTrait {} //~ ERROR E0321
13+
14+
// It would be unsound for this function to compile.
15+
fn pin_it(not_yet_pinned: &mut PhantomPinned) -> Pin<&mut PhantomPinned> {
16+
Pin::new(not_yet_pinned as &mut dyn MyUnpinTrait).into_pinned_type()
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0321]: cross-crate traits with a default impl, like `Unpin`, can only be implemented for a struct/enum type, not `(dyn MyUnpinTrait + 'static)`
2+
--> $DIR/pin-dyn-dispatch-sound.rs:12:1
3+
|
4+
LL | impl Unpin for dyn MyUnpinTrait {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0321`.

0 commit comments

Comments
 (0)