Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,20 @@ impl<'db> SemanticsImpl<'db> {
&self,
path: &ast::Path,
) -> Option<(PathResolution<'db>, Option<GenericSubstitution<'db>>)> {
// The resolver uses the lexical scope for path qualifiers. Inferred
// expression and pattern resolutions apply to the final segment.
if path.parent_path().is_some() {
return self.analyze_no_infer(path.syntax())?.resolve_path(self.db, path);
}
// A lexical local shadows other values and has no substitution.
// Other value paths can still require inference for associated items.
if path.as_single_name_ref().is_some()
&& path.syntax().parent().is_some_and(|node| ast::PathExpr::can_cast(node.kind()))
&& let Some((resolution @ PathResolution::Local(_), _)) =
self.analyze_no_infer(path.syntax())?.resolve_path(self.db, path)
{
return Some((resolution, None));
}
self.analyze(path.syntax())?.resolve_path(self.db, path)
}

Expand Down Expand Up @@ -2074,7 +2088,9 @@ impl<'db> SemanticsImpl<'db> {
}

pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
self.analyze(pat.syntax())?.resolve_bind_pat_to_const(self.db, pat)
// The lowered pattern and lexical value namespace determine whether a
// bare identifier is a constant or a binding.
self.analyze_no_infer(pat.syntax())?.resolve_bind_pat_to_const(self.db, pat)
}

pub fn record_literal_missing_fields(
Expand Down
19 changes: 19 additions & 0 deletions crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,17 @@ impl<'a, 'db> FindUsages<'a, 'db> {
name_ref: &ast::NameRef,
sink: &mut dyn FnMut(EditionedFileId, FileReference) -> bool,
) -> bool {
// Module names in references occur as paths. Skip fields, method
// names, and shorthand bindings before their type-dependent lookup.
if matches!(self.def, Definition::Module(_))
&& !name_ref
.syntax()
.parent()
.is_some_and(|node| ast::PathSegment::can_cast(node.kind()))
{
cov_mark::hit!(skip_non_path_module_reference_candidate);
return false;
}
if self.is_excluded_name_ref(name_ref) {
return false;
}
Expand Down Expand Up @@ -1321,6 +1332,14 @@ impl<'a, 'db> FindUsages<'a, 'db> {
name: &ast::Name,
sink: &mut dyn FnMut(EditionedFileId, FileReference) -> bool,
) -> bool {
// Identifier patterns bind a local or refer to a value, so they
// cannot reference a module.
if matches!(self.def, Definition::Module(_))
&& name.syntax().parent().is_some_and(|node| ast::IdentPat::can_cast(node.kind()))
{
cov_mark::hit!(skip_binding_module_reference_candidate);
return false;
}
match NameClass::classify(self.sema, name) {
Some(NameClass::PatFieldShorthand { local_def: _, field_ref, adt_subst: _ })
if matches!(
Expand Down
15 changes: 14 additions & 1 deletion crates/ide/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ fn f() {

#[test]
fn test_find_all_refs_decl_module() {
cov_mark::check!(skip_non_path_module_reference_candidate);
cov_mark::check!(skip_binding_module_reference_candidate);
check(
r#"
//- /lib.rs
Expand All @@ -1196,7 +1198,17 @@ mod foo$0;
use foo::Foo;

fn f() {
let i = Foo { n: 5 };
let foo = foo::Foo { n: 5 };
let renamed = foo;
let same_name = Other { foo: 0 };
let _ = same_name.foo;
same_name.foo();
}

struct Other { foo: u32 }

impl Other {
fn foo(&self) {}
}

//- /foo.rs
Expand All @@ -1208,6 +1220,7 @@ pub struct Foo {
foo Module FileId(0) 0..8 4..7

FileId(0) 14..17 import
FileId(0) 48..51
"#]],
);
}
Expand Down
Loading