# An alternative to the current \`Unpin\` hack:\`UnsafeAlias\`

**URL:** <https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810>\
**Category:** Unsafe Code Guidelines\
**Created:** [May 11, 2023, 12:27pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810 "2023-05-11T12:27:57Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdasfe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fdasfe/32/13939_2.png) [@fdasfe](https://internals.rust-lang.org/u/fdasfe)\
**Post date:** [May 11, 2023, 12:27pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/1 "2023-05-11T12:27:57Z")

</div>

One way to allow self-referential structs would be a new type `UnsafeAlias`, which allows a pointer and a mutable reference to overlap. The proposed api would be:

```rust
impl<T:?Sized> UnsafeAlias<T>{
    pub fn new(value:T) where T:Sized ->Self
    pub fn as_ref(&self)-&T
    pub fn from_ref(reference:&T)->&Self
    pub fn from_mut(reference:&mut T)->&mut Self
    pub fn into_inner(self)->T where T:Sized

    ///Safety:The returned pointer is safe to read from but can cause undefined behaviour 
    ///if written to(unless the data written to is inside an `UnsafeCell`)
    pub fn get_ptr(&mut self)->*const T
}

impl<T:?Sized> !Unpin for UnsafeAlias<T>{} 

```

This would allow self-referential structs and async functions while still allowing optimisations on shared references. On an llvm level, the only change would be removing the `noalias` annotation on mutable references pointing to a type containing a `UnsafeAlias` ;Shared references can remain `noalias readonly`(assuming the type does not contain a `UnsafeCell`).

The only change to Tree Borrows that would be needed to accommodate this would be adding a check to the transition from active to a frozen and leaving the reference in the active state if the affected byte is in an `UnsafeAlias`.

[Example of a self referential struct using `UnsafeAlias`](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a2ea2fb87063ad086f131b0ccf9379bb)

[Another example of code that would work](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=38be0982c7e821e7f43b22f5079578da)

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [May 11, 2023, 1:10pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/2 "2023-05-11T13:10:48Z")

</div>

What is the intended/desired variance on T?

---

<div class="post-metadata">

**Author:** ![fdasfe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fdasfe/32/13939_2.png) [@fdasfe](https://internals.rust-lang.org/u/fdasfe)\
**Post date:** [May 11, 2023, 1:38pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/3 "2023-05-11T13:38:54Z")

</div>

Probably covariance, because it is impossible for a downstream type to make it covariant if it is invariant.

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [May 11, 2023, 5:35pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/4 "2023-05-11T17:35:53Z")

</div>

Would it make legal (that is, non-UB) to actually perform read and writes from both `&mut UnsafeAlias<T>` and `*mut UnsafeAlias<T>` or this only prevents the insta UB that happens when creating the alias?

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [May 11, 2023, 8:05pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/5 "2023-05-11T20:05:45Z")

</div>

(NOT A CONTRIBUTION)

You're proposing this in addition to Pin and Unpin, to replace the rules currently used for noalias, without changing the library-soundness aspects of Pin and Unpin? Presumably generated async function bodies would be wrapped in this (or treated as if they were by the compiler)?

Would this have a negative impl of Unpin? it seems like it should.

And because of this, anyone currently relying on the pin guarantees (like tokio) would now be unsound, right? They would need to wrap their types in this? So it would be a technically breaking change for those users.

---

<div class="post-metadata">

**Author:** ![fdasfe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fdasfe/32/13939_2.png) [@fdasfe](https://internals.rust-lang.org/u/fdasfe)\
**Post date:** [May 12, 2023, 8:25am UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/6 "2023-05-12T08:25:00Z")

</div>

> [@dlight](#):
>
> Would it make legal (that is, non-UB) to actually perform read and writes from both `&mut UnsafeAlias<T>` and `*mut UnsafeAlias<T>` or this only prevents the insta UB that happens when creating the alias?

It would be legal to read, but not write, to the derived pointer(with the normal exception for `UnsafeCell`); The fact that `get_ptr()` returns a `*const` instead of a `*mut` is supposed to imply this.

> [@withoutboats](#):
>
> Would this have a negative impl of Unpin? it seems like it should.

I will add that.

> [@withoutboats](#):
>
> And because of this, anyone currently relying on the pin guarantees (like tokio) would now be unsound, right? They would need to wrap their types in this? So it would be a technically breaking change for those users.

The easiest way to phase this in would be to say that it is technically Undefined Behaviour, but not to optimise around it until the next edition (Technically `&mut !Unpin` should have `noalias` applied now, so tokio(and other crates that rely on this behaviour) are already unsound).

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [May 12, 2023, 9:32am UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/7 "2023-05-12T09:32:11Z")

</div>

> [@fdasfe](#):
>
> ` pub fn from_mut(reference:&mut T)->&mut Self`

btw this looks unsound

---

<div class="post-metadata">

**Author:** ![fdasfe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fdasfe/32/13939_2.png) [@fdasfe](https://internals.rust-lang.org/u/fdasfe)\
**Post date:** [May 12, 2023, 10:31am UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/8 "2023-05-12T10:31:20Z")

</div>

How could it cause unsoundness?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [May 12, 2023, 10:48am UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/9 "2023-05-12T10:48:02Z")

</div>

hmm... we guess UnsafeAlias is like some sort of AlwaysShared...

so `&mut T` to `&mut AlwaysShared<T>` is like `&mut T` to `&T`?

the problem is once you create the self-references...

hmm so maybe it's not inherently unsound but you'd need to undo the self-references by the time you go back to having a `&mut T`. which sounds like a footgun.

(we still think we'd be better off getting `selfref` into `std`...)

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [May 12, 2023, 1:38pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/10 "2023-05-12T13:38:09Z")

</div>

This sounds very similar to [my own prior proposal](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/UnsafeCellMut). Would be good to see a comparison.

> [@withoutboats](#):
>
> And because of this, anyone currently relying on the pin guarantees (like tokio) would now be unsound, right? They would need to wrap their types in this? So it would be a technically breaking change for those users.

`Pin` guarantees in general are unaffected. This is about self-referential generators, which none of the `Pin` RFCs ever specified how to define.

What would be unsound is defining your own self-referential generators. We never stably guaranteed that you could even do that; the only stable way we expose is to write an `async fn` and those will be fixed by adding `UnsafeCellMut`/`UnsafeAlias` to their type in a suitable manner.

So, we might be able to justify this _de jure_. But more relevantly: how big is this problem? How many manual implementations of self-referential futures (not using `async fn`) are there out there that would be rendered unsound by this?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [May 12, 2023, 1:48pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/11 "2023-05-12T13:48:41Z")

</div>

there's `selfref`. and we use it. a lot. arguably too much.

but it's an abstraction, so we can easily update it.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [May 12, 2023, 4:41pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/12 "2023-05-12T16:41:42Z")

</div>

(NOT A CONTRIBUTION)

> [@RalfJung](#):
>
> So, we might be able to justify this _de jure_. But more relevantly: how big is this problem? How many manual implementations of self-referential futures (not using `async fn`) are there out there that would be rendered unsound by this?

Tokio currently uses the pinning guarantee to implement many of its synchronization primitives using an intrusive linked list.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [May 12, 2023, 4:54pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/13 "2023-05-12T16:54:33Z")

</div>

Oh, I think I see what you mean. Whether such code is rendered unsound heavily depends on the details of the code and the aliasing model (Stacked Borrows vs Tree Borrows).

---

<div class="post-metadata">

**Author:** ![foconoco](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@foconoco](https://internals.rust-lang.org/u/foconoco)\
**Post date:** [May 18, 2023, 1:25pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/14 "2023-05-18T13:25:21Z")

</div>

I don't understand the need for this function:

```rust
pub fn get_ptr(&mut self) -> *const T

```

wouldn't this be equivalent to the following?

```rust
UnsafeAlias::as_ref(x) as *const T

```

Or is the pointer returned by `get_ptr` somehow magic?

---

<div class="post-metadata">

**Author:** ![fdasfe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fdasfe/32/13939_2.png) [@fdasfe](https://internals.rust-lang.org/u/fdasfe)\
**Post date:** [May 18, 2023, 8:08pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/15 "2023-05-18T20:08:15Z")

</div>

I think the best solution would be to still leave the current `!Unpin` hack in place, but mark it as depreciated/don't document it at all. This means all existing code still works, but at some point in the future, we can remove the hack.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 22, 2024, 5:04pm UTC](https://internals.rust-lang.org/t/an-alternative-to-the-current-unpin-hack-unsafealias/18810/16 "2024-12-22T17:04:10Z")

</div>

Closed on December 22, 2024, 5:04pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
