Skip to content

Include constraints held for type tests when computing loan liveness - #163417

Open
jackh726 wants to merge 4 commits into
rust-lang:mainfrom
jackh726:polonius-unsoundness-2
Open

jackh726 wants to merge 4 commits into
rust-lang:mainfrom
jackh726:polonius-unsoundness-2

Conversation

@jackh726

@jackh726 jackh726 commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

If you have:

fn make<'a: 'b, 'b>(x: &'a u8) -> impl Copy + 'a + 'b { x }

pub fn test() {
    let mut y = make::<'static, 'static>(&0);
    {
        let x = 42u8;
        y = make(&x);
        //~^ ERROR `x` does not live long enough
        require_static(y);
    }
}

At require_static(y), we require that (impl Copy + 'a + 'b): 'static, which we do by registering a type test that either 'a: 'static OR 'b: 'static. Neither is registered as a normal region constraint - and so it is not used to compute loan liveness.

In this PR, anytime we discharge a type test, we register all the constraints that could have been used to prove that type test at the location of the type test itself.

You might ask: why all? Surely, we could have registered only 'a: 'static or 'b: 'static? The problem, is that we don't know what constraints later code may rely on (and in fact, if we registered only 'b: 'static, then that would be a soundness hole - because 'bisn't live). Though, if'b: 'static` didn't hold, we wouldn't register it.

This fixes bug two here: https://rust-lang.zulipchat.com/#narrow/channel/186049-t-types.2Fpolonius/topic/Reporting.20a.20few.20Polonius.20bugs/near/617350569

r? lqd

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 27, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 27, 2026
@rustbot

rustbot commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

lqd is not on the review rotation at the moment.
They may take a while to respond.

@jackh726

Copy link
Copy Markdown
Member Author

Disclosure: an LLM was used to generate a first pass at the tests and for early investigation.

@jackh726 jackh726 added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Sep 27, 2026
// considered live for the entire function. Any deferred regions *are*
// regions that fit this category.
if let Some((polonius_context, move_data, borrow_set)) = polonius_context_and_args {
polonius_context.compute_loan_liveness(

@lqd lqd Sep 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the downside of having the regioncx solving be in this module, if we have to propagate loans on the unsolved regioncx, it feels very very hidden to do it here. We discussed maybe having this in nll.rs.

View changes since the review

Comment on lines +257 to +263
unsolved_regioncx
.inner
.constraints
.outlives()
.iter()
.copied()
.chain(unsolved_regioncx.type_test_constraints.iter().copied()),

@lqd lqd Sep 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale is not clear to me on why we need to pass these via this side-channel. So I'd like us to discuss the entire commit.

A bunch of it feels like workarounds for the lack of intersection regions, and some inability to register actual outlives constraints.

View changes since the review

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the polonius-unsoundness-2 branch from 9260398 to 7024200 Compare September 30, 2026 15:18
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the polonius-unsoundness-2 branch from 7024200 to 8d039b2 Compare September 30, 2026 15:22
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the polonius-unsoundness-2 branch from bdb9831 to f7c15cb Compare September 30, 2026 15:35
@lqd lqd added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants