[FIR] Allow treating candidates from the same non-source module as equivalent

This situation occurs in Native metadata compilation and lead to
false-positive OVERLOAD_RESOLUTION_AMBIGUITY reports.

#KT-61461 Fixed
This commit is contained in:
Kirill Rakhman
2023-08-28 17:12:53 +02:00
committed by Space Team
parent fd5ac8575c
commit a42cb2f37f
4 changed files with 48 additions and 2 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.resolve.calls.jvm
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.containingClassLookupTag
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
@@ -70,11 +71,13 @@ class ConeEquivalentCallConflictResolver(
secondCandidate: Candidate
): Boolean {
if (first.symbol.callableId != second.symbol.callableId) return false
// Emulate behavior from K1 where declarations from the same module are never equivalent.
// Emulate behavior from K1 where declarations from the same source module are never equivalent.
// We expect REDECLARATION or CONFLICTING_OVERLOADS to be reported in those cases.
// See a.containingDeclaration == b.containingDeclaration check in
// org.jetbrains.kotlin.resolve.DescriptorEquivalenceForOverrides.areCallableDescriptorsEquivalent.
if (first.moduleData == second.moduleData) return false
// We can't rely on the fact that library declarations will have different moduleData, e.g. in Native metadata compilation,
// multiple stdlib declarations with the same moduleData can be present, see KT-61461.
if (first.moduleData == second.moduleData && first.moduleData.session.kind == FirSession.Kind.Source) return false
if (first.isExpect != second.isExpect) return false
if (first is FirVariable != second is FirVariable) {
return false
@@ -89,6 +89,18 @@ class CustomK2Tests : KGPBaseTest() {
}
}
@Disabled("disable until kotlin/native dependency is updated to include KT-61461")
@GradleTest
@DisplayName("Native metadata of intermediate with multiple targets. KT-61461")
fun nativeMetadataOfIntermediateWithMultipleTargets(gradleVersion: GradleVersion) {
with(project("k2-native-intermediate-multiple-targets", gradleVersion, buildOptions = defaultBuildOptions.copy(languageVersion = "2.0"))) {
val taskToExecute = ":compileNativeMainKotlinMetadata"
build(taskToExecute) {
assertTasksExecuted(taskToExecute)
}
}
}
@Disabled("disable until kotlin/native dependency is updated to include KT-58145")
@GradleTest
@DisplayName("Compiling shared native source with FirFakeOverrideGenerator referencing a common entity. KT-58145")
@@ -0,0 +1,20 @@
group = "test"
version = "1.0"
plugins {
kotlin("multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
applyDefaultHierarchyTemplate()
linuxX64()
macosX64()
macosArm64()
mingwX64()
androidNativeX64()
}
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
fun foo() {
require(true)
for (i in 0 until 42) {}
}
private val mutexPool by lazy { 42 }