-
Notifications
You must be signed in to change notification settings - Fork 1.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
fix: if item exsits on module, resolve as module instead of type #19088
fix: if item exsits on module, resolve as module instead of type #19088
Conversation
This comment has been minimized.
This comment has been minimized.
cf71962
to
9568a66
Compare
crates/hir/src/source_analyzer.rs
Outdated
Some(it) | ||
if matches!( | ||
it, | ||
PathResolution::Def(ModuleDef::Adt(_) | ModuleDef::BuiltinType(_)) |
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.
It cannot be an ADT (there will be an error - https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=66662b72d42109fcca7af9a68de608d7), only a primitive type can be shadowed with the same name and different resolutions
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.
Now I see that we don't resolve them correctly even in the case of a module shadowing a struct (I haven't checked why), but this is a different issue - we should always resolve to the module in this case (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=960405ae19d2bdd97128fb38e364ba08).
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.
Thank you for comment.
Since defining an Adt and a module in the same scope causes an error, I fixed it by removing the check for whether an item exists in the module when there is a duplicate.
remove_only_std.str.mov
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 I looked into this, and I think this isn't the correct approach. Instead, we need to do the following: resolve_hir_path_qualifier()
currently tries the module path last (
rust-analyzer/crates/hir/src/source_analyzer.rs
Line 1571 in 208bc52
.resolve_module_path_in_items(db.upcast(), path.mod_path()?) |
str
the type and str
the module are resolved correctly).
As an unrelated note, can you please move the test to the goto definition feature? remove_unused_imports will eventually switch to a proper approach that requests the unused imports from analysis, and it'll be a shame to lose these tests in the way (also I believe most resolve_path()
tests are there).
No. Wait. Even what I suggested won't solve the problem, because it will prefer the module even when the struct should be preferred, like the following: mod foo {
pub fn bar() {}
}
fn main() {
struct foo;
impl foo {
pub fn bar() {}
}
foo::bar();
} Something else is needed. I'll think about it more (it'd be perfect if we could solve the full path, and during the process record which way we went - but this will be too costly. Also, I've discovered that even our resolution is not perfect, in https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ae501f2aa9e9b987a35d5cfb50146641 we should fail to resolve but we instead fall back to the associate fn |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
aed7d07
to
f9eebaf
Compare
I think this function can resolve if those item is only rust-analyzer/crates/hir/src/source_analyzer.rs Line 1571 in 208bc52
When I try it first, this mod foo {
pub fn bar() {}
}
fn main() {
struct foo;
impl foo {
pub fn bar() {}
}
foo::bar();
} but I think trait Foo {
fn fn_foo();
}
mod bar {
pub mod Bar {
// ^^^
pub fn fn_bar() {}
}
}
fn qux<Bar: Foo>() {
// ^^^ should find mod bar::Bar
use bar::Bar;
Bar::fn_bar();
} |
Sorry, but your comment is unclear. Are you claiming that swapping the order is enough? |
Sorry for the confusion. |
But you can't know if the path conflicts with a parameter, or a struct, or whatever. I think what we need to do is this: the problem is that rust-analyzer/crates/hir-def/src/resolver.rs Lines 1017 to 1056 in 37355b3
This is incorrect. If we resolve to such item, that means we have no resolution, but we instead continue to search in outer scopes. This is also what leads to IDE resolution to fail here, because when resolving only part of the path these resolutions can be correct. I think we need to remove this filtering, and instead let downstream (analysis, not IDE) err when the resolution is unexpected. IDE should just resolve to them. Anyway I think we need to separate that from this PR. Make this one focus on builtin shadowing, and if you want make a new PR to solve shadowing of other types. |
Thank you for your advice. At another time, I'll take a look at those functions and try to solve the shadowing of other types. |
f9eebaf
to
1d87b5a
Compare
Signed-off-by: Hayashi Mikihiro <[email protected]>
1d87b5a
to
c84cec1
Compare
Thanks! |
fix : #18941, #19107
This PR resolves modules if items exist in the module and treats them as modules.
duplicate-str-module.mov
If a type import overwrites a module, this PR ensures it is resolved as a type
duplicate_with_module_struct.mov