Skip to content

Investigate mutating the AST during late resolution #99669

Open
@cjgillot

Description

@cjgillot

Late name resolution happens on the AST and is responsible for finding which definition each name in the AST points to, be it a type, trait expression, pattern, lifetime or label. The name "late" means that this resolution happens after all macros have been expanded, in opposition to "early" resolution which resolves macros. The corresponding code lives in rustc_resolve::late.

The results of this late resolution are stored in tables, and used by HIR lowering to directly embed the information in HIR.

Later, lowering reinterprets some of the AST where the syntax is ambiguous. For instance, lowering is responsible for making bare trait objects explicit: interpreting types of the form T with dyn T when resolution finds that T is a trait. Lowering also introduces implicit lifetime parameters for lifetime elision. The corresponding code lives in rustc_ast_lowering.

This re-interpretation of the AST needs to closely mirror what resolution is doing. Mismatches can cause bugs, like AST and HIR disagreeing on how to resolve lifetimes in the dyn T discussed above.

The objective of this project is to have this resolution-based desugaring in only one place. We would like to have late resolution modify the AST to directly perform these transformations, and remove them from lowering.

At the same time, lowering also performs its own transformations, like transforming impl Trait to generic parameters or opaque types. Those transformations should stay in lowering for the time being. Rule of thumb: if the transformation inspects resolutions, it should go in resolution, if it's purely syntactic it can stay in lowering.

Steps:

  • Refactor the visitor in rustc_resolve::late by an AST MutVisitor. MutVisitor is designed for macro expansions, so it may lack some methods to make resolution practical. There can be design work here, so feel free to suggest new methods / a different MutVisitor trait / ...
  • Desugar type-relative paths during resolution. The current code in lowering is in rustc_ast_lowering::path, and aims to transform a::b::c::d to <a::b::c>::d when a::b::c can be resolved to a type and ::d cannot (for instance because it is a trait method). See rustc_resolve::late::smart_resolve_path and associated methods.
  • Desugar bare trait object during late resolution when the path is actually a trait. See rustc_resolve::late::visit_ty.
  • Explicit elided lifetimes on the AST by inserting fresh lifetimes.

Please contact me on zulip (cjgillot) for any questions.

Note: this project is a large body of work, with possibly many changes to the resolution code. The point is to simplify the lowering code. If this simplification goal is not reached, or if the perf impact is prohibitive, we may end-up not merging the modifications, and instead document why.
To be merged, the PR will probably require a major change proposal. I will take care of this proposal once the perspectives become a bit clear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-HIRArea: The high-level intermediate representation (HIR)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.E-help-wantedCall for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions