11795: fix: Correctly suggest auto importing traits from aliases r=Veykril a=unexge

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/11506

Co-authored-by: unexge <unexge@gmail.com>
This commit is contained in:
bors[bot] 2022-03-22 21:44:06 +00:00 committed by GitHub
commit c2ea378920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -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(

View File

@ -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 }),