Partial Borrows in Rust

Australian National University National University of Singapore

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. println!("{a}");

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. println!("{a}");

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = &mut text;
  7. edit.push('!');

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. let edit = &mut text;
  6. println!("{b}");
  7. edit.push('!');

error[E0502]: cannot borrow `text` as mutable because it is also borrowed as immutable

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
    immutable borrow occurs here
  4. println!("{a}");
  5. let edit = &mut text;
  6. println!("{b}");
  7. edit.push('!');

error[E0502]: cannot borrow `text` as mutable because it is also borrowed as immutable

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
    immutable borrow occurs here
  4. println!("{a}");
  5. let edit = &mut text;
    mutable borrow occurs here
  6. println!("{b}");
  7. edit.push('!');

error[E0502]: cannot borrow `text` as mutable because it is also borrowed as immutable

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
    immutable borrow occurs here
  4. println!("{a}");
  5. let edit = &mut text;
    mutable borrow occurs here
  6. println!("{b}");
    immutable borrow later used here
  7. edit.push('!');

error[E0502]: cannot borrow `text` as mutable because it is also borrowed as immutable

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = &mut text;
  7. edit.push('!');

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = &mut text;
  7. edit.push('!');
The expression assigned to edit on line 6 is highlighted.

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = get_mut(&mut text);
  7. edit.push('!');
The expression assigned to edit on line 6 is highlighted.
fn get_mut(s: &mut String) -> &mut String { ... }

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = get_mut(&mut text);
  7. edit.push('!');
Ferris holds the last t in text on line 6, where the exclusive borrow begins. The bracket beside edit ends at its last use.
fn get_mut(s: &mut String) -> &mut String { ... }

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = get_mut(&mut text);
  7. println!("{text}");
  8. edit.push('!');
Ferris holds the last t in text on line 6, where the exclusive borrow begins. The bracket beside edit ends at its last use.Rust rejects the print on line 7 because line 8 still uses edit.
fn get_mut(s: &mut String) -> &mut String { ... }

error[E0502]: cannot borrow `text` as immutable because it is also borrowed as mutable

A bit of Rust

  1. let mut text = String::from("SG PL Summit is awesome");
  2. let a = &text;
  3. let b = &text;
  4. println!("{a}");
  5. println!("{b}");
  6. let edit = get_mut(&mut text);
  7. edit.push('!');
  8. println!("{text}");
Ferris holds the last t in text on line 6, where the exclusive borrow begins. The bracket beside edit ends at its last use.
fn get_mut(s: &mut String) -> &mut String { ... }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3. }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3. }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3.     let register = editor.registers.get_or_insert(name);
  4. }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3.     let register = editor.registers.get_or_insert(name);
  4.     delete_selection_impl(register, doc, pane.id);
  5. }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
    first mutable borrow occurs here
  3.     let register = editor.registers.get_or_insert(name);
    second mutable borrow occurs here
  4.     delete_selection_impl(register, doc, pane.id);
    first borrow later used here
  5. }

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
    first mutable borrow occurs here
  3.     let register = editor.registers.get_or_insert(name);
    second mutable borrow occurs here
  4.     delete_selection_impl(register, doc, pane.id);
    first borrow later used here
  5. }
impl Editor {
    fn current(&mut self) -> (&mut Pane, &mut Document) {
        let pane = &mut self.tree.panes[self.tree.focus];
        let doc = &mut self.documents[pane.doc];
        (pane, doc)
    }
}

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
    first mutable borrow occurs here
  3.     let register = editor.registers.get_or_insert(name);
    second mutable borrow occurs here
  4.     delete_selection_impl(register, doc, pane.id);
    first borrow later used here
  5. }
impl Editor {
    fn current(&mut self) -> (&mut Pane, &mut Document) {
        let pane = &mut self.tree.panes[self.tree.focus];
        let doc = &mut self.documents[pane.doc];
        (pane, doc)
    }
}

Rust is too coarse

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
    first mutable borrow occurs here
  3.     let register = editor.registers.get_or_insert(name);
    second mutable borrow occurs here
  4.     delete_selection_impl(register, doc, pane.id);
    first borrow later used here
  5. }
impl Editor {
    fn current(&mut self) -> (&mut Pane, &mut Document) {
        let pane = &mut self.tree.panes[self.tree.focus];
        let doc = &mut self.documents[pane.doc];
        (pane, doc)
Bars cover the three lines of the method body. Rust uses the signature to borrow-check the caller.
    }
}

View types

impl Editor {
    fn current(&mut {mut tree, mut documents} self)
        -> (&mut Pane, &mut Document) {
        ...
    }
}

View types

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3.     let register = editor.registers.get_or_insert(name);
  4.     delete_selection_impl(register, doc, pane.id);
  5. }

Compiles!

impl Editor {
    fn current(&mut {mut tree, mut documents} self)
        -> (&mut Pane, &mut Document) {
        ...
    }
}

View types

  1. fn delete_selection(editor: &mut Editor, name: char) {
  2.     let (pane, doc) = editor.current();
  3.     let register = editor.registers.get_or_insert(name);
  4.     delete_selection_impl(register, doc, pane.id);
  5. }
impl Editor {
    fn current(&mut {mut tree, mut documents} self)
        -> (&mut Pane, &mut Document) {
        ...
    }
}

Aeneas

Aeneas: Rust Verification by Functional Translation. Son Ho and Jonathan Protzenko, ICFP 2022.

Aeneas

LLBC

Concrete semantics

E-MutBorrow

⊢Ω[p↦v←loanm ℓ:ty_of(v)]⇒mutΩ′⊥,loan∉vℓ∉ΩΩ⊢&mut  p⇓(borm ℓ  v, Ω′)\dfrac{ \begin{gathered} \vdash \Omega[p \mapsto v \leftarrow \mathsf{loan}^{m}\,\ell : \mathsf{ty\_of}(v)] \xRightarrow{\mathsf{mut}} \Omega'\\ \bot,\mathsf{loan} \notin v \qquad \ell \notin \Omega \end{gathered} }{\Omega \vdash \&\mathsf{mut}\;p \Downarrow (\mathsf{bor}^{m}\,\ell\;v,\,\Omega')}

E-SharedBorrow

(ℓ′,w)={(ℓ0,v)if v=loans ℓ0  u(ℓ,loans ℓ  v)otherwise⊢Ω[p↦v←w]⇒immΩ′⊥,loanm∉vℓ∉ΩΩ⊢& p⇓(bors ℓ′:& ty_of(v), Ω′)\dfrac{ \begin{gathered} (\ell', w) = \begin{cases} (\ell_0, v) & \text{if } v = \mathsf{loan}^{s}\,\ell_0\;u \\ (\ell, \mathsf{loan}^{s}\,\ell\;v) & \text{otherwise} \end{cases}\\[0.45em] \vdash \Omega[p \mapsto v \leftarrow w] \xRightarrow{\mathsf{imm}} \Omega'\\ \bot,\mathsf{loan}^{m} \notin v \qquad \ell \notin \Omega \end{gathered} }{\Omega \vdash \&\,p \Downarrow (\mathsf{bor}^{s}\,\ell' : \&\,\mathsf{ty\_of}(v),\,\Omega')}

LLBC#

Symbolic semantics

Region abstraction

A[borm ℓ  (_:τ),  loanm ℓ′]A\big[\mathsf{bor}^m\,\ell\;(\_:\tau),\;\mathsf{loan}^m\,\ell'\big]

Proj-Input-Mut

ty_of(v) is ref-freeproj_input  (borm ℓ  v)  [borm ℓ  (_:ty_of(v))]\dfrac{\mathsf{ty\_of}(v)\ \text{is ref-free}} {\mathsf{proj\_input}\;(\mathsf{bor}^m\,\ell\;v)\;[\mathsf{bor}^m\,\ell\;(\_:\mathsf{ty\_of}(v))]}

Proj-Output-Mut

τ is ref-freeproj_output  (&mut τ)  (borm ℓ′  sym_of(τ))  [loanm ℓ′:τ]\dfrac{\tau\ \text{is ref-free}} {\mathsf{proj\_output}\;(\&\mathsf{mut}\,\tau)\;(\mathsf{bor}^m\,\ell'\;\mathsf{sym\_of}(\tau))\;[\mathsf{loan}^m\,\ell' : \tau]}

Aeneas

Soundness

If every function borrow-checks, main never gets stuck.

(∀f∈P.  borrow_checks(P,f))⟹∀n.  ∃R.  P, Ωmain⊢nsmain⇓R⟹∀n.  ∃R.  ∧  (R=∞  ∨  ∃Ω′.  R∈{(return, Ω′),  (panic, Ω′)})\begin{aligned} &\bigl(\forall f \in \mathcal P.\; \mathsf{borrow\_checks}(\mathcal P, f)\bigr) \\[0.15em] &\quad \Longrightarrow \quad \forall n.\; \exists R.\; \mathcal P,\,\Omega_{\mathit{main}} \vdash_n s_{\mathit{main}} \Downarrow R \\ &\hphantom{\quad \Longrightarrow \quad \forall n.\; \exists R.\;} {}\land\; \bigl(R = \infty \;\lor\; \exists \Omega'.\; R \in \{(\mathtt{return},\,\Omega'),\; (\mathtt{panic},\,\Omega')\}\bigr) \end{aligned}

Aeneas

Soundness

If every function borrow-checks, main never gets stuck.

(∀f∈P.  borrow_checks(P,f))⟹∀n.  ∃R.  P, Ωmain⊢nsmain⇓R⟹∀n.  ∃R.  ∧  (R=∞  ∨  ∃Ω′.  R∈{(return, Ω′),  (panic, Ω′)})\begin{aligned} &\bigl(\forall f \in \mathcal P.\; \mathsf{borrow\_checks}(\mathcal P, f)\bigr) \\[0.15em] &\quad \Longrightarrow \quad \forall n.\; \exists R.\; \mathcal P,\,\Omega_{\mathit{main}} \vdash_n s_{\mathit{main}} \Downarrow R \\ &\hphantom{\quad \Longrightarrow \quad \forall n.\; \exists R.\;} {}\land\; \bigl(R = \infty \;\lor\; \exists \Omega'.\; R \in \{(\mathtt{return},\,\Omega'),\; (\mathtt{panic},\,\Omega')\}\bigr) \end{aligned}
LLBC#

Borrow checking

LLBC+

Combined semantics

LLBC

Concrete execution

Ω#⊢#s⇓R#\Omega^{\#} \vdash_{\#} s \Downarrow R^{\#}
Embedding
Ω#⊢n+s⇓R+\Omega^{\#} \vdash^{+}_{n} s \Downarrow R_{+}
Simulation
Ω⊢n+s⇓[r]\Omega \vdash^{+}_{n} s \Downarrow [r]
Stability
Ω⊢ns⇓r\Omega \vdash_{n} s \Downarrow r

Aeneas

LLBC#. Shared borrowing inside a region abstraction needed repair.

Borrow checking

LLBC+. This semantics includes the same shared-borrow rule.

Combined semantics

LLBC

Concrete execution

Ω#⊢#s⇓R#\Omega^{\#} \vdash_{\#} s \Downarrow R^{\#}
Embedding Twemoji collision, by Twitter, Inc. and other contributors. Colors adapted for the slide. Artwork: https://github.com/twitter/twemoji/blob/v14.0.2/assets/svg/1f4a5.svg . Licensed under CC BY 4.0: https://creativecommons.org/licenses/by/4.0/ .
Ω#⊢n+s⇓R+\Omega^{\#} \vdash^{+}_{n} s \Downarrow R_{+}
Simulation Twemoji collision, by Twitter, Inc. and other contributors. Colors adapted for the slide. Artwork: https://github.com/twitter/twemoji/blob/v14.0.2/assets/svg/1f4a5.svg . Licensed under CC BY 4.0: https://creativecommons.org/licenses/by/4.0/ .
Ω⊢n+s⇓[r]\Omega \vdash^{+}_{n} s \Downarrow [r]
Stability
Ω⊢ns⇓r\Omega \vdash_{n} s \Downarrow r

Aeneas

LLBC#Mechanized in Rocq

Borrow checking

LLBC+Mechanized in Rocq

Combined semantics

LLBCMechanized in Rocq

Concrete execution

Ω#⊢#s⇓R#\Omega^{\#} \vdash_{\#} s \Downarrow R^{\#}
EmbeddingProved in Rocq
Ω#⊢n+s⇓R+\Omega^{\#} \vdash^{+}_{n} s \Downarrow R_{+}
SimulationProved in Rocq
Ω⊢n+s⇓[r]\Omega \vdash^{+}_{n} s \Downarrow [r]
StabilityProved in Rocq
Ω⊢ns⇓r\Omega \vdash_{n} s \Downarrow r

Aeneas

LLBC#-ViewMechanized in Rocq

Borrow checking

LLBC+-ViewMechanized in Rocq

Combined semantics

LLBC-ViewMechanized in Rocq

Concrete execution

Ω#⊢#s⇓R#\Omega^{\#} \vdash_{\#} s \Downarrow R^{\#}
EmbeddingProved in Rocq
Ω#⊢n+s⇓R+\Omega^{\#} \vdash^{+}_{n} s \Downarrow R_{+}
SimulationProved in Rocq
Ω⊢n+s⇓[r]\Omega \vdash^{+}_{n} s \Downarrow [r]
StabilityProved in Rocq
Ω⊢ns⇓r\Omega \vdash_{n} s \Downarrow r

Zero-cost abstraction

Compile time (per-workspace range)
−1.09%to+1.14%

Zero-cost abstraction

Compile time (rounded overall change)
≈0.00%

Zero-cost abstraction

Compile time (rounded overall change)
≈0.00%
Runtime
0%

Summary

  1. View types are a refinement of reference types.

Summary

  1. View types are a refinement of reference types.
  2. Backward compatible and zero cost.

Summary

  1. View types are a refinement of reference types.
  2. Backward compatible and zero cost.
  3. Proved sound in Aeneas and mechanized in Rocq.

Summary

  1. View types are a refinement of reference types.
  2. Backward compatible and zero cost.
  3. Proved sound in Aeneas and mechanized in Rocq.

Try the playground!

richwill.dev/research/partial-borrows-in-rust

Read the paper!

arxiv.org/abs/2611.20576