diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 37ae9db0d90..0f7c21a9820 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -1834,6 +1834,16 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/callableReference/functionReferenceWithDefaultValueAsOtherFunctionType_enabled.kt"); } + @TestMetadata("genericCallWithReferenceAgainstVararg.kt") + public void testGenericCallWithReferenceAgainstVararg() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt"); + } + + @TestMetadata("genericCallWithReferenceAgainstVarargAndKFunction.kt") + public void testGenericCallWithReferenceAgainstVarargAndKFunction() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt"); + } + @TestMetadata("kt15439_completeCall.kt") public void testKt15439_completeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt15439_completeCall.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt index 916d59cf006..6fdcc4b6895 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt @@ -170,7 +170,8 @@ class CallableReferencesCandidateFactory( val callComponents: KotlinCallComponents, val scopeTower: ImplicitScopeTower, val compatibilityChecker: ((ConstraintSystemOperation) -> Unit) -> Unit, - val expectedType: UnwrappedType? + val expectedType: UnwrappedType?, + private val csBuilder: ConstraintSystemOperation ) : CandidateFactory { fun createCallableProcessor(explicitReceiver: DetailedReceiver?) = @@ -333,7 +334,9 @@ class CallableReferencesCandidateFactory( return when (varargMappingState) { VarargMappingState.UNMAPPED -> { - if (KotlinBuiltIns.isArrayOrPrimitiveArray(expectedParameterType)) { + if (KotlinBuiltIns.isArrayOrPrimitiveArray(expectedParameterType) || + csBuilder.isTypeVariable(expectedParameterType) + ) { val arrayType = builtins.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(elementType) ?: builtins.getArrayType(Variance.OUT_VARIANCE, elementType) arrayType to VarargMappingState.MAPPED_WITH_ARRAY diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolver.kt index a0eaf463f38..3b54d9e90c6 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolver.kt @@ -78,7 +78,7 @@ class CallableReferenceResolver( val expectedType = resolvedAtom.expectedType?.let { (csBuilder.buildCurrentSubstitutor() as NewTypeSubstitutor).safeSubstitute(it) } val scopeTower = callComponents.statelessCallbacks.getScopeTowerForCallableReferenceArgument(argument) - val candidates = runRHSResolution(scopeTower, argument, expectedType) { checkCallableReference -> + val candidates = runRHSResolution(scopeTower, argument, expectedType, csBuilder) { checkCallableReference -> csBuilder.runTransaction { checkCallableReference(this); false } } @@ -133,9 +133,12 @@ class CallableReferenceResolver( scopeTower: ImplicitScopeTower, callableReference: CallableReferenceKotlinCallArgument, expectedType: UnwrappedType?, // this type can have not fixed type variable inside + csBuilder: ConstraintSystemBuilder, compatibilityChecker: ((ConstraintSystemOperation) -> Unit) -> Unit // you can run anything throw this operation and all this operation will be rolled back ): Set { - val factory = CallableReferencesCandidateFactory(callableReference, callComponents, scopeTower, compatibilityChecker, expectedType) + val factory = CallableReferencesCandidateFactory( + callableReference, callComponents, scopeTower, compatibilityChecker, expectedType, csBuilder + ) val processor = createCallableReferenceProcessor(factory) val candidates = towerResolver.runResolve(scopeTower, processor, useOrder = true, name = callableReference.rhsName) return callableReferenceOverloadConflictResolver.chooseMaximallySpecificCandidates( diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.fir.kt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.fir.kt new file mode 100644 index 00000000000..e63a47a9cbb --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.fir.kt @@ -0,0 +1,17 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun foo(vararg ints: Int) {} +fun test(i: IntArray) { + myLet(i, ::foo) + myLet(::foo) + myLet(::foo) + myLet(::foo) + myLetExplicit1(::foo) + myLetExplicit2(::foo) +} + +fun myLet(t: T, block: (T) -> Unit) {} +fun myLet(block: (T) -> Unit) {} +fun myLetExplicit1(block: (Int) -> Unit) {} +fun myLetExplicit2(block: (IntArray) -> Unit) {} diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt new file mode 100644 index 00000000000..9f8f53d4851 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt @@ -0,0 +1,17 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun foo(vararg ints: Int) {} +fun test(i: IntArray) { + myLet(i, ::foo) + myLet(::foo) + myLet(::foo) + myLet(::foo) + myLetExplicit1(::foo) + myLetExplicit2(::foo) +} + +fun myLet(t: T, block: (T) -> Unit) {} +fun myLet(block: (T) -> Unit) {} +fun myLetExplicit1(block: (Int) -> Unit) {} +fun myLetExplicit2(block: (IntArray) -> Unit) {} diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.txt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.txt new file mode 100644 index 00000000000..7eb594a979a --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.txt @@ -0,0 +1,8 @@ +package + +public fun foo(/*0*/ vararg ints: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit +public fun myLet(/*0*/ block: (T) -> kotlin.Unit): kotlin.Unit +public fun myLet(/*0*/ t: T, /*1*/ block: (T) -> kotlin.Unit): kotlin.Unit +public fun myLetExplicit1(/*0*/ block: (kotlin.Int) -> kotlin.Unit): kotlin.Unit +public fun myLetExplicit2(/*0*/ block: (kotlin.IntArray) -> kotlin.Unit): kotlin.Unit +public fun test(/*0*/ i: kotlin.IntArray): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.fir.kt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.fir.kt new file mode 100644 index 00000000000..17b15007eff --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.fir.kt @@ -0,0 +1,12 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun fun2(f: kotlin.reflect.KFunction1, a: A1) { + f.invoke(a) +} + +fun containsRegex(vararg otherPatterns: String) {} + +fun main() { + fun2(::containsRegex, arrayOf("foo")) +} diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt new file mode 100644 index 00000000000..17b15007eff --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt @@ -0,0 +1,12 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun fun2(f: kotlin.reflect.KFunction1, a: A1) { + f.invoke(a) +} + +fun containsRegex(vararg otherPatterns: String) {} + +fun main() { + fun2(::containsRegex, arrayOf("foo")) +} diff --git a/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.txt b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.txt new file mode 100644 index 00000000000..aef57595f2d --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.txt @@ -0,0 +1,5 @@ +package + +public fun containsRegex(/*0*/ vararg otherPatterns: kotlin.String /*kotlin.Array*/): kotlin.Unit +public fun fun2(/*0*/ f: kotlin.reflect.KFunction1, /*1*/ a: A1): kotlin.Unit +public fun main(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index b0b81811d7e..235cb4100cc 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -1841,6 +1841,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/callableReference/functionReferenceWithDefaultValueAsOtherFunctionType_enabled.kt"); } + @TestMetadata("genericCallWithReferenceAgainstVararg.kt") + public void testGenericCallWithReferenceAgainstVararg() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt"); + } + + @TestMetadata("genericCallWithReferenceAgainstVarargAndKFunction.kt") + public void testGenericCallWithReferenceAgainstVarargAndKFunction() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt"); + } + @TestMetadata("kt15439_completeCall.kt") public void testKt15439_completeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt15439_completeCall.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 11b97782305..146394cb3cd 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -1836,6 +1836,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/functionReferenceWithDefaultValueAsOtherFunctionType_enabled.kt"); } + @TestMetadata("genericCallWithReferenceAgainstVararg.kt") + public void testGenericCallWithReferenceAgainstVararg() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVararg.kt"); + } + + @TestMetadata("genericCallWithReferenceAgainstVarargAndKFunction.kt") + public void testGenericCallWithReferenceAgainstVarargAndKFunction() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/genericCallWithReferenceAgainstVarargAndKFunction.kt"); + } + @TestMetadata("kt15439_completeCall.kt") public void testKt15439_completeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt15439_completeCall.kt");