-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
resolve: improve import resolution #31461
Conversation
828bd48
to
6a2f81e
Compare
6a2f81e
to
eb1a9b6
Compare
The second one was supposed to be #31404, edited.
Awesome, I look forward to any feedback. |
Ok, I give up, too many things are happening here at the same time. By the way, how much of the code treating imports and extern crates specially is caused only by error message compatibility? |
// Define the name or return the existing binding if there is a collision. | ||
fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> { | ||
let old_binding = match self.binding { | ||
Some(_) if binding.defined_with(DefModifiers::SHADOWABLE) => return Ok(()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if we try to define a shadowable binding (1) conflicting with another existing shadowable binding (2), then (2) wins or the conflict is reported somewhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, binding (2) would win (since the first match arm would be taken) and the conflict would not be reported (it would also not be reported in the original code). This isn't a problem since the only SHADOWABLE
bindings are from the special prelude import, so there can't be conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I should rename SHADOWABLE to PRELUDE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I should rename SHADOWABLE to PRELUDE.
That would be much clearer, because prelude and shadowable glob imports would behave differently (I suppose).
The resolution result depending on the resolution order is still pretty bad, even if the concern is theoretical at the moment due to absence of user-defined preludes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I'll make it an error to have duplicate SHADOWABLE / PRELUDE imports.
I'll split the first commit into more smaller commits.
The special treatment in |
eb1a9b6
to
fe6ecd7
Compare
I addressed @gereeter's and @petrochenkov's comments and rewrote history to make the biggest commit a little cleaner and easier to review. |
85afd8e
to
a847317
Compare
NameBinding now encodes these directly with binding.is_public() and (binding.is_public() && binding.is_import()) (respectively)
…ame ImportResolution to NameResolution
…tanding_references_for
…valued map. Refactor away resolve_name_in_module in resolve_imports.rs Rewrite and improve the core name resolution procedure in NameResolution::result and Module::resolve_name Refactor the duplicate checking code into NameResolution::try_define
a847317
to
3c62d90
Compare
Reviewed everything except for jseyfried@7000e70 |
69635f0
to
3c62d90
Compare
…sts) Derive the Default impl for NameResolution
1e71fc0
to
3df40c0
Compare
@bors: r+ |
📌 Commit 3df40c0 has been approved by |
This PR adds to `NameBinding` so it can more fully represent bindings from imports as well from items, refactors away `Target`, generalizes `ImportResolution` to a simpler type `NameResolution`, and uses a single `NameResolution`-valued map in place the existing maps `children` and `import_resolutions` (of `NameBinding`s and `ImportResolution`s, respectively), simplifying duplicate checking and name resolution. It also unifies the `resolve_name_in_module` in `lib.rs` with its namesake in `resolve_imports.rs`, clarifying and improving the core logic (fixes #31403 and fixes #31404) while maintaining clear future-comparability with shadowable globs (i.e., never reporting that a resolution is a `Success` or is `Failing` unless this would also be knowable with shadowable globs). Since it fixes #31403, this is technically a [breaking-change], but it is exceedingly unlikely to cause breakage in practice. The following is an example of code that would break: ```rust mod foo { pub mod bar {} // This defines bar in the type namespace pub use alpha::bar; // This defines bar in the value namespace // This should define baz in both namespaces, but it only defines baz in the type namespace. pub use self::bar as baz; pub fn baz() {} // This should collide with baz, but now it does not. } pub fn f() {} mod alpha { pub use self::f as bar; // Changing this to `pub fn bar() {}` causes the collision right now. pub use super::*; } ``` r? @nrc
@nrc Thanks! |
…d to catch it.
This PR adds to
NameBinding
so it can more fully represent bindings from imports as well from items, refactors awayTarget
, generalizesImportResolution
to a simpler typeNameResolution
, and uses a singleNameResolution
-valued map in place the existing mapschildren
andimport_resolutions
(ofNameBinding
s andImportResolution
s, respectively), simplifying duplicate checking and name resolution.It also unifies the
resolve_name_in_module
inlib.rs
with its namesake inresolve_imports.rs
, clarifying and improving the core logic (fixes #31403 and fixes #31404) while maintaining clear future-comparability with shadowable globs (i.e., never reporting that a resolution is aSuccess
or isFailing
unless this would also be knowable with shadowable globs).Since it fixes #31403, this is technically a [breaking-change], but it is exceedingly unlikely to cause breakage in practice. The following is an example of code that would break:
r? @nrc