Skip to content

delegation: refactor AST -> HIR lowering#158434

Draft
aerooneqq wants to merge 8 commits into
rust-lang:mainfrom
aerooneqq:delegation-refactoring
Draft

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

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

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

rust-bors Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

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

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

Labels

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