Strengthening Tree Borrows so that we can discard Stacked Borrows

オープン
#573 コメント 7 件 リアクション 5 件 担当者 1 名 GitHub で見る

@RalfJung がすでに取り組んでいます。

2025年11月8日 から。

評価

この issue はまだ評価されていません。

説明

A-aliasing-model

We now have SB and TB as two aliasing models with quite different structure. While there's a bunch of questions on which both give the same answer that we may still want to explore (https://github.com/rust-lang/unsafe-code-guidelines/issues/450, and whether nested references have any aliasing requirements), it'd be good to converge towards a single model on the questions where the two do differ.

One of the biggest differences is that TB manages to get away with giving raw pointers the same tag as their parent pointer. This has multiple nice consequences:

  • Within a function, a let mut x and &raw mut x are allowed to be interleaved arbitrarily. Without this, it is wrong to tell people "if you do not use references (nor Box), you do not have to worry about aliasing". Technically, it is correct to say "if you only use raw pointers, you are good", but most people would not realize that accessing x directly runs afoul of that idea. c2rust had to be specifically adjusted to this SB behavior.
  • Casting a &mut T to a *const T and a *mut T finally gives the same result.
  • On the implementation side, we no longer have to worry about finding all the places where a "safe" point gets turned into a raw pointer, which is quite tricky for Box due to its weird state as a hybrid of primitive type and library type.

@rust-lang/opsem if we manage to otherwise make TB basically as strict as SB, do we have general consensus that having a raw pointer share the tag of its parent reference is the right way forward? My vision here is that instead of 2 fairly different models, we have one model with a bunch of parameters that we can tweak.

The remaining differences I can think of right now are:

  • TB does not track UnsafeCell as precisely as SB.
  • TB allows using &T to access memory outside the T. Such "subobject provenance" could be useful for optimizations so we should not discard it prematurely. It should be fairly easy to add a flag to make this optional.
  • TB does not do implicit writes on &mut retags. Doing such implicit writes has been requested by Nikita so we should not discard this option lightly. It should be easy to add a flag for this but I don't know the consequences, it may well be that this breaks too much code without some of the other quirks of SB. So this is the biggest unknown here and likely the first experiment I would make to make progress here: function-entry retags (and only those!) could do an implicit write. This PR has some good testcases.
  • Foreign reads from an &mut freeze it rather than disabling it. That seems fine? I can't think of a good reason why we'd want to force-disable references in this case. Also see https://github.com/rust-lang/unsafe-code-guidelines/issues/351.
  • Raw pointers are allowed to arbitrarily alias with the place they are created from. I think that's a big win over SB.
  • Anything else I forgot?

(I have various student projects planned in that area so please coordinate before starting to work on this yourself.)

主要言語
言語のデータがありません
スター
760
フォーク
68
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/unsafe-code-guidelines のほかの issue

rust-lang/unsafe-code-guidelines の issue をすべて見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。