Skip to content

Make sure to account for the right item universal regions in borrowck #144666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jul 30, 2025

Fixes #144608.

The ICE comes from a mismatch between the liberated late bound regions (i.e. "ReLateParam"s) that come from promoting closure outlives, and the regions we have in our region vid mapping from UniversalRegions.

When building UniversalRegions, we end up using the liberated regions from the binder of the closure's signature:

for (idx, bound_var) in bound_inputs_and_output.bound_vars().iter().enumerate() {

Notably, this signature may be anonymized if the closure signature being deduced comes from an external constraints:

// `deduce_expectations_from_expected_type` introduces
// late-bound lifetimes defined elsewhere, which we now
// anonymize away, so as not to confuse the user.
let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);

This is true in the test file I committed, where the signature is influenced by the impl FnMut(&mut ()) RPIT.

However, when promoting a type outlives constraint we end up creating a late bound lifetime mapping that disagrees with those liberated late bound regions we constructed in UniversalRegions:

for_each_late_bound_region_in_recursive_scope(tcx, tcx.local_parent(closure_def_id), |r| {

Specifically, in for_each_late_bound_region_in_item (which is called by for_each_late_bound_region_in_recursive_scope), we were using tcx.late_bound_vars which uses the late bound regions from the HIR. This query both undercounts the late bound regions (e.g. those that end up being deduced from bounds), and also doesn't account for the fact that we anonymize them in the signature as mentioned above.

tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)).iter().enumerate()

This PR fixes that function to use the correct signature, which properly considers the bound vars that come from deducing the signature of the closure, and which comes from the closure's args from the type_of query.

@rustbot
Copy link
Collaborator

rustbot commented Jul 30, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Jul 30, 2025
@lqd
Copy link
Member

lqd commented Jul 30, 2025

chef's kiss at the PR description.

r? lqd @bors r+

@bors
Copy link
Collaborator

bors commented Jul 30, 2025

📌 Commit 98d08ff has been approved by lqd

It is now in the queue for this repository.

@rustbot rustbot assigned lqd and unassigned davidtwco Jul 30, 2025
@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2025
bors added a commit that referenced this pull request Jul 30, 2025
Rollup of 6 pull requests

Successful merges:

 - #144042 (Verify llvm-needs-components are not empty and match the --target value)
 - #144268 (Add method `find_ancestor_not_from_macro` and `find_ancestor_not_from_extern_macro` to supersede `find_oldest_ancestor_in_same_ctxt`)
 - #144411 (Remove `hello_world` directory)
 - #144662 (compiletest: Move directive names back into a separate file)
 - #144666 (Make sure to account for the right item universal regions in borrowck)
 - #144668 ([test][run-make] add needs-llvm-components)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 08e26fc into rust-lang:master Jul 30, 2025
10 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 30, 2025
rust-timer added a commit that referenced this pull request Jul 30, 2025
Rollup merge of #144666 - compiler-errors:correct-late, r=lqd

Make sure to account for the right item universal regions in borrowck

Fixes #144608.

The ICE comes from a mismatch between the liberated late bound regions (i.e. "`ReLateParam`"s) that come from promoting closure outlives, and the regions we have in our region vid mapping from `UniversalRegions`.

When building `UniversalRegions`, we end up using the liberated regions from the binder of the closure's signature:

https://github.com/rust-lang/rust/blob/c8bb4e8a126cf38cff70cea488a3a423a5321954/compiler/rustc_borrowck/src/universal_regions.rs#L521

Notably, this signature may be anonymized if the closure signature being deduced comes from an external constraints:

https://github.com/rust-lang/rust/blob/c8bb4e8a126cf38cff70cea488a3a423a5321954/compiler/rustc_hir_typeck/src/closure.rs#L759-L762

This is true in the test file I committed, where the signature is influenced by the `impl FnMut(&mut ())` RPIT.

However, when promoting a type outlives constraint we end up creating a late bound lifetime mapping that disagrees with those liberated late bound regions we constructed in `UniversalRegions`:

https://github.com/rust-lang/rust/blob/c8bb4e8a126cf38cff70cea488a3a423a5321954/compiler/rustc_borrowck/src/universal_regions.rs#L299

Specifically, in `for_each_late_bound_region_in_item` (which is called by `for_each_late_bound_region_in_recursive_scope`), we were using `tcx.late_bound_vars` which uses the late bound regions *from the HIR*. This query both undercounts the late bound regions (e.g. those that end up being deduced from bounds), and also doesn't account for the fact that we anonymize them in the signature as mentioned above.

https://github.com/rust-lang/rust/blob/c8bb4e8a126cf38cff70cea488a3a423a5321954/compiler/rustc_borrowck/src/universal_regions.rs#L977

This PR fixes that function to use the *correct signature*, which properly considers the bound vars that come from deducing the signature of the closure, and which comes from the closure's args from the `type_of` query.
@compiler-errors compiler-errors deleted the correct-late branch July 30, 2025 14:47
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jul 31, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#144042 (Verify llvm-needs-components are not empty and match the --target value)
 - rust-lang/rust#144268 (Add method `find_ancestor_not_from_macro` and `find_ancestor_not_from_extern_macro` to supersede `find_oldest_ancestor_in_same_ctxt`)
 - rust-lang/rust#144411 (Remove `hello_world` directory)
 - rust-lang/rust#144662 (compiletest: Move directive names back into a separate file)
 - rust-lang/rust#144666 (Make sure to account for the right item universal regions in borrowck)
 - rust-lang/rust#144668 ([test][run-make] add needs-llvm-components)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

[ICE][ICE->Error] Explicit naming of parameter leads to ICE
5 participants