-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support local imports (use statements in blocks) #1165
Comments
Yeah, Seem like current
An here is a simplifed test case for this bug:
|
cc @pnkfelix, supporting local items (\w imports) and laziness is going to be a major design constarint on the name resolution library. The current code in rust-analyzer just completely ignores local items and builds a Resolver instance for top-level scope. We can ask the Resolver to process item bodies at the same time as it processes modules, but that would kill laziness. Instead, I hope that for each function we can create a separate Resolver instance on-demand which references the parent resolver. That is, each function gets its own independent resolver, but they all share the same module-level scope |
Is this planned to be implemented (yet)? |
This is not planned for the nearest future |
Just avoided creating another duplicate of this... Given the amount of refs and duplicates this looks like top priority. |
|
I guess the answer is "both" |
Any |
If RA desugars each function with local imports into a module with |
@MikailBag you can have use statements at |
It's not that simple, the anonymous modules created by block-level items work differently from named modules (in that everything from the containing named module stays in scope). |
Also, the main complication here is not actually implementing the logic, but doing it in a way that doesn't require us to parse all function bodies all the time. |
This will produce significant large amount of modules, which is no good. |
Yeah, the #5922 is still how I think we should proceed here. Basically, we first should refactor existing code to be more modular, and than we should add support for local modules. PRs welcome, but be warned that this is a large task. I'd estimate it as 1-2 weeks full-time for someone who is already familiar with the relevant infra. At the same time, I wouldn't worry to much about "core design" here. I think both of these are true:
Also, +1 to this comment regarding the language :-) |
At the risk of dragging this on more, I just want to make the point that from the perspective of this particular user, false positive compilation errors would have the most positive impact on my experience using RA -- substantially more than refactoring tools, search & replace tools, further performance improvements or more code sharing. Of course this disregards the experience of RA contributors and wider ecosystem benefits from things like code sharing, but I feel like it should count for something. I don't know if/how RA prioritizes work currently -- making that more transparent might also help in the sense of making it more concrete what trade-offs are affecting issues like this. Also, this is the kind of thing where I wonder if a focused fundraising effort could help -- I for one would gladly contribute some cash to help prioritize this, but I don't think my one-man business can currently sustain paying for the entirety of 1-2 person-weeks of work. |
For wording, my initial intention was to bring some "ping"-like comments so there can be some discussion. This issue is so important but is only widely mentioned, and is on page 3 when sorting issues with "most discussed". I apologize for the moronic words though, I have made some attempts on trying to make this work (like introducing block-modules) before, but they turns out to be not good design, so I'm aware of the difficulty of the task. Thanks for everyone who have made RA so great! |
Fixes bevyengine#348 Changes to uses fully qualified names to work around rust-lang/rust-analyzer#1165
7336: Rename `CrateDefMap` to `DefMap` r=matklad a=jonas-schievink I propose handling local items by computing a `DefMap` for every block expression, using the regular (early) name resolution algorithm. The result of that will be a `DefMap` that has a reference to the parent `DefMap`, which is either the one computed for the containing block expression, or the crate's root `DefMap`. Name resolution will fall back to a name in the parent `DefMap` if it cannot be resolved in the inner block. The `DefMap`s computed for block expressions will go through a separate query that can be garbage-collected much more aggressively, since these `DefMap`s should be cheap to compute and are never part of a crate's public API. The first step towards that is to make `CrateDefMap` not specific to crates anymore, hence this rename (if this plans sounds reasonable). cc #7325 and #1165 Co-authored-by: Jonas Schievink <[email protected]>
I'd just like to chime in with some motivation on why I think supporting local Increasingly I only put a In short: just like I try to limit the scope of my variables, I try to limit the scope of my |
It's so beautiful... Thanks @jonas-schievink ! this has been my most-desired missing feature in rust-analyzer for such a long time! |
@jonas-schievink woo! Thanks so much for picking this up. |
Inside a function, I often write something like this:
But RA seems to have a problem with such imports:
The text was updated successfully, but these errors were encountered: