Skip to content

Commit d73953f

Browse files
committed
Simplify checks of descriptor and psi correspondance.
1 parent ffb13a6 commit d73953f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,15 @@ class ResolverImpl(
536536
kindFilter = DescriptorKindFilter.VARIABLES,
537537
name = propName
538538
) {
539-
synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.findPsi() == psi
539+
synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.correspondsTo(psi) == true
540540
}
541541
}
542542
property ?: moduleClassResolver
543543
.resolveContainingClass(psi)?.let { containingClass ->
544544
val filter = if (psi is SyntheticElement) {
545545
{ declaration: DeclarationDescriptor -> declaration.name.asString() == psi.name }
546546
} else {
547-
{ declaration: DeclarationDescriptor -> declaration.findPsi() == psi }
547+
{ declaration: DeclarationDescriptor -> declaration.correspondsTo(psi) }
548548
}
549549
containingClass.findEnclosedDescriptor(
550550
kindFilter = DescriptorKindFilter.FUNCTIONS,
@@ -559,7 +559,7 @@ class ResolverImpl(
559559
?.findEnclosedDescriptor(
560560
kindFilter = DescriptorKindFilter.VARIABLES,
561561
name = psi.name,
562-
filter = { it.findPsi() == psi }
562+
filter = { it.correspondsTo(psi) }
563563
)
564564
}
565565
else -> throw IllegalStateException("unhandled psi element kind: ${psi.javaClass}")
@@ -756,7 +756,7 @@ class ResolverImpl(
756756
?.findEnclosedDescriptor(
757757
kindFilter = DescriptorKindFilter.FUNCTIONS,
758758
name = owner.name,
759-
filter = { it.findPsi() == owner }
759+
filter = { it.correspondsTo(owner) }
760760
) as FunctionDescriptor
761761
} as DeclarationDescriptor
762762
val typeParameterDescriptor = LazyJavaTypeParameterDescriptor(

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/utils.kt

+5
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,11 @@ fun DeclarationDescriptor.findPsi(): PsiElement? {
559559
return leaf.parentsWithSelf.firstOrNull { psi.manager.areElementsEquivalent(it, psi) }
560560
}
561561

562+
fun DeclarationDescriptor.correspondsTo(candidate: PsiElement): Boolean {
563+
val psi = (this as? DeclarationDescriptorWithSource)?.source?.getPsi() ?: return false
564+
return psi.manager.areElementsEquivalent(psi, candidate)
565+
}
566+
562567
internal fun KSFile.getPackageAnnotations() = (this as? KSFileJavaImpl)?.psi?.packageStatement
563568
?.annotationList?.annotations?.map { KSAnnotationJavaImpl.getCached(it) } ?: emptyList<KSAnnotation>()
564569

0 commit comments

Comments
 (0)