rust-lang / rust-lang/rfcs

Pin the reference inside drop

Open
#3,105 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I'm kinda surprised nobody mentioned this before, since this problem exists since introduction of pinned types

Problem

Drop currently gives a &mut Self, which requires some workarounds (and unsafe code) for !Unpin types.
This is mentioned in pin module.

Solution

Changing the definition of fn drop to receive Pin<&mut Self> is a braking change, but we can do it with the incoming 2021 edition.

Fixes to pre-2021-edition code

When the type is Unpin, the fix is pretty easy, since Pin can deref directly into &mut T

impl Drop for UnpinnedType {
  fn drop(self: Pin<&mut Self>) {
    /* use `self` as it would be `&mut self` */
  }
}

When the type is !Unpin, it is most likely using a similar workaround as described in pin module, also making the change easy

impl Drop for PinnedType {
  fn drop(self: Pin<&mut Self>) {
     fn pinned_drop(this: Pin<&mut PinnedType>) {
       /* ... */
     }
     pinned_drop(self); /* We don't need unsafe, hooray! */
  }
}
Other solutions/ideas
  • Use compiler magic to give Pin<&mut Self> for pinned and &mut Self for unpinned types (since we have already some magic with may_dangle) - in my opinion, this makes more problems than it solves
  • The "do nothing" solution: assume that everyone using pinned types knows how to deal with them - still, unsafe code is needed to convert a &mut T into Pin<&mut T>

EDIT: "this makes more problems than it solves" -> how to deal with cases where a type is generic over some T that can be Unpin or not
EDIT: Link to discussion on zulip
EDIT: Pin has DerefMut implementation if target is Unpin

Contributor guide

No contributing guide indexed for this repository

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 Rust pin module documentation linked in the issue and review the linked Zulip discussion. Determine whether changing Drop to receive Pin<&mut Self> is viable, including generic Unpin cases and compatibility with pre-2021 code; done means reaching and documenting a resolved language-design decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.