Skip to content

Consider captured regions for opaque type region liveness.#156027

Open
jackh726 wants to merge 5 commits into
rust-lang:mainfrom
jackh726:opaque-liveness
Open

Consider captured regions for opaque type region liveness.#156027
jackh726 wants to merge 5 commits into
rust-lang:mainfrom
jackh726:opaque-liveness

Conversation

@jackh726

@jackh726 jackh726 commented May 1, 2026

Copy link
Copy Markdown
Member

View all comments

Fixes #153215

For opaques, when we're calculating liveness for opaques, we want to consider any captured lifetimes that can outlive the opaque type, which is more than just the outlives bounds.

r? lqd

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

lqd commented May 1, 2026

Copy link
Copy Markdown
Member

Until I can look at this next week, @bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 1, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 1, 2026
Consider captured regions for opaque type region liveness.
@rust-bors

rust-bors Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 97f0332 (97f0332b563f5fd8ae70b57a13a7b23b27db3012, parent: 0469a92a76c327df972cb6c1356934b7a0c6b86d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (97f0332): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -2.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

Cycles

Results (secondary 2.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.1% [3.3%, 4.9%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.2%, secondary 0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.4%] 33
Regressions ❌
(secondary)
0.2% [0.0%, 0.4%] 24
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.1%, 0.4%] 33

Bootstrap: 481.713s -> 483.106s (0.29%)
Artifact size: 390.96 MiB -> 390.99 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 1, 2026

@lqd lqd left a comment

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.

I also started down this direction so some of this makes sense to me. We stopped because it felt fragile and test coverage was, and still is, weak. So with that caveat this generally feels fine to me, but again, I don't know enough about this area to say what problems there could be.

This direction felt less nice than what lcnr described in the t-types meeting where we described the issue, so I'll defer to them.

View changes since this review

Comment thread compiler/rustc_middle/src/queries.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment on lines +55 to +59
// Thinking about it, I was originally a bit concerned about something like `'a: 'static`, and
// whether or not we need to mark `'a` as live. I don't think *today* we do, since I think regions
// that outlive `'static` are special enough, but I *could* imagine some world where we need to be
// more careful about this. Given I can't find a test that goes wrong, I'm going to leave in this
// optimization.

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.

I was also a bit worried, and we should expand test coverage here. I'm not confident enough in any of this to trust the two tests we have.

Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment on lines +319 to +321
// Unfortunately, we have to use a new `InferCtxt` each call, because
// region constraints get added and solved there and we need to test each
// call individually.

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 also unfortunate, combined with the fact that it's being called a quadratic number of times (thankfully the N should be small in most cases).

@lcnr lcnr self-assigned this May 5, 2026
@rustbot

This comment has been minimized.

Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
@jackh726

jackh726 commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 3, 2026
Consider captured regions for opaque type region liveness.
@rust-bors

rust-bors Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8e819ff (8e819ff5434466e9a823e03871a1775ac7d84ddf, parent: 53509ca37e3b507887607c2f4a7f23bd4838f099)

@jackh726

jackh726 commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-156027 created and queued.
🤖 Automatically detected try build 8e819ff
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 3, 2026
@jackh726

jackh726 commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@craterbot p=2

We want to get this in so that we can do a nightly preview of Polonius

@craterbot

Copy link
Copy Markdown
Collaborator

📝 Configuration of the pr-156027 experiment changed.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot

Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-156027 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot

Copy link
Copy Markdown
Collaborator

🎉 Experiment pr-156027 is completed!
📊 65 regressed and 1 fixed (964870 total)
📊 5391 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-156027/retry-regressed-list.txt

@craterbot craterbot removed the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jun 7, 2026
@craterbot craterbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 7, 2026
@rust-cloud-vms rust-cloud-vms Bot force-pushed the opaque-liveness branch 2 times, most recently from 0ecfc31 to 02dbabc Compare June 13, 2026 20:06
@rust-log-analyzer

This comment has been minimized.

@lqd

lqd commented Jun 15, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 15, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 15, 2026
Consider captured regions for opaque type region liveness.
@rust-bors

rust-bors Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 15410a9 (15410a9b8f02f49884fc00943ebd7e2fcc8eed78, parent: 5ff740e85e8569289a93573ad25f1295dfae0323)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (15410a9): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -0.5%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
1.4% [1.4%, 1.4%] 1
Improvements ✅
(primary)
-2.7% [-2.7%, -2.7%] 1
Improvements ✅
(secondary)
-1.7% [-1.7%, -1.7%] 1
All ❌✅ (primary) -0.5% [-2.7%, 1.7%] 2

Cycles

Results (primary 2.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Binary size

Results (primary 0.2%, secondary 0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.8%] 82
Regressions ❌
(secondary)
0.2% [0.0%, 0.8%] 49
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.0%, 0.8%] 82

Bootstrap: 518.77s -> 521.39s (0.51%)
Artifact size: 401.47 MiB -> 401.09 MiB (-0.10%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 15, 2026
@rust-bors

This comment has been minimized.

Comment on lines +10 to +11
// Test to show what happens if we were not careful and allowed invariant
// lifetimes to escape through a GAT.

@lcnr lcnr Jun 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why does the "invariant" matter? Could just as well be contravariant, couldn't it?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, potentially? Would be an interesting test case to get an contravariant lifetime through (since the &mut is going to force invariance).

This is a preexisting test anyways.

Comment thread tests/ui/borrowck/alias-liveness/gat-outlives-env.rs
Comment thread tests/ui/borrowck/alias-liveness/gat-outlives-implied.rs

@lcnr lcnr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mainly a bunch of nits. From reading https://hackmd.io/rvBf2GtiRGKqUVIdDd5g2g I am still left feeling unclear why polonius cares about liveness the way it does, what additional constraints this puts on liveness (e.g. cases where something has to outlive 'static are fine for liveness apparently).

The other question I have is whether you see this having benefits outside of this specific need of polonius? I think yes?

View changes since this review

Comment thread compiler/rustc_middle/src/query/keys.rs Outdated
/// (and so is `T`, since `T: 'static` implies `T: 'a`)
/// - `bar` outlives `'static`, so we know that no args are potentially live and we can return an empty set
/// - `baz` has no outlives bound, so return `None` and let the caller decide what to do
query live_args_for_alias_from_outlives_bounds(kind: ty::AliasTyKind<'tcx>) -> &'tcx Option<ty::EarlyBinder<'tcx, Vec<ty::GenericArg<'tcx>>>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

feel like as a query we want to just take a DefId and have a wrapper function that calls this query if necessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See https://github.com/rust-lang/rust/pull/156027/changes#diff-5d86c3fc35a603e984030459eb67d4870a975e53a460c331f15e48813ce8ce0eR40

The issue is that we search through the bounds on the item and need to match against the self type. Probably what this indicates is that item_bounds should potentially return something akin to ExistentialPredicate where there is no self type. But that's definitely out of scope for the current PR.

Comment on lines +63 to +70
// If any of the outlives bounds are `'static`, then we know the alias
// doesn't capture *any* regions, so we can skip visiting any regions at all.
//
// I was originally a bit concerned about something like `'a: 'static`, and
// whether or not we need to mark `'a` as live. I don't think *today* we do,
// since I think regions that outlive `'static` are special enough, but I
// *could* imagine some world where we need to be more careful about this.
// Given I can't find a test that goes wrong, I'm going to leave in this optimization.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does really hinge on "how do liveness and outlives requirements interact", which still feels unclear to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I adjusted this comment a bit. I feel pretty strongly positive that we're good here.

Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs
Comment on lines +387 to +397
// A free region (e.g. `for<'a> T::Assoc<'a, 'b>: 'b`): no higher-ranked
// arg of the alias can be proven (by a caller) to outlive a free region
// today, and pinned args only ever match universal regions (which are
// always live), so we conservatively treat this as giving no
// restriction.
//
// NB: if we ever get implied bounds inside binders, a bound var *could*
// be assumed to outlive a free region (e.g. `for<'a> T::Assoc<'x, 'a>: 'x`
// with a declared `'b: 'a` on the assoc type implies `'a: 'x` under the
// binder), so this would need revisiting -- though the result would
// still only need to include bound-var positions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

vaguely confused by this. I feel like we would want to already consider the implied bounds from free -> bound regions here? Or well, this feels very easy to miss with -Zassumptions-on-binders

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This comment just wasn't correct. The tl;dr is that we could consider fewer lifetimes as live with the free region as the outlived region. That would allow more code to compile that does today, so sticking with the conservative choice for now (i.e. don't restrict any arg liveness).

Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs Outdated
@jackh726

Copy link
Copy Markdown
Member Author

I am still left feeling unclear why polonius cares about liveness the way it does

Polonius cares about liveness because for an outlives relationship to be relevant at a given point, both regions must be live.

what additional constraints this puts on liveness (e.g. cases where something has to outlive 'static are fine for liveness apparently).

So, I think the key think about 'static (and any other free region) is that outlives requirements involving those must hold over the entire function.

The other question I have is whether you see this having benefits outside of this specific need of polonius? I think yes?

Yes, it does. This logic lets us consider few alias args as live outside borrowck too (which is needed to fix #42940). I have an (outdated) branch that does this: https://github.com/jackh726/rust/tree/opaque-liveness-42490

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

free region visitor for liveness marking regions dead and polonius alpha soundness

7 participants