fix: tyck for non-ADT types when searching refs for Self kw

This commit is contained in:
roife 2024-08-06 21:52:43 +08:00
parent 5fe545c87f
commit dc104b05cd
2 changed files with 32 additions and 2 deletions

View file

@ -663,9 +663,16 @@ impl<'a> FindUsages<'a> {
name_ref: &ast::NameRef, name_ref: &ast::NameRef,
sink: &mut dyn FnMut(EditionedFileId, FileReference) -> bool, sink: &mut dyn FnMut(EditionedFileId, FileReference) -> bool,
) -> bool { ) -> bool {
// See https://github.com/rust-lang/rust-analyzer/pull/15864/files/e0276dc5ddc38c65240edb408522bb869f15afb4#r1389848845
let ty_eq = |ty: hir::Type| match (ty.as_adt(), self_ty.as_adt()) {
(Some(ty), Some(self_ty)) => ty == self_ty,
(None, None) => ty == *self_ty,
_ => false,
};
match NameRefClass::classify(self.sema, name_ref) { match NameRefClass::classify(self.sema, name_ref) {
Some(NameRefClass::Definition(Definition::SelfType(impl_))) Some(NameRefClass::Definition(Definition::SelfType(impl_)))
if impl_.self_ty(self.sema.db).as_adt() == self_ty.as_adt() => if ty_eq(impl_.self_ty(self.sema.db)) =>
{ {
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
let reference = FileReference { let reference = FileReference {

View file

@ -60,7 +60,6 @@ pub(crate) fn find_all_refs(
move |def: Definition| { move |def: Definition| {
let mut usages = let mut usages =
def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all(); def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all();
if literal_search { if literal_search {
retain_adt_literal_usages(&mut usages, def, sema); retain_adt_literal_usages(&mut usages, def, sema);
} }
@ -817,6 +816,30 @@ impl<T> S<T> {
) )
} }
#[test]
fn test_self_inside_not_adt_impl() {
check(
r#"
pub trait TestTrait {
type Assoc;
fn stuff() -> Self;
}
impl TestTrait for () {
type Assoc$0 = u8;
fn stuff() -> Self {
let me: Self = ();
me
}
}
"#,
expect![[r#"
Assoc TypeAlias FileId(0) 92..108 97..102
FileId(0) 31..36
"#]],
)
}
#[test] #[test]
fn test_find_all_refs_two_modules() { fn test_find_all_refs_two_modules() {
check( check(