Skip to content

Commit 99ace2e

Browse files
fix: avoid shadow local symbol from completions
1 parent 1b90e97 commit 99ace2e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,28 @@ enum Foo {
14581458
)
14591459
}
14601460

1461+
#[test]
1462+
fn flyimport_enum_variant_not_shadow_by_fn() {
1463+
check(r#"
1464+
//- /std.rs crate:std
1465+
pub enum Result<T, E> {
1466+
Ok(T),
1467+
Err(E)
1468+
}
1469+
1470+
//- /dep.rs crate:dep deps:std
1471+
use std::Result;
1472+
pub fn Ok<T>(t: T) -> Result<T, ()> {}
1473+
1474+
//- /main.rs crate:main deps:std,dep
1475+
use std::{Result, Result::Ok};
1476+
fn main() -> Result<(), ()> {
1477+
Ok$0
1478+
}
1479+
"#,
1480+
expect![[r#""#]])
1481+
}
1482+
14611483
#[test]
14621484
fn flyimport_attribute() {
14631485
check(

crates/ide-db/src/imports/import_assets.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,35 @@ impl ImportAssets {
281281
match &self.import_candidate {
282282
ImportCandidate::Path(path_candidate) => {
283283
path_applicable_imports(sema, krate, path_candidate, mod_path, |item_to_import| {
284+
let import_def = match item_to_import {
285+
ItemInNs::Types(def) => def,
286+
ItemInNs::Values(def) => def,
287+
ItemInNs::Macros(def) => ModuleDef::Macro(def),
288+
};
289+
fn will_shadow(db: &dyn HirDatabase, cur: ModuleDef, other: ModuleDef) -> bool {
290+
match (cur, other) {
291+
(ModuleDef::Module(a), ModuleDef::Module(b)) => a.name(db) == b.name(db),
292+
(ModuleDef::Function(a), ModuleDef::Function(b)) => a.name(db) == b.name(db),
293+
(ModuleDef::Adt(a), ModuleDef::Adt(b)) => a.name(db) == b.name(db),
294+
(ModuleDef::Variant(a), ModuleDef::Variant(b)) => a.name(db) == b.name(db),
295+
(ModuleDef::Variant(a), ModuleDef::Function(b)) => a.name(db) == b.name(db),
296+
(ModuleDef::Const(a), ModuleDef::Const(b)) => a.name(db) == b.name(db),
297+
(ModuleDef::Static(a), ModuleDef::Static(b)) => a.name(db) == b.name(db),
298+
(ModuleDef::Trait(a), ModuleDef::Trait(b)) => a.name(db) == b.name(db),
299+
(ModuleDef::TraitAlias(a), ModuleDef::TraitAlias(b)) => a.name(db) == b.name(db),
300+
(ModuleDef::TypeAlias(a), ModuleDef::TypeAlias(b)) => a.name(db) == b.name(db),
301+
(ModuleDef::Macro(a), ModuleDef::Macro(b)) => a.name(db) == b.name(db),
302+
(_, _) => false,
303+
}
304+
}
305+
284306
!scope_definitions.contains(&ScopeDef::from(item_to_import))
307+
&& scope_definitions.iter().find(|&&scope_def| match scope_def {
308+
ScopeDef::ModuleDef(module_def) => {
309+
will_shadow(sema.db, module_def, import_def)
310+
},
311+
_ => false
312+
}).is_none()
285313
})
286314
}
287315
ImportCandidate::TraitAssocItem(trait_candidate)

0 commit comments

Comments
 (0)