Refactor import resolution (CrateDefMap
)
#5922
Labels
A-nameres
name, path and module resolution
C-Architecture
Big architectural things which we need to figure up-front (or suggestions for rewrites :0) )
E-hard
S-actionable
Someone could pick this issue up and work on it right now
Problem
On of the larger and most important phases of rust-analyzer is
CrateDefMap
construction --- for each module, rust-analyzer determines the set of items defined & visible in the modules. This requires resolving imports, processing globs & expanding macros. The bulk of the current implementation lives here:https://github.com/rust-analyzer/rust-analyzer/blob/753af410056937d7be3a1b3457c834a887288957/crates/hir_def/src/nameres/collector.rs
There are two big problems with code in question:
fn foo() { use Bar::*; ... }
) in a principled way.I think we first should tackle the first problem (basically, refactor the thing in place until it makes sense), and then attack the second one.
Broadly, what we want is some kind of "name resolution IR", a-la chalk, which allows us to capture the essence of name resolution.
Constraints
The rough constraints for the final solution are as follows:
CrateDefMap
-like thingsProposed solution
I think the best thing we can do is to extract the fixed point iteration logic elswhere, as an IR. This IR will represent a bunch of scopes (modules) which contains names and links between scopes (glob import). The primitive operation of the IR is adding a name to the module (which should backpropagate via links) or adding a link (which similarly should propagate all the names). This thing needs to know about namespaces, but it doesn't need to know what
def
s are (we probably can represent them as just indices) and it also doesn't need to know about imports & macros (ie, I think results of import resolution and macro expansion could be injected from the outside). Here's a very rough and incomplete sketch of the API:https://gist.github.com/matklad/145c88131282e8392e64d088ee8a50e4
The text was updated successfully, but these errors were encountered: