Skip to content
Merged
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
48 changes: 48 additions & 0 deletions crates/ide-completion/src/tests/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,3 +1976,51 @@ fn main() {
"#]],
);
}

#[test]
fn trait_method_import_across_multiple_crates() {
let fixture = r#"
//- /lib.rs crate:test-trait
pub trait TestTrait {
fn test_function(&self) -> u32;
}

//- /lib.rs crate:test-implementation deps:test-trait
pub struct TestStruct(pub usize);

impl test_trait::TestTrait for TestStruct {
fn test_function(&self) -> u32 {
1
}
}

//- /main.rs crate:main deps:test-implementation,test-trait
use test_implementation::TestStruct;

fn main() {
let test = TestStruct(42);
test.test_f$0
}
"#;

check(
fixture,
expect![[r#"
me test_function() (use test_trait::TestTrait) fn(&self) -> u32
"#]],
);

check_edit(
"test_function",
fixture,
r#"
use test_implementation::TestStruct;
use test_trait::TestTrait;

fn main() {
let test = TestStruct(42);
test.test_function()$0
}
"#,
);
}
14 changes: 13 additions & 1 deletion crates/ide-db/src/imports/import_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,19 @@ fn trait_applicable_items<'db>(
}
deref_chain
.into_iter()
.filter_map(|ty| Some((ty.krate(db).into(), ty.fingerprint_for_trait_impl()?)))
.flat_map(|ty| {
let fingerprint = ty.fingerprint_for_trait_impl()?;
let mut crates = vec![];

if let Some(adt) = ty.as_adt() {
// Push crate where ADT was defined
crates.push((adt.krate(db).into(), fingerprint));
}
// Always include environment crate
crates.push((ty.krate(db).into(), fingerprint));
Some(crates)
})
.flatten()
.unique()
.collect::<Vec<_>>()
};
Expand Down