Skip to content

Commit 0713a47

Browse files
committed
Auto merge of rust-lang#17813 - roife:fix-issue-17803, r=Veykril
fix: tyck for non-ADT types when searching refs for `Self` kw See https://github.com/rust-lang/rust-analyzer/pull/15864/files/e0276dc5ddc38c65240edb408522bb869f15afb4#r1389848845 For ADTs, to handle `{error}` in generic args, we should to convert them to ADT for comparisons; for others, we can directly compare the types.
2 parents 937ef11 + ac7c466 commit 0713a47

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/tools/rust-analyzer/crates/ide-db/src/search.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,16 @@ impl<'a> FindUsages<'a> {
663663
name_ref: &ast::NameRef,
664664
sink: &mut dyn FnMut(EditionedFileId, FileReference) -> bool,
665665
) -> bool {
666+
// See https://github.com/rust-lang/rust-analyzer/pull/15864/files/e0276dc5ddc38c65240edb408522bb869f15afb4#r1389848845
667+
let ty_eq = |ty: hir::Type| match (ty.as_adt(), self_ty.as_adt()) {
668+
(Some(ty), Some(self_ty)) => ty == self_ty,
669+
(None, None) => ty == *self_ty,
670+
_ => false,
671+
};
672+
666673
match NameRefClass::classify(self.sema, name_ref) {
667674
Some(NameRefClass::Definition(Definition::SelfType(impl_)))
668-
if impl_.self_ty(self.sema.db).as_adt() == self_ty.as_adt() =>
675+
if ty_eq(impl_.self_ty(self.sema.db)) =>
669676
{
670677
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
671678
let reference = FileReference {

src/tools/rust-analyzer/crates/ide/src/references.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ pub(crate) fn find_all_refs(
6060
move |def: Definition| {
6161
let mut usages =
6262
def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all();
63-
6463
if literal_search {
6564
retain_adt_literal_usages(&mut usages, def, sema);
6665
}
@@ -817,6 +816,30 @@ impl<T> S<T> {
817816
)
818817
}
819818

819+
#[test]
820+
fn test_self_inside_not_adt_impl() {
821+
check(
822+
r#"
823+
pub trait TestTrait {
824+
type Assoc;
825+
fn stuff() -> Self;
826+
}
827+
impl TestTrait for () {
828+
type Assoc$0 = u8;
829+
fn stuff() -> Self {
830+
let me: Self = ();
831+
me
832+
}
833+
}
834+
"#,
835+
expect![[r#"
836+
Assoc TypeAlias FileId(0) 92..108 97..102
837+
838+
FileId(0) 31..36
839+
"#]],
840+
)
841+
}
842+
820843
#[test]
821844
fn test_find_all_refs_two_modules() {
822845
check(

0 commit comments

Comments
 (0)