rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.93k stars 12.53k forks source link

Investigate mutating the AST during late resolution #99669

Open cjgillot opened 2 years ago

cjgillot commented 2 years ago

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:

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.

petrochenkov commented 2 years ago

I'm not sure whether it's a good idea in general, but the implementation/prototyping here can be simplified by splitting this job into two steps:

petrochenkov commented 2 years ago

@cjgillot How does all this map on incremental compilation changes you've been planning to do?

vincenzopalazzo commented 2 years ago

I'm taking a lock on it because its feet on my learning path! and see the evolution of the discussion in the meanwhile

@rustbot claim

Noratrieb commented 1 year ago

@vincenzopalazzo are you still working on this?

vincenzopalazzo commented 1 year ago

@Nilstrieb it is in my todo list my I had some other priority, so I unassign me if you had any other good target to assign this

jon-chuang commented 1 year ago

@rustbot claim

Hi, I'd like to try this out.

kostekIV commented 1 year ago

Hi @jon-chuang are you working on this? And @cjgillot is this still a relevant issue?

jon-chuang commented 1 year ago

Hello, not anymore

cjgillot commented 1 year ago

This issue is still relevant.

kostekIV commented 1 year ago

cool, I will work on this then.

axelmagn commented 8 months ago

I'm picking this issue up after a conversation with @cjgillot.

@rustbot claim

axelmagn commented 6 months ago

Stepping down from this issue -- the complexity is a bit much for me right now, and my priorities have shifted.

@rustbot release-assignment

petrochenkov commented 6 months ago

Having a pre-lowering pass working on AST may be useful for implementing delegation (https://github.com/rust-lang/rust/issues/118212). The part that does this transformation in particular.

impl Foo { // id_foo
    reuse Trait::{a, b, c}; // id_reuse
}

=>

impl Foo { // id_foo
    reuse Trait::a; // id_a
    reuse Trait::b; // id_b
    reuse Trait::c; // id_c
}

Then AST lowering will turn the reuse items into proper fn items.

With the current lowering we immediately go inside the id_reuse owner node when processing an (associated) item. But I want to generate the delegated fn items immediately under the id_foo parent, and next to id_reuse, because a lot of code in the compiler relies on an impl being a direct parent of its impl items.

Not sure how to do it better without a pre-lowering pass.

UPD: Design for this specific question about delegation - https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2020869823.