Fix exception when Java class has different methods with same erasure

^KT-36856 Fixed
This commit is contained in:
Denis Zharkov
2020-02-20 13:06:41 +03:00
parent 7efb32628e
commit 071149e0fb
8 changed files with 80 additions and 23 deletions
@@ -12761,6 +12761,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/j+k/kt3311.kt");
}
@TestMetadata("kt36856.kt")
public void testKt36856() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt36856.kt");
}
@TestMetadata("kt6720_abstractProperty.kt")
public void testKt6720_abstractProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt6720_abstractProperty.kt");
+19
View File
@@ -0,0 +1,19 @@
// !CHECK_TYPE
// FILE: Test.java
public class Test {
public <T> T with(Foo<T> matcher) {
return null;
}
public boolean with(Foo<Boolean> matcher) {
return false;
}
}
// FILE: main.kt
class Foo<T>
fun main(foo1: Foo<Boolean>, foo2: Foo<String>) {
val x = object : Test() {} // FE exception is thrown here
x.with(foo1) checkType { <!UNRESOLVED_REFERENCE!>_<!><Boolean>() }
x.with(foo2) checkType { <!UNRESOLVED_REFERENCE!>_<!><String>() }
}
+20
View File
@@ -0,0 +1,20 @@
// !CHECK_TYPE
// FILE: Test.java
public class Test {
public <T> T with(Foo<T> matcher) {
return null;
}
public boolean with(Foo<Boolean> matcher) {
return false;
}
}
// FILE: main.kt
class Foo<T>
fun main(foo1: Foo<Boolean>, foo2: Foo<String>) {
val x = object : Test() {} // FE exception is thrown here
x.with(foo1) checkType { _<Boolean>() }
x.with(foo2) checkType { _<String>() }
}
+19
View File
@@ -0,0 +1,19 @@
package
public fun main(/*0*/ foo1: Foo<kotlin.Boolean>, /*1*/ foo2: Foo<kotlin.String>): kotlin.Unit
public final class Foo</*0*/ T> {
public constructor Foo</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class Test {
public constructor Test()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public open fun </*0*/ T : kotlin.Any!> with(/*0*/ matcher: Foo<T!>!): T!
public open fun with(/*0*/ matcher: Foo<kotlin.Boolean!>!): kotlin.Boolean
}
@@ -12768,6 +12768,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/j+k/kt3311.kt");
}
@TestMetadata("kt36856.kt")
public void testKt36856() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt36856.kt");
}
@TestMetadata("kt6720_abstractProperty.kt")
public void testKt6720_abstractProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt6720_abstractProperty.kt");
@@ -12763,6 +12763,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/j+k/kt3311.kt");
}
@TestMetadata("kt36856.kt")
public void testKt36856() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt36856.kt");
}
@TestMetadata("kt6720_abstractProperty.kt")
public void testKt6720_abstractProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/j+k/kt6720_abstractProperty.kt");
@@ -126,7 +126,7 @@ abstract class LazyJavaScope(
val groups = groupBy { it.computeJvmDescriptor(withReturnType = false) }.values
for (group in groups) {
if (group.size == 1) continue
val mostSpecificMethods = group.selectMostSpecificFromOverridableGroup()
val mostSpecificMethods = group.selectMostSpecificInEachOverridableGroup { this }
removeAll(group)
addAll(mostSpecificMethods)
}
@@ -66,7 +66,12 @@ fun <H : Any> Collection<H>.selectMostSpecificInEachOverridableGroup(
continue
}
val mostSpecific = mostSpecific(overridableGroup, conflictedHandles, descriptorByHandle)
val mostSpecific = OverridingUtil.selectMostSpecificMember(overridableGroup, descriptorByHandle)
val mostSpecificDescriptor = mostSpecific.descriptorByHandle()
overridableGroup.filterNotTo(conflictedHandles) {
OverridingUtil.isMoreSpecific(mostSpecificDescriptor, it.descriptorByHandle())
}
if (conflictedHandles.isNotEmpty()) {
result.addAll(conflictedHandles)
@@ -76,24 +81,3 @@ fun <H : Any> Collection<H>.selectMostSpecificInEachOverridableGroup(
}
return result
}
private fun <H : Any> mostSpecific(
overridableGroup: Collection<H>,
conflictedHandles: MutableSet<H>,
descriptorByHandle: H.() -> CallableDescriptor
): H {
val mostSpecific = OverridingUtil.selectMostSpecificMember(overridableGroup, descriptorByHandle)
val mostSpecificDescriptor = mostSpecific.descriptorByHandle()
overridableGroup.filterNotTo(conflictedHandles) {
OverridingUtil.isMoreSpecific(mostSpecificDescriptor, it.descriptorByHandle())
}
return mostSpecific
}
// Return multiple elements in case of conflicts
fun <D : CallableDescriptor> Collection<D>.selectMostSpecificFromOverridableGroup(): Collection<D> {
val result = mutableSetOf<D>()
result.add(mostSpecific(this, conflictedHandles = result) { this })
return result
}