Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit209703a

Browse files
authored
Rollup merge of#125072 - Darksonn:pin-dyn-dispatch-sound, r=jhpratt
Add test for dynamic dispatch + Pin::new soundnessWhile working on [the `#[derive(SmartPointer)]` RFC][1], I realized that the soundness of <code>impl [DispatchFromDyn](https://doc.rust-lang.org/stable/std/ops/trait.DispatchFromDyn.html) for [Pin](https://doc.rust-lang.org/stable/std/pin/struct.Pin.html)</code> relies on the restriction that you can't implement [`Unpin`](https://doc.rust-lang.org/stable/std/marker/trait.Unpin.html) for trait objects.As far as I can tell, the relevant error exists to solve some unrelated issues with coherence. To avoid cases where `Pin` is made unsound due to changes in the coherence-related errors, add a test that verifies that unsound use of `Pin` and `DispatchFromDyn` does not become allowed in the future.[1]:rust-lang/rfcs#3621
2 parents18d9c03 +b3a78c1 commit209703a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::marker::PhantomPinned;
2+
use std::pin::Pin;
3+
4+
traitMyUnpinTrait{
5+
fninto_pinned_type(self:Pin<&mutSelf>) ->Pin<&mutPhantomPinned>;
6+
}
7+
implMyUnpinTraitforPhantomPinned{
8+
fninto_pinned_type(self:Pin<&mutSelf>) ->Pin<&mutPhantomPinned>{
9+
self
10+
}
11+
}
12+
implUnpinfordynMyUnpinTrait{}//~ ERROR E0321
13+
14+
// It would be unsound for this function to compile.
15+
fnpin_it(not_yet_pinned:&mutPhantomPinned) ->Pin<&mutPhantomPinned>{
16+
Pin::new(not_yet_pinnedas&mutdynMyUnpinTrait).into_pinned_type()
17+
}
18+
19+
fnmain(){}
Lines changed: 9 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp