Skip to content

delegation: refactor AST -> HIR lowering#158434

Open
aerooneqq wants to merge 15 commits into
rust-lang:mainfrom
aerooneqq:delegation-refactoring
Open

delegation: refactor AST -> HIR lowering#158434
aerooneqq wants to merge 15 commits into
rust-lang:mainfrom
aerooneqq:delegation-refactoring

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

View all comments

After a lot of new features were added to delegation the code became a bit worse, as resolution and lowering logic is mixed in delegation.rs. In this file the output of resolution step of delegation is transferred as function params (in some functions up to 7 or 8 params), which is not good looking for me.

As I mentioned in the previous paragraph delegation AST -> HIR lowering can be split into two phases:

  1. Resolution - we collect all information about signature, signature parent context, delegation parent, delegation generics. Then this information is used in AST -> HIR lowering,
  2. AST -> HIR lowering - we generate everything based on the output of resolution.

This PR explicitly splits those two phases: as a first step we collect all needed information into DelegationResolution struct and then use it during AST -> HIR lowering. The delegation.rs file is moved into delegation folder and renamed to mod.rs, the final file structure is as follows:

  • mod.rs - contains lowering logic for everything but generics,
  • generics.rs - contains logic for resolution and uplifting of generic params, placed in a separate file as it is a significant and independent part of lowering,
  • attributes.rs - contains relatively small piece of logic which connects to attributes inheritance,
  • resolution.rs - contains logic for resolution of everything but generics.

As resolution of delegation and generics logically does not depend on LoweringContext, as from its name LoweringContext contains many additional information which is used for lowering, I've created LoweringContextForResolution trait which abstracts operations which are used during delegation resolution.

Benefits of this PR:

  • Resolution information is grouped into single struct, is extracted from AST -> HIR lowering which leads to better transfer of resolution across lowering (through a single struct, not through many arguments) and enhanced understanding of which information we use for delegation lowering,
  • There is now a single path for generating error delegation - if an error was returned from delegation resolution,
  • Refactorings of uplift_delegation_generics which was creating generic params for delegation made the code more understandable.

Part of #118212.
r? @petrochenkov

@rustbot rustbot added 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. labels Jun 26, 2026
@aerooneqq

aerooneqq commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Now this PR is based on #158397, so it is draft until #158397 is merged, but feedback is anyway welcomed.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the delegation-refactoring branch from d6dd2be to c2d7c9c Compare June 29, 2026 12:03
@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the delegation-refactoring branch from ae9c536 to faa250b Compare July 1, 2026 06:56
@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the delegation-refactoring branch from faa250b to 002fc94 Compare July 2, 2026 05:42
@aerooneqq aerooneqq marked this pull request as ready for review July 2, 2026 05:45
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2026
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Jul 2, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

Could you split this into 2 commits - one moving code around (to different files, or within files), and the second one doing the code changes/refactorings?
@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 2, 2026
@rustbot

rustbot commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 2, 2026
@aerooneqq aerooneqq force-pushed the delegation-refactoring branch from 67f05b8 to 2196005 Compare July 2, 2026 11:11
@aerooneqq

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2026
Comment thread compiler/rustc_ast_lowering/src/delegation/attributes.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/resolution.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/resolution.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/mod.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/mod.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/mod.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/generics.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/generics.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation/generics.rs Outdated
@petrochenkov petrochenkov 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 Jul 2, 2026
@aerooneqq

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2026
Comment thread compiler/rustc_ast_lowering/src/delegation/resolution.rs
/// Abstracts operations that are needed for delegation's resolution, so resolution
/// is independent of `LoweringContext`. Placed in a separate module so `LoweringContext`
/// can not be accessed directly.
pub(crate) struct DelegationResolverWrapper<'a, 'hir>(&'a LoweringContext<'a, 'hir>);

@petrochenkov petrochenkov Jul 2, 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.

Suggested change
pub(crate) struct DelegationResolverWrapper<'a, 'hir>(&'a LoweringContext<'a, 'hir>);
pub(crate) struct DelegationResolver<'a, 'hir>(&'a LoweringContext<'a, 'hir>);

There's no naming ambiguity with the trait anymore.

View changes since the review

}

pub(super) fn check_for_cycles(
impl<'tcx> wrapper::DelegationResolverWrapper<'_, 'tcx> {

@petrochenkov petrochenkov Jul 2, 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.

Suggested change
impl<'tcx> wrapper::DelegationResolverWrapper<'_, 'tcx> {
impl<'tcx> DelegationResolver<'_, 'tcx> {

Import, here and in one more place.

View changes since the review

(!args.args.is_empty()).then(|| args)
}

pub(super) fn resolve_generics(

@petrochenkov petrochenkov Jul 2, 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.

Suggested change
pub(super) fn resolve_generics(
pub(super) fn resolve_and_generate_generics(

Because we produce GenericsGenerationResults here and not GenericsResolution.
create_generics_resolution can then be renamed to resolve_generics.

View changes since the review

@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after addressing the remaining comments and squashing the refactoring commits.
@rustbot author
@bors delegate+

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 2, 2026
@rust-bors

rust-bors Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

✌️ @aerooneqq, you can now approve this pull request!

If @petrochenkov told you to "r=me" after making some further change, then please make that change and post @bors r=petrochenkov.

View changes since this delegation.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 2, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
...............................F.................. (50/144)
.................................................. (100/144)
............................................       (144/144)

======== tests/rustdoc-gui/go-to-collapsed-elem.goml ========

[ERROR] line 40
    at `tests/rustdoc-gui/go-to-collapsed-elem.goml` line 21: Error: Node is detached from document: for command `click: "//*[@id='search']//a[@href='../test_docs/struct.Foo.html#method.must_use']"`
    at <file:///checkout/obj/build/x86_64-unknown-linux-gnu/test/rustdoc-gui/doc/test_docs/struct.Foo.html?search=t_use>


<= doc-ui tests done: 143 succeeded, 1 failed, 0 filtered out

Error: ()

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

Labels

F-fn_delegation `#![feature(fn_delegation)]` 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.

4 participants