External Publication
Visit Post

Could borrow checking with origins unblock sound specialization

Rust Internals [Unofficial] March 14, 2026
Source

We just don't want codegen to depend on lifetimes , though; we don't merely want specialization on lifetimes to not be unsound, we further don't want to allow specialization on lifetimes at all. Whether or not a given reference is 'static can be somewhat unpredictable due to const promotion (e.g., let x = &1_u8) or reborrowing. Further, specialization on lifetimes can try to specialize on two references having the same lifetime, which seems even more unpredictable.

Also, consider the following:

let n: u8 = 0;
let n_ref = &n;
{
    let other: u8 = 1;
    let another_ref = n_ref;
    // Fairly sure that `if false { .. }` would have the same effect.
    if random_u32().is_multiple_of(2) {
        another_ref = &other;
        // Conditional early return of a borrow.
        return another_ref;
    }

    // Under the current borrow checker, I'm fairly sure the origins are:
    // {shared(n)} and {shared(n), shared(other)}, not the same.
    // Under Polonius, I'm fairly sure the origins are:
    // {shared(n)} and {shared(n)}, the same.
    let result = (n_ref, another_ref).merge();
}

In other words, your proposal would make the exact behavior of the borrow checker load-bearing, and improving the borrow checker would be a breaking change.

Discussion in the ATmosphere

Loading comments...