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
    C-bug
    on Oct 18, 2026
  2. added
    needs-triage
    on Oct 18, 2026
  3. added
    A-closures
    A-borrow-checker
    A-impl-trait
    NLL-polonius
    C-bug
    regression-from-stable-to-nightly
    I-unsound
    T-types
    and removed
    A-closures
    A-borrow-checker
    A-impl-trait
    C-bug
    needs-triage
    on Oct 19, 2026
  4. added
    I-prioritize
    on Oct 19, 2026
  5. 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.)

  6. removed
    I-prioritize
    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-checkerA-closuresA-impl-traitC-bugI-unsoundNLL-poloniusT-typesregression-from-stable-to-nightly

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions