K2: add ACCIDENTAL_OVERRIDE_CLASH_BY_JVM_SIGNATURE for clashing functions

This diagnostic is reported on an explicitly-declared function which
overrides a regular Java function with the same signature as a hidden
one from the same scope.

#KT-64846 Fixed
This commit is contained in:
Mikhail Glukhikh
2024-01-22 18:41:45 +01:00
committed by Space Team
parent 8f9614c1fb
commit 7615d863d5
19 changed files with 138 additions and 4 deletions
@@ -0,0 +1,2 @@
/main.kt:(78,81): error: This function accidentally overrides both 'public open operator fun /A.get(index: kotlin/Int): kotlin/Char' and a renamed function 'public open fun /A.charAt(index: kotlin/Int): kotlin/Char' from JVM point of view because of mixed Java/Kotlin hierarchy.
This situation provokes a JVM clash and thus is forbidden. To fix it, you have to delete either this function or one of overridden functions.
@@ -1,4 +1,5 @@
// SCOPE_DUMP: KA:get
// RENDER_DIAGNOSTICS_FULL_TEXT
// FILE: A.java
abstract public class A implements CharSequence {
@@ -17,7 +18,7 @@ abstract public class A implements CharSequence {
// FILE: main.kt
abstract class KA : A() {
override fun get(index: Int) = 'O'
override fun <!ACCIDENTAL_OVERRIDE_CLASH_BY_JVM_SIGNATURE!>get<!>(index: Int) = 'O'
}
fun foo(a: A, ka: KA) {
@@ -1,4 +1,5 @@
// SCOPE_DUMP: KA:get
// RENDER_DIAGNOSTICS_FULL_TEXT
// FILE: A.java
abstract public class A implements CharSequence {
@@ -0,0 +1,12 @@
/main.kt:18:16: error: the integer literal does not conform to the expected type String!
a.contains(1)
^
/main.kt:20:5: error: the integer literal does not conform to the expected type String!
1 in a
^
/main.kt:23:17: error: the integer literal does not conform to the expected type String
ka.contains(1)
^
/main.kt:25:5: error: the integer literal does not conform to the expected type String
1 in ka
^
@@ -0,0 +1,10 @@
/main.kt:(70,78): error: This function accidentally overrides both 'public open operator fun /A.contains(x: kotlin/String!): kotlin/Boolean' and a function with erased parameters 'public open operator fun /A.contains(x: kotlin/Any!): kotlin/Boolean' from JVM point of view because of mixed Java/Kotlin hierarchy.
This situation provokes a JVM clash and thus is forbidden. To fix it, you have to delete either this function or one of overridden functions.
/main.kt:(159,160): error: Argument type mismatch: actual type is 'kotlin.Int', but 'kotlin.String!' was expected.
/main.kt:(178,179): error: Argument type mismatch: actual type is 'kotlin.Int', but 'kotlin.String!' was expected.
/main.kt:(222,223): error: Argument type mismatch: actual type is 'kotlin.Int', but 'kotlin.String' was expected.
/main.kt:(242,243): error: Argument type mismatch: actual type is 'kotlin.Int', but 'kotlin.String' was expected.
@@ -1,5 +1,6 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -PARAMETER_NAME_CHANGED_ON_OVERRIDE
// SCOPE_DUMP: KA:contains
// RENDER_DIAGNOSTICS_FULL_TEXT
// FILE: A.java
abstract public class A implements java.util.Collection<String> {
@@ -9,7 +10,7 @@ abstract public class A implements java.util.Collection<String> {
// FILE: main.kt
abstract class KA : A() {
override fun contains(x: String) = false
override fun <!ACCIDENTAL_OVERRIDE_CLASH_BY_JVM_SIGNATURE!>contains<!>(x: String) = false
}
fun foo(a: A, ka: KA) {
@@ -1,5 +1,6 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -PARAMETER_NAME_CHANGED_ON_OVERRIDE
// SCOPE_DUMP: KA:contains
// RENDER_DIAGNOSTICS_FULL_TEXT
// FILE: A.java
abstract public class A implements java.util.Collection<String> {
@@ -146,7 +146,7 @@ public class X implements java.util.List<String> {
class Y : X()
class Y2 : X() {
override fun removeAt(index: Int) = ""
override fun <!ACCIDENTAL_OVERRIDE_CLASH_BY_JVM_SIGNATURE!>removeAt<!>(index: Int) = ""
}
fun main() {