Making var arg kParameters default to empty array no argument given

fixes KT-29969
This commit is contained in:
Mathias Quintero
2020-03-14 10:33:41 +01:00
committed by Alexander Udalov
parent b1dbacf45f
commit 34a64d9171
9 changed files with 118 additions and 0 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
import java.lang.reflect.Array as ReflectArray
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.WildcardType
@@ -17,6 +18,7 @@ import kotlin.coroutines.Continuation
import kotlin.reflect.*
import kotlin.reflect.jvm.internal.calls.Caller
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmErasure
internal abstract class KCallableImpl<out R> : KCallable<R> {
abstract val descriptor: CallableMemberDescriptor
@@ -134,6 +136,9 @@ internal abstract class KCallableImpl<out R> : KCallable<R> {
mask = mask or (1 shl (index % Integer.SIZE))
anyOptional = true
}
parameter.isVararg -> {
arguments.add(defaultEmptyArray(parameter.type))
}
else -> {
throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
}
@@ -174,6 +179,7 @@ internal abstract class KCallableImpl<out R> : KCallable<R> {
args[parameter] ?: throw IllegalArgumentException("Annotation argument value cannot be null ($parameter)")
}
parameter.isOptional -> null
parameter.isVararg -> defaultEmptyArray(parameter.type)
else -> throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
}
}
@@ -202,6 +208,14 @@ internal abstract class KCallableImpl<out R> : KCallable<R> {
}
} else null
private fun defaultEmptyArray(type: KType): Any =
type.jvmErasure.java.run {
if (isArray) ReflectArray.newInstance(componentType, 0)
else throw KotlinReflectionInternalError(
"Cannot instantiate the default empty array of type $simpleName, because it is not an array type"
)
}
private fun extractContinuationArgument(): Type? {
if ((descriptor as? FunctionDescriptor)?.isSuspend == true) {
// kotlin.coroutines.Continuation<? super java.lang.String>