rust-lang / rust-lang/rust

Polonius soundness bug with higher ranked function pointers

Open
#161,471 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-higher-ranked C-bug I-unsound NLL-polonius requires-nightly T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm filing this issue on behalf of @carlini, who discovered the initial unsoundness reproducer with an LLM. I then minimized the code to the code below.

The code below creates a &mut that aliases with a &, and somehow uses both of them, causing unsoundness. The aliasing references are r and p.

#![forbid(unsafe_code)]

type Payload = Option<Box<i32>>;

type P<'a, 'b> = for<'x> fn(&'a mut &'b Payload, &'x ()) -> &'x Payload;

fn f<'a, 'b, 'c>(x: &'a mut &'b Payload, _: &'c ()) -> &'b Payload {
    *x
}

fn main() {
    let p: &'static mut Payload = Box::leak(Box::new(Some(Box::new(1))));
    let mid: &mut &Payload = Box::leak(Box::new(&*p));
    let r: &Payload = (f as P<'_, '_>)(mid, &());
    let b: &Box<i32> = r.as_ref().unwrap();
    if false {
        let _: &mut &'static Payload = mid;
    } else {
        *p = None;
        println!("{b}");  // Segfault
    };
}

The unsoundness reproduces with -Zpolonius=next, but doesn't reproduce (fails to compile) with -Zpolonius=no.

Meta

Reproducible on the playground with version 1.100.0-nightly (2026-08-20 8925ea358a0f265ca610)

Contributor guide

Open the contributing guide

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 minimized Rust program in the issue and reproduce it on the stated nightly using -Zpolonius=next; compare with -Zpolonius=no, which currently rejects it. Investigate Polonius handling of the higher-ranked function pointer and the aliasing references r and p. Done means the reproducer no longer permits unsound aliasing or produces the shown failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.