Skip to content

Commit c2ea378

Browse files
bors[bot]unexge
andauthored
Merge #11795
11795: fix: Correctly suggest auto importing traits from aliases r=Veykril a=unexge Fixes #11506 Co-authored-by: unexge <[email protected]>
2 parents 1a92ee5 + 4e4c9ea commit c2ea378

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

crates/ide_completion/src/tests/flyimport.rs

+41
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,47 @@ fn main() {
297297
);
298298
}
299299

300+
#[test]
301+
fn trait_method_from_alias() {
302+
let fixture = r#"
303+
//- /lib.rs crate:dep
304+
pub mod test_mod {
305+
pub trait TestTrait {
306+
fn random_method();
307+
}
308+
pub struct TestStruct {}
309+
impl TestTrait for TestStruct {
310+
fn random_method() {}
311+
}
312+
pub type TestAlias = TestStruct;
313+
}
314+
315+
//- /main.rs crate:main deps:dep
316+
fn main() {
317+
dep::test_mod::TestAlias::ran$0
318+
}
319+
"#;
320+
321+
check(
322+
fixture,
323+
expect![[r#"
324+
fn random_method() (use dep::test_mod::TestTrait) fn()
325+
"#]],
326+
);
327+
328+
check_edit(
329+
"random_method",
330+
fixture,
331+
r#"
332+
use dep::test_mod::TestTrait;
333+
334+
fn main() {
335+
dep::test_mod::TestAlias::random_method()$0
336+
}
337+
"#,
338+
);
339+
}
340+
300341
#[test]
301342
fn no_trait_type_fuzzy_completion() {
302343
check(

crates/ide_db/src/imports/import_assets.rs

+11
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,17 @@ fn path_import_candidate(
639639
assoc_item_name: name,
640640
})
641641
}
642+
Some(PathResolution::Def(ModuleDef::TypeAlias(alias))) => {
643+
let ty = alias.ty(sema.db);
644+
if ty.as_adt().is_some() {
645+
ImportCandidate::TraitAssocItem(TraitImportCandidate {
646+
receiver_ty: ty,
647+
assoc_item_name: name,
648+
})
649+
} else {
650+
return None;
651+
}
652+
}
642653
Some(_) => return None,
643654
},
644655
None => ImportCandidate::Path(PathImportCandidate { qualifier: None, name }),

0 commit comments

Comments
 (0)