Skip to content
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

Remap impl-trait lifetimes on HIR instead of AST lowering #129383

Merged
merged 6 commits into from
Oct 31, 2024

Conversation

cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Aug 22, 2024

Current AST->HIR lowering goes out of its way to remap lifetimes for opaque types. This is complicated and leaks into upstream and downstream code.

This PR stops trying to be clever during lowering, and prefers to do this remapping during the HIR->ty lowering. The remapping computation easily fits into the bound var resolution code. Its result can be used in by generics_of and hir_ty_lowering::new_opaque to add the proper parameters and arguments.

See an example on the doc for query opaque_captured_lifetimes.

Based on #129244

Fixes #125249
Fixes #126850

cc @compiler-errors @spastorino
r? @petrochenkov

@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 22, 2024
@petrochenkov
Copy link
Contributor

Blocked on #129244.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 22, 2024
@cjgillot cjgillot force-pushed the opaque-noremap branch 2 times, most recently from 8127ba2 to d334955 Compare August 24, 2024 00:06
@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@cjgillot cjgillot force-pushed the opaque-noremap branch 2 times, most recently from c2ad831 to 12b94cd Compare October 5, 2024 11:52
@cjgillot cjgillot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Oct 5, 2024
@cjgillot cjgillot marked this pull request as ready for review October 5, 2024 11:53
@rustbot
Copy link
Collaborator

rustbot commented Oct 5, 2024

HIR ty lowering was modified

cc @fmease

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@bors

This comment was marked as resolved.

@bors
Copy link
Contributor

bors commented Oct 15, 2024

☔ The latest upstream changes (presumably #131724) made this pull request unmergeable. Please resolve the merge conflicts.

compiler/rustc_resolve/src/late.rs Outdated Show resolved Hide resolved
compiler/rustc_hir/src/hir.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Show resolved Hide resolved
@petrochenkov
Copy link
Contributor

Reviewed.
It would be better is someone more familiar with impl trait desugaring details looked at the compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs part.
@rustbot author

@rustbot rustbot 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 Oct 24, 2024
@cjgillot
Copy link
Contributor Author

@compiler-errors do you mind taking a look? I'm not sure I correctly implemented the logic around use bounds.

@compiler-errors
Copy link
Member

compiler-errors commented Oct 26, 2024

Sure. If I don't look at this in a day or two, please ping me again. Do you have any specific concerns, like any code that you are suspicious that may act differently than before or needs some input, or do you just want a general review?

@compiler-errors compiler-errors self-assigned this Oct 26, 2024
@rust-log-analyzer

This comment has been minimized.

//~^ ERROR ['_: o]
//~| ERROR ['_: o]
//~| ERROR `impl Trait` captures lifetime parameter
//~^ ERROR []
Copy link
Member

@compiler-errors compiler-errors Oct 26, 2024

Choose a reason for hiding this comment

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

Ah, this doesn't seem right. Or at least, it changes the semantics of what we mean by "capture all in-scope parameters" for nested opaques.

Without having yet looked at the code, I assume what's happening here is that the nested opaque doesn't see any nested lifetimes because of the use<> from the outer opaque, which shadows the lifetimes that the inner opaque considers to be in-scope. Is that right?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not actually certain if https://rust-lang.github.io/rfcs/3617-precise-capturing.html#summary prescribes what should happen here 🤔 -- it doesn't seem to do so, after having done a brief scan.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the code to recurse and capture all in-scope lifetimes when we have nested opaques.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@compiler-errors I have re-implemented the resolution to walk the scopes and not stop at the closest opaque. Is this closer to what you had in mind?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I believe so. If in the future we want opaque type captures to stop at the closest opaque, we can do so; that actually seems like the right behavior, but this should mean there are no observable changes.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

r=me,petrochenkov after rebase

@cjgillot
Copy link
Contributor Author

@bors r=compiler-errors,petrochenkov rollup=iffy

@bors
Copy link
Contributor

bors commented Oct 30, 2024

📌 Commit 2d74d8f has been approved by compiler-errors,petrochenkov

It is now in the queue for this repository.

@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 Oct 30, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 30, 2024
…kingjubilee

Rollup of 5 pull requests

Successful merges:

 - rust-lang#129383 (Remap impl-trait lifetimes on HIR instead of AST lowering)
 - rust-lang#132210 (rustdoc: make doctest span tweak a 2024 edition change)
 - rust-lang#132246 (Rename `rustc_abi::Abi` to `BackendRepr`)
 - rust-lang#132267 (force-recompile library changes on download-rustc="if-unchanged")
 - rust-lang#132344 (Merge `HostPolarity` and `BoundConstness`)

Failed merges:

 - rust-lang#132347 (Remove `ValueAnalysis` and `ValueAnalysisWrapper`.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6b60f03 into rust-lang:master Oct 31, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Oct 31, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 31, 2024
Rollup merge of rust-lang#129383 - cjgillot:opaque-noremap, r=compiler-errors,petrochenkov

Remap impl-trait lifetimes on HIR instead of AST lowering

Current AST->HIR lowering goes out of its way to remap lifetimes for opaque types. This is complicated and leaks into upstream and downstream code.

This PR stops trying to be clever during lowering, and prefers to do this remapping during the HIR->ty lowering. The remapping computation easily fits into the bound var resolution code. Its result can be used in by `generics_of` and `hir_ty_lowering::new_opaque` to add the proper parameters and arguments.

See an example on the doc for query `opaque_captured_lifetimes`.

Based on rust-lang#129244

Fixes rust-lang#125249
Fixes rust-lang#126850

cc `@compiler-errors` `@spastorino`
r? `@petrochenkov`
@cjgillot cjgillot deleted the opaque-noremap branch October 31, 2024 01:01
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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: no def-id for fresh lifetime ICE: assertion failed: unique
6 participants