rust-lang / rust-lang/rust

Interaction between `Pin` soundness and arbitrary self types

Open
#157,067 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-pin C-bug F-arbitrary_self_types I-unsound needs-triage requires-nightly
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

#![feature(arbitrary_self_types)]
use std::marker::PhantomPinned;
use std::ops::Receiver;
use std::pin::Pin;

trait MyTrait {
    fn hello(self: Pin<&mut Foo<Self>>) -> Pin<&mut Foo<u32>>;
}
impl MyTrait for u32 {
    fn hello(self: Pin<&mut Foo<Self>>) -> Pin<&mut Foo<u32>> {
        self
    }
}

struct Foo<T: ?Sized> {
    _pin: PhantomPinned,
    val: T,
}

impl<T: ?Sized> Receiver for Foo<T> {
    type Target = T;
}
impl Unpin for Foo<dyn MyTrait> {}

fn pin_it(r: &mut Foo<u32>) -> Pin<&mut Foo<u32>> {
    Pin::<&mut Foo<dyn MyTrait>>::new(r).hello()
}

In the above piece of code, I'm able to convert a &mut Foo<u32> into a Pin<&mut Foo<u32>> even though the type Foo<u32> is !Unpin.

I'm not sure if you can concretely abuse this to trigger UB right now, but it seems bad.

Related test case:
https://github.com/rust-lang/rust/blob/b5e038d7158c1af55a646027fdacf5ecd7c783c7/tests/ui/coercion/pin-dyn-dispatch-sound.rs#L1-L19

https://github.com/rust-lang/rust/blob/b5e038d7158c1af55a646027fdacf5ecd7c783c7/tests/ui/coercion/pin-dyn-dispatch-sound.stderr#L1-L6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue and the related tests in tests/ui/coercion/pin-dyn-dispatch-sound.rs and its .stderr file. Run that UI test, then investigate the interaction between arbitrary self types, Pin, Receiver, and Unpin; done means establishing whether the conversion is unsound and documenting or implementing the required compiler behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.