-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for dynamic dispatch + Pin::new soundness
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |