Skip to content

Polonius: passing an impl Trait value through a closure drops the borrow it captures #164546

Closed as duplicate

Description

@leeewee

The code below is the dangle example of tests/ui/borrowck/alias-liveness/rpit-hide-lifetime-for-swap.rs, with one change: the short-lived impl Swap value is passed through an identity closure, (|a| a)(hide(short)), before it is used.

use std::cell::RefCell;
use std::rc::Rc;

trait Swap: Sized {
    fn swap(self, other: Self);
}
impl<T> Swap for Rc<RefCell<T>> {
    fn swap(self, other: Self) {
        <RefCell<T>>::swap(&self, &other);
    }
}

fn hide<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + use<'a, 'b, T> + 'a {
    x
}

fn dangle() -> &'static [i32; 3] {
    let long = Rc::new(RefCell::new(&[4, 5, 6]));
    let x = [1, 2, 3];
    let short = Rc::new(RefCell::new(&x));
    hide(long.clone()).swap((|a| a)(hide(short))); // `.swap(hide(short))` is rejected (E0597)
    let res: &'static [i32; 3] = *long.borrow();
    res
}

fn main() {
    println!("{:?}", dangle());
}

I expected to see this happen: E0597 (x does not live long enough), as without the closure, and as with -Zpolonius=off.

Instead, this happened: It compiles on nightly-2026-10-15 with default flags.

Related

This was accepted before #164337, which we reported earlier. With -Zpolonius=next, #164337's programs are first accepted on nightly-2025-05-24, while this program is accepted on nightly-2025-05-23 already.

LLM disclosure: The bug was found by an automated testing tool developed with the assistance of an LLM. This report text was drafted by an LLM and reviewed/edited by me.

Meta

rustc --version --verbose:

rustc 1.101.0-nightly (7df81381d 2026-10-14)
binary: rustc
commit-hash: 7df81381d027710708209f36b98d11b1c88f8d2e
commit-date: 2026-10-14
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.3

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 18, 2026
  2. added
    A-closuresArea: Closures (`|…| { … }`)
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    NLL-poloniusIssues related for using Polonius in the borrow checker
    C-bugCategory: This is a bug.
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    and removed
    A-closuresArea: Closures (`|…| { … }`)
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    C-bugCategory: This is a bug.
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 19, 2026
  3. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 19, 2026
  4. jackh726 commented on Oct 19, 2026

    @jackh726
    Member

    In the future, it would be good to first check on Zulip, or do a more thorough issue search, before opening issues if you're going to be doing automated discovery like this. (I generally think doing this discovery is good - but only if the issues are not opened as duplicates.)

  5. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessNLL-poloniusIssues related for using Polonius in the borrow checkerT-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions