Skip to content

fix: find Self reference #15864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2023
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
2 changes: 1 addition & 1 deletion crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ impl<'a> FindUsages<'a> {
) -> bool {
match NameRefClass::classify(self.sema, name_ref) {
Some(NameRefClass::Definition(Definition::SelfType(impl_)))
if impl_.self_ty(self.sema.db) == *self_ty =>
if impl_.self_ty(self.sema.db).as_adt() == self_ty.as_adt() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this doesn't seem right to me, now we discard any other type that could appear as the self type in an impl. I'm curious as to why this equality fails in the given example in the first place. Sounds like we are losing some type info along the way somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious too. debug to that point and found the unequality. Is there anyother type for Self in an impl beside Adt?

Actually I don't quite understand the whole implementation of find refenence, it‘s complicated for me :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trait T {}
impl T for fn() {}

is an example. Don't think you can trigger references highlighting on that, but other code might require the Self resolution in search to work for that. I can take a look at that next week and see what's going on here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, the reference search is pretty messy looking and undocumented so I don't blame you for not being able to fully grasp the implementation there :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the problem is that the self_ty here contains an {error} for the generic param where as the impl.self_ty() returns the struct with a var for the generic parameter, so the equality check fails. Hm

{
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
let reference = FileReference {
Expand Down
26 changes: 26 additions & 0 deletions crates/ide/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,32 @@ enum Foo {
);
}

#[test]
fn test_self() {
check(
r#"
struct S$0<T> {
t: PhantomData<T>,
}

impl<T> S<T> {
fn new() -> Self {
Self {
t: Default::default(),
}
}
}
"#,
expect![[r#"
S Struct FileId(0) 0..38 7..8

FileId(0) 48..49
FileId(0) 71..75
FileId(0) 86..90
"#]],
)
}

#[test]
fn test_find_all_refs_two_modules() {
check(
Expand Down