Skip to content

Consider captured regions for opaque type region liveness.#156027

Open
jackh726 wants to merge 1 commit intorust-lang:mainfrom
jackh726:opaque-liveness
Open

Consider captured regions for opaque type region liveness.#156027
jackh726 wants to merge 1 commit intorust-lang:mainfrom
jackh726:opaque-liveness

Conversation

@jackh726
Copy link
Copy Markdown
Member

@jackh726 jackh726 commented May 1, 2026

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
Copy link
Copy Markdown
Member

lqd commented May 1, 2026

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
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 1, 2026

☀️ 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
Copy link
Copy Markdown
Member

@lqd lqd left a comment

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

desc { "listing captured lifetimes for opaque `{}`", tcx.def_path_str(def_id) }
}

query opaque_live_args(def_id: DefId) -> &'tcx ty::EarlyBinder<'tcx, Vec<ty::GenericArg<'tcx>>> {
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.

We should ensure the query is documented, and if not, add the doc comment from the fn here as well.

/// 2. If there are *any* outlives bounds, Then we find any args that outlive those bounds.
/// 3. If there are *no* outlives bounds, then we return all non-bivariant args, since they could potentially be relevant.
#[tracing::instrument(level = "debug", skip(tcx), ret)]
pub(crate) fn opaque_live_args<'tcx>(
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 name feels suboptimal

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.

}
rustc_hir::OpaqueTyOrigin::TyAlias { parent, .. } => (parent, IndexSet::default()),
};

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 section below could also use comments.

/// Given a known `param_env` and a set of well formed types, set up an
/// `InferCtxt`, call the passed function (to e.g. set up region constraints
/// to be tested), then resolve region and return errors
fn test_region_obligations<'tcx>(
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 only used once

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
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

5 participants