From 8e0c2af176c13d7b421948d3cd9b58d31915d751 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Tue, 12 Sep 2023 17:20:45 +0200 Subject: [PATCH] [FIR] Fix overload resolution ambiguity between expect and non-expect in native ^KT-61778 Fixed --- .../calls/ConeOverloadConflictResolver.kt | 8 +++++--- .../org/jetbrains/kotlin/gradle/K2Tests.kt | 13 +++++++++++++ .../build.gradle.kts | 18 ++++++++++++++++++ .../src/commonMain/kotlin/Main.kt | 1 + 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/build.gradle.kts create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/src/commonMain/kotlin/Main.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeOverloadConflictResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeOverloadConflictResolver.kt index 4ee3e7433a9..8d0cc74781c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeOverloadConflictResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeOverloadConflictResolver.kt @@ -318,21 +318,23 @@ class ConeOverloadConflictResolver( private fun List.exactMaxWith(): CandidateSignature? { var result: CandidateSignature? = null for (candidate in this) { - if (result == null || isOfNotLessSpecificShape(candidate, result)) { + if (result == null || checkExpectAndNotLessSpecificShape(candidate, result)) { result = candidate } } if (result == null) return null - if (any { it != result && isOfNotLessSpecificShape(it, result) }) { + if (any { it != result && checkExpectAndNotLessSpecificShape(it, result) }) { return null } return result } - private fun isOfNotLessSpecificShape( + private fun checkExpectAndNotLessSpecificShape( call1: FlatSignature, call2: FlatSignature ): Boolean { + if (!call1.isExpect && call2.isExpect) return true + if (call1.isExpect && !call2.isExpect) return false val hasVarargs1 = call1.hasVarargs val hasVarargs2 = call2.hasVarargs if (hasVarargs1 && !hasVarargs2) return false diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/K2Tests.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/K2Tests.kt index 1ed770cdde7..bc1bad6de44 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/K2Tests.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/K2Tests.kt @@ -201,4 +201,17 @@ class CustomK2Tests : KGPBaseTest() { } } } + + @GradleTest + @DisplayName("No overload resolution ambiguity between expect and non-expect in native") + fun kt61778NoOverloadResolutionAmbiguityBetweenExpectAndNonExpectInNative(gradleVersion: GradleVersion) { + project( + "k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native", gradleVersion, + buildOptions = defaultBuildOptions.copyEnsuringK2() + ) { + build("compileCommonMainKotlinMetadata") { + assertTasksExecuted(":compileCommonMainKotlinMetadata") + } + } + } } diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/build.gradle.kts new file mode 100644 index 00000000000..21fa89bd3bb --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/build.gradle.kts @@ -0,0 +1,18 @@ +// ISSUE: KT-61778 + +plugins { + kotlin("multiplatform") +} + +repositories { + mavenLocal() + mavenCentral() +} + +kotlin { + applyDefaultHierarchyTemplate() + + linuxX64() + macosArm64() + macosX64() +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/src/commonMain/kotlin/Main.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/src/commonMain/kotlin/Main.kt new file mode 100644 index 00000000000..b87b56734dd --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/k2-no-overload-resolution-ambiguity-between-expect-and-non-expect-in-native/src/commonMain/kotlin/Main.kt @@ -0,0 +1 @@ +private val mutexPool by lazy { 42 }