Fix adaptation for references with default values after varargs
After vararg argument it's possible to pass values only by name, so here we modulate this behavior #KT-37721 Fixed
This commit is contained in:
Generated
+5
@@ -2090,6 +2090,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("reflectionForVarargAsArray.kt")
|
@TestMetadata("reflectionForVarargAsArray.kt")
|
||||||
public void testReflectionForVarargAsArray() throws Exception {
|
public void testReflectionForVarargAsArray() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
||||||
|
|||||||
+31
-1
@@ -292,7 +292,7 @@ class CallableReferencesCandidateFactory(
|
|||||||
val expectedArgumentCount = inputOutputTypes.inputTypes.size - unboundReceiverCount
|
val expectedArgumentCount = inputOutputTypes.inputTypes.size - unboundReceiverCount
|
||||||
if (expectedArgumentCount < 0) return null
|
if (expectedArgumentCount < 0) return null
|
||||||
|
|
||||||
val fakeArguments = (0 until expectedArgumentCount).map { FakeKotlinCallArgumentForCallableReference(it) }
|
val fakeArguments = createFakeArgumentsForReference(descriptor, expectedArgumentCount, inputOutputTypes, unboundReceiverCount)
|
||||||
val argumentMapping =
|
val argumentMapping =
|
||||||
callComponents.argumentsToParametersMapper.mapArguments(fakeArguments, externalArgument = null, descriptor = descriptor)
|
callComponents.argumentsToParametersMapper.mapArguments(fakeArguments, externalArgument = null, descriptor = descriptor)
|
||||||
if (argumentMapping.diagnostics.any { !it.candidateApplicability.isSuccess }) return null
|
if (argumentMapping.diagnostics.any { !it.candidateApplicability.isSuccess }) return null
|
||||||
@@ -387,6 +387,36 @@ class CallableReferencesCandidateFactory(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createFakeArgumentsForReference(
|
||||||
|
descriptor: FunctionDescriptor,
|
||||||
|
expectedArgumentCount: Int,
|
||||||
|
inputOutputTypes: InputOutputTypes,
|
||||||
|
unboundReceiverCount: Int
|
||||||
|
): List<FakeKotlinCallArgumentForCallableReference> {
|
||||||
|
var afterVararg = false
|
||||||
|
var varargComponentType: UnwrappedType? = null
|
||||||
|
var vararg = false
|
||||||
|
return (0 until expectedArgumentCount).map { index ->
|
||||||
|
val inputType = inputOutputTypes.inputTypes.getOrNull(index + unboundReceiverCount)
|
||||||
|
if (vararg && varargComponentType != inputType) {
|
||||||
|
afterVararg = true
|
||||||
|
}
|
||||||
|
|
||||||
|
val valueParameter = descriptor.valueParameters.getOrNull(index)
|
||||||
|
val name =
|
||||||
|
if (afterVararg && valueParameter?.declaresDefaultValue() == true)
|
||||||
|
valueParameter.name
|
||||||
|
else
|
||||||
|
null
|
||||||
|
|
||||||
|
if (valueParameter?.isVararg == true) {
|
||||||
|
varargComponentType = inputType
|
||||||
|
vararg = true
|
||||||
|
}
|
||||||
|
FakeKotlinCallArgumentForCallableReference(index, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun varargParameterTypeByExpectedParameter(
|
private fun varargParameterTypeByExpectedParameter(
|
||||||
expectedParameterType: KotlinType,
|
expectedParameterType: KotlinType,
|
||||||
substitutedParameter: ValueParameterDescriptor,
|
substitutedParameter: ValueParameterDescriptor,
|
||||||
|
|||||||
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.prepareReceiverRegardingCap
|
|||||||
|
|
||||||
|
|
||||||
class FakeKotlinCallArgumentForCallableReference(
|
class FakeKotlinCallArgumentForCallableReference(
|
||||||
val index: Int
|
val index: Int,
|
||||||
|
val name: Name?
|
||||||
) : KotlinCallArgument {
|
) : KotlinCallArgument {
|
||||||
override val isSpread: Boolean get() = false
|
override val isSpread: Boolean get() = false
|
||||||
override val argumentName: Name? get() = null
|
override val argumentName: Name? get() = name
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReceiverExpressionKotlinCallArgument private constructor(
|
class ReceiverExpressionKotlinCallArgument private constructor(
|
||||||
|
|||||||
Vendored
+23
@@ -0,0 +1,23 @@
|
|||||||
|
var result = "fail"
|
||||||
|
|
||||||
|
fun foo(vararg xs: Int, s1: String = "", s2: String = "OK") {
|
||||||
|
if (xs[0] == 42 && s1 == "good") {
|
||||||
|
result = s2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(vararg xs: Int, s: String = "") {}
|
||||||
|
|
||||||
|
fun use(fn: (IntArray, String) -> Unit) {
|
||||||
|
fn(intArrayOf(42), "good")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
use(::foo)
|
||||||
|
use(::bar)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
test()
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+5
@@ -2110,6 +2110,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("reflectionForVarargAsArray.kt")
|
@TestMetadata("reflectionForVarargAsArray.kt")
|
||||||
public void testReflectionForVarargAsArray() throws Exception {
|
public void testReflectionForVarargAsArray() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
||||||
|
|||||||
+5
@@ -2110,6 +2110,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("reflectionForVarargAsArray.kt")
|
@TestMetadata("reflectionForVarargAsArray.kt")
|
||||||
public void testReflectionForVarargAsArray() throws Exception {
|
public void testReflectionForVarargAsArray() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
||||||
|
|||||||
+5
@@ -2090,6 +2090,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noReflectionForAdaptedCallableReferencesWithSuspendConversion.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("reflectionForVarargAsArray.kt")
|
@TestMetadata("reflectionForVarargAsArray.kt")
|
||||||
public void testReflectionForVarargAsArray() throws Exception {
|
public void testReflectionForVarargAsArray() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/reflectionForVarargAsArray.kt");
|
||||||
|
|||||||
Generated
+5
@@ -1510,6 +1510,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleDefaultArgument.kt")
|
@TestMetadata("simpleDefaultArgument.kt")
|
||||||
public void testSimpleDefaultArgument() throws Exception {
|
public void testSimpleDefaultArgument() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
||||||
|
|||||||
Generated
+5
@@ -1520,6 +1520,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleDefaultArgument.kt")
|
@TestMetadata("simpleDefaultArgument.kt")
|
||||||
public void testSimpleDefaultArgument() throws Exception {
|
public void testSimpleDefaultArgument() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
||||||
|
|||||||
+5
@@ -1520,6 +1520,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
|||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/noNameClashForReferencesToSameFunction.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToVarargWithDefaults.kt")
|
||||||
|
public void testReferenceToVarargWithDefaults() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/referenceToVarargWithDefaults.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleDefaultArgument.kt")
|
@TestMetadata("simpleDefaultArgument.kt")
|
||||||
public void testSimpleDefaultArgument() throws Exception {
|
public void testSimpleDefaultArgument() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
runTest("compiler/testData/codegen/box/callableReference/adaptedReferences/simpleDefaultArgument.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user