From 4e4c9ea4ac9827c3e6b099c262418ba172910df3 Mon Sep 17 00:00:00 2001 From: unexge Date: Tue, 22 Mar 2022 22:30:29 +0100 Subject: [PATCH] fix: Correctly suggest auto importing traits from aliases --- crates/ide_completion/src/tests/flyimport.rs | 41 ++++++++++++++++++++ crates/ide_db/src/imports/import_assets.rs | 11 ++++++ 2 files changed, 52 insertions(+) diff --git a/crates/ide_completion/src/tests/flyimport.rs b/crates/ide_completion/src/tests/flyimport.rs index 4a0fcf34429..b47a7100065 100644 --- a/crates/ide_completion/src/tests/flyimport.rs +++ b/crates/ide_completion/src/tests/flyimport.rs @@ -297,6 +297,47 @@ fn main() { ); } +#[test] +fn trait_method_from_alias() { + let fixture = r#" +//- /lib.rs crate:dep +pub mod test_mod { + pub trait TestTrait { + fn random_method(); + } + pub struct TestStruct {} + impl TestTrait for TestStruct { + fn random_method() {} + } + pub type TestAlias = TestStruct; +} + +//- /main.rs crate:main deps:dep +fn main() { + dep::test_mod::TestAlias::ran$0 +} +"#; + + check( + fixture, + expect![[r#" + fn random_method() (use dep::test_mod::TestTrait) fn() + "#]], + ); + + check_edit( + "random_method", + fixture, + r#" +use dep::test_mod::TestTrait; + +fn main() { + dep::test_mod::TestAlias::random_method()$0 +} +"#, + ); +} + #[test] fn no_trait_type_fuzzy_completion() { check( diff --git a/crates/ide_db/src/imports/import_assets.rs b/crates/ide_db/src/imports/import_assets.rs index 3963d4d79a8..d6246c7ad64 100644 --- a/crates/ide_db/src/imports/import_assets.rs +++ b/crates/ide_db/src/imports/import_assets.rs @@ -639,6 +639,17 @@ fn path_import_candidate( assoc_item_name: name, }) } + Some(PathResolution::Def(ModuleDef::TypeAlias(alias))) => { + let ty = alias.ty(sema.db); + if ty.as_adt().is_some() { + ImportCandidate::TraitAssocItem(TraitImportCandidate { + receiver_ty: ty, + assoc_item_name: name, + }) + } else { + return None; + } + } Some(_) => return None, }, None => ImportCandidate::Path(PathImportCandidate { qualifier: None, name }),