rust-lang / rust-lang/rust

Unsoundness in Polonius involving impl Trait

Open
#161,038 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-impl-trait 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

The following code was created based on #160979 with trial and error. It compiles in nightly and segfaults. It doesn't compile with -Zpolonius=off.

type Payload = Box<i32>;

trait Produce<'a> {
    fn produce(self) -> &'a &'a Payload;
}
impl<'a> Produce<'a> for &'a &'a Payload {
    fn produce(self) -> &'a &'a Payload {
        self
    }
}

trait Outlives<'a, 'b> {
    fn shorten(x: impl Produce<'a> + 'a) -> impl Produce<'a> + 'b;
}
impl<'a: 'b, 'b> Outlives<'a, 'b> for () {
    fn shorten(x: impl Produce<'a> + 'a) -> impl Produce<'a> + 'b {
        x
    }
}

fn make_producer<'a, 'b>(payload: &'a &'b Payload) -> impl Produce<'a> + 'b + use<'a, 'b>
where
    (): Outlives<'a, 'b>,
{
    <()>::shorten(payload)
}

fn main() {
    let wrong: &&Payload;
    let a = Box::new(1);
    {
        let mut r: &Payload = &a;
        let x: &&Payload = &r;
        wrong = make_producer(x).produce();
        r = &*Box::new(Box::new(2));
    }
    println!("{wrong}");
}
Meta

Reproducible on the playground with version 1.99.0-nightly (2026-08-12 c98d0cb27cc63afdd626)

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 provided Rust reproducer, compiling it on nightly and with -Zpolonius=off to confirm the differing behavior. Investigate the Polonius and impl Trait handling implicated by the example; done means the compiler no longer accepts this unsound case or segfaults.

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.