Skip to content

Commit d8a6b79

Browse files
committed
Reintroduce class extensions test
To fix the case where e.g. an extension method for Collection<T> is called on a List<Int>.
1 parent 1050bae commit d8a6b79

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

server/src/main/kotlin/org/javacs/kt/completion/Completions.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,19 @@ private fun isExtensionFor(type: KotlinType, extensionFunction: CallableDescript
434434
val receiverType = extensionFunction.extensionReceiverParameter?.type ?: return false
435435
return KotlinTypeChecker.DEFAULT.isSubtypeOf(type, receiverType)
436436
|| (TypeUtils.getTypeParameterDescriptorOrNull(receiverType)?.isGenericExtensionFor(type) ?: false)
437+
|| (TypeUtils.getClassDescriptor(receiverType)?.isClassExtensionFor(type) ?: false)
437438
}
438439

439440
private fun TypeParameterDescriptor.isGenericExtensionFor(type: KotlinType): Boolean =
440441
upperBounds.all { KotlinTypeChecker.DEFAULT.isSubtypeOf(type, it) }
441442

443+
// Currently only needed for extensions such as e.g. Collection<T> applied to List<SomeConcreteType>
444+
// Direct subtype relationships will be detected by KotlinTypeChecker.isSubtypeOf
445+
private fun ClassDescriptor.isClassExtensionFor(type: KotlinType): Boolean {
446+
fun matchesFqName(t: KotlinType) = TypeUtils.getClassDescriptor(t)?.fqNameSafe == fqNameSafe
447+
return matchesFqName(type) || type.supertypes().any(::matchesFqName)
448+
}
449+
442450
private val loggedHidden = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build<Pair<Name, Name>, Unit>()
443451

444452
private fun logHidden(target: DeclarationDescriptor, from: DeclarationDescriptor) {

0 commit comments

Comments
 (0)