diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index c86f276df66..30baaf98a78 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -369,8 +369,10 @@ class ResolvedAtomCompleter( } private fun ReceiverValue.updateReceiverValue(substitutor: TypeSubstitutor): ReceiverValue { - val newType = substitutor.safeSubstitute(type, Variance.INVARIANT) - return if (type != newType) replaceType(newType) else this + val newType = substitutor.safeSubstitute(type, Variance.INVARIANT).let { + typeApproximator.approximateToSuperType(it, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference) ?: it + } + return if (type != newType) replaceType(newType as KotlinType) else this } private fun recordArgumentAdaptationForCallableReference( diff --git a/compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt b/compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt new file mode 100644 index 00000000000..d9406b5a41c --- /dev/null +++ b/compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt @@ -0,0 +1,42 @@ +// !LANGUAGE: +NewInference +// TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR +// WITH_REFLECT + +import kotlin.test.assertEquals + +fun bar0(vararg a: T) = test(a::get) +fun bar1(vararg a: T) = test(a::set) + +fun bar2(a: Array) = test(a::get) +fun bar3(a: Array) = test(a::set) + +fun bar4(a: Array) = test(a::get) +fun bar5(a: Array) = test(a::set) + +fun test(f: F): String = f.toString() + +fun box(): String { + val getMethod = "fun kotlin.Array.get(kotlin.Int): T" + val setMethod = "fun kotlin.Array.set(kotlin.Int, T): kotlin.Unit" + + val b0 = bar0("") + val b1 = bar1("") + + assertEquals(getMethod, b0) + assertEquals(setMethod, b1) + + val b2 = bar2(arrayOf("")) + val b3 = bar3(arrayOf("")) + + assertEquals(getMethod, b2) + assertEquals(setMethod, b3) + + val b4 = bar4(arrayOf("")) + val b5 = bar5(arrayOf("")) + + assertEquals(getMethod, b4) + assertEquals(setMethod, b5) + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 78e04d4aff1..160929a2ab5 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -2082,6 +2082,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/callableReference/bound/receiverEvaluatedOnce.kt"); } + @TestMetadata("referenceToGetSetMethodsFromVararg.kt") + public void testReferenceToGetSetMethodsFromVararg() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt"); + } + @TestMetadata("simpleFunction.kt") public void testSimpleFunction() throws Exception { runTest("compiler/testData/codegen/box/callableReference/bound/simpleFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 1895cb3534e..10b2df7278f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -2082,6 +2082,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/callableReference/bound/receiverEvaluatedOnce.kt"); } + @TestMetadata("referenceToGetSetMethodsFromVararg.kt") + public void testReferenceToGetSetMethodsFromVararg() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt"); + } + @TestMetadata("simpleFunction.kt") public void testSimpleFunction() throws Exception { runTest("compiler/testData/codegen/box/callableReference/bound/simpleFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index c9cd31e41f4..e5ebe375170 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -2062,6 +2062,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/callableReference/bound/receiverEvaluatedOnce.kt"); } + @TestMetadata("referenceToGetSetMethodsFromVararg.kt") + public void testReferenceToGetSetMethodsFromVararg() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt"); + } + @TestMetadata("simpleFunction.kt") public void testSimpleFunction() throws Exception { runTest("compiler/testData/codegen/box/callableReference/bound/simpleFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 37758a6cf99..56fcce64068 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -2062,6 +2062,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/callableReference/bound/receiverEvaluatedOnce.kt"); } + @TestMetadata("referenceToGetSetMethodsFromVararg.kt") + public void testReferenceToGetSetMethodsFromVararg() throws Exception { + runTest("compiler/testData/codegen/box/callableReference/bound/referenceToGetSetMethodsFromVararg.kt"); + } + @TestMetadata("simpleFunction.kt") public void testSimpleFunction() throws Exception { runTest("compiler/testData/codegen/box/callableReference/bound/simpleFunction.kt");