fixup: include more information for impls

This commit is contained in:
TJ DeVries 2021-12-02 00:04:03 -05:00
parent d50f18fb65
commit 6870bfd099

View file

@ -110,8 +110,23 @@ pub(crate) fn def_to_moniker(
// Handle associated items within a trait
if let Some(assoc) = def.as_assoc_item(db) {
let container = assoc.container(db);
if let AssocItemContainer::Trait(parent_trait) = container {
path.push(parent_trait.name(db));
match container {
AssocItemContainer::Trait(trait_) => {
// Because different traits can have functions with the same name,
// we have to include the trait name as part of the moniker for uniqueness.
path.push(trait_.name(db));
}
AssocItemContainer::Impl(impl_) => {
// Because a struct can implement multiple traits, for implementations
// we add both the struct name and the trait name to the path
if let Some(adt) = impl_.self_ty(db).as_adt() {
path.push(adt.name(db));
}
if let Some(trait_) = impl_.trait_(db) {
path.push(trait_.name(db));
}
}
}
}
@ -253,6 +268,29 @@ pub mod module {
);
}
#[test]
fn moniker_for_trait_impl_function() {
check_moniker(
r#"
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
pub mod module {
pub trait MyTrait {
pub fn func() {}
}
struct MyStruct {}
impl MyTrait for MyStruct {
pub fn func$0() {}
}
}
"#,
"foo::module::MyStruct::MyTrait::func",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
MonikerKind::Export,
);
}
#[test]
fn moniker_for_field() {
check_moniker(