From 4c9a4548f2b15644ce1e8e56bd1e01ed28357d1a Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 10 Nov 2020 22:08:54 +0300 Subject: [PATCH] FIR: Fix overrides binding for Java inheritor `overriddenMembers` contract requires original (non-enhanced) function See other usages Ignored tests have been working accidentally ^KT-43289 Open --- .../defaultParametersInheritedToJava.kt | 15 +++++ .../defaultParametersInheritedToJava.txt | 13 +++++ .../fir/FirDiagnosticsTestGenerated.java | 5 ++ ...DiagnosticsWithLightTreeTestGenerated.java | 5 ++ ...TouchedTilContractsPhaseTestGenerated.java | 5 ++ .../JavaClassMembersEnhancementScope.kt | 19 ++++--- .../implementCollectionThroughKotlin.kt | 1 + .../javaCollectionWithRemovePrimitiveInt.kt | 1 + .../codegen/box/fullJdk/charBuffer.kt | 1 + .../codegen/box/jvm8/javaDefaults/kt40920.kt | 2 - .../defaultWithKotlinBase.kt | 3 +- .../platformTypes/specializedMapFull.kt | 1 + .../platformTypes/specializedMapPut.kt | 1 + .../specialBuiltins/charBuffer.kt | 1 + .../bytecodeText/builtinFunctions/contains.kt | 1 - .../defaultNullAndParameter.fir.kt | 57 ------------------- .../defaultNullAndParameter.kt | 1 + .../overridesDefaultValue.fir.kt | 6 +- 18 files changed, 66 insertions(+), 72 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt create mode 100644 compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.txt delete mode 100644 compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.fir.kt diff --git a/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt b/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt new file mode 100644 index 00000000000..25c7662648e --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt @@ -0,0 +1,15 @@ +// FILE: main.kt + +fun foo(j: MyJavaClass) { + j.bar() +} + +abstract class MyClass { + open fun bar(y: String? = null) {} +} + +// FILE: MyJavaClass.java + +public class MyJavaClass extends MyClass { + public void bar(String y) {} +} diff --git a/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.txt b/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.txt new file mode 100644 index 00000000000..fd03999f732 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.txt @@ -0,0 +1,13 @@ +FILE: main.kt + public final fun foo(j: R|MyJavaClass|): R|kotlin/Unit| { + R|/j|.R|/MyJavaClass.bar|() + } + public abstract class MyClass : R|kotlin/Any| { + public constructor(): R|MyClass| { + super() + } + + public open fun bar(y: R|kotlin/String?| = Null(null)): R|kotlin/Unit| { + } + + } diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index e71791595f4..857fe390915 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -83,6 +83,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { runTest("compiler/fir/analysis-tests/testData/resolve/defaultJavaImportHiding.kt"); } + @TestMetadata("defaultParametersInheritedToJava.kt") + public void testDefaultParametersInheritedToJava() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt"); + } + @TestMetadata("definitelyNotNullAmbiguity.kt") public void testDefinitelyNotNullAmbiguity() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/definitelyNotNullAmbiguity.kt"); diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 3e27f604ed3..e8917ad8388 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -83,6 +83,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/defaultJavaImportHiding.kt"); } + @TestMetadata("defaultParametersInheritedToJava.kt") + public void testDefaultParametersInheritedToJava() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt"); + } + @TestMetadata("definitelyNotNullAmbiguity.kt") public void testDefinitelyNotNullAmbiguity() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/definitelyNotNullAmbiguity.kt"); diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index aea2b4134a5..33f2005642a 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -83,6 +83,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/defaultJavaImportHiding.kt"); } + @TestMetadata("defaultParametersInheritedToJava.kt") + public void testDefaultParametersInheritedToJava() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/defaultParametersInheritedToJava.kt"); + } + @TestMetadata("definitelyNotNullAmbiguity.kt") public void testDefinitelyNotNullAmbiguity() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/definitelyNotNullAmbiguity.kt"); diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt index 355d1f0a2d4..04887b974fc 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassMembersEnhancementScope.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin +import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction import org.jetbrains.kotlin.fir.java.enhancement.FirSignatureEnhancement import org.jetbrains.kotlin.fir.resolve.lookupSuperTypes @@ -46,11 +47,11 @@ class JavaClassMembersEnhancementScope( override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) { useSiteMemberScope.processPropertiesByName(name) process@{ original -> val enhancedPropertySymbol = signatureEnhancement.enhancedProperty(original, name) - - if (enhancedPropertySymbol is FirPropertySymbol) { + val originalFir = original.fir + if (originalFir is FirProperty && enhancedPropertySymbol is FirPropertySymbol) { val enhancedProperty = enhancedPropertySymbol.fir overriddenProperties[enhancedPropertySymbol] = - enhancedProperty + originalFir .overriddenMembers(enhancedProperty.name) .mapNotNull { it.symbol as? FirPropertySymbol } } @@ -141,11 +142,15 @@ class JavaClassMembersEnhancementScope( val enhancedFunction = (symbol.fir as? FirSimpleFunction)?.changeSignatureIfErasedValueParameter() val enhancedFunctionSymbol = enhancedFunction?.symbol ?: symbol + val originalFunction = original.fir as? FirSimpleFunction + overriddenFunctions[enhancedFunctionSymbol] = - enhancedFunction - ?.overriddenMembers(enhancedFunction.name) - ?.mapNotNull { it.symbol as? FirFunctionSymbol<*> } - .orEmpty() + if (enhancedFunction != null && originalFunction != null) + originalFunction + .overriddenMembers(enhancedFunction.name) + .mapNotNull { it.symbol as? FirFunctionSymbol<*> } + else + emptyList() processor(enhancedFunctionSymbol) } diff --git a/compiler/testData/codegen/box/collections/implementCollectionThroughKotlin.kt b/compiler/testData/codegen/box/collections/implementCollectionThroughKotlin.kt index 036eb71f713..7ae5dc78ad3 100644 --- a/compiler/testData/codegen/box/collections/implementCollectionThroughKotlin.kt +++ b/compiler/testData/codegen/box/collections/implementCollectionThroughKotlin.kt @@ -1,4 +1,5 @@ // TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR // FILE: J.java diff --git a/compiler/testData/codegen/box/collections/javaCollectionWithRemovePrimitiveInt.kt b/compiler/testData/codegen/box/collections/javaCollectionWithRemovePrimitiveInt.kt index 7768bd068eb..15c4643bd45 100644 --- a/compiler/testData/codegen/box/collections/javaCollectionWithRemovePrimitiveInt.kt +++ b/compiler/testData/codegen/box/collections/javaCollectionWithRemovePrimitiveInt.kt @@ -1,4 +1,5 @@ // TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // FILE: javaCollectionWithRemovePrimitiveInt.kt diff --git a/compiler/testData/codegen/box/fullJdk/charBuffer.kt b/compiler/testData/codegen/box/fullJdk/charBuffer.kt index 7d7f2702e91..36aead1ab9f 100644 --- a/compiler/testData/codegen/box/fullJdk/charBuffer.kt +++ b/compiler/testData/codegen/box/fullJdk/charBuffer.kt @@ -1,4 +1,5 @@ // TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR // FULL_JDK diff --git a/compiler/testData/codegen/box/jvm8/javaDefaults/kt40920.kt b/compiler/testData/codegen/box/jvm8/javaDefaults/kt40920.kt index f39c8b7b653..55127728525 100644 --- a/compiler/testData/codegen/box/jvm8/javaDefaults/kt40920.kt +++ b/compiler/testData/codegen/box/jvm8/javaDefaults/kt40920.kt @@ -1,7 +1,5 @@ // !JVM_DEFAULT_MODE: disable // JVM_TARGET: 1.8 -// TARGET_BACKEND: JVM -// IGNORE_BACKEND_FIR: JVM_IR // FILE: JBase.java public interface JBase extends Base { diff --git a/compiler/testData/codegen/box/signatureAnnotations/defaultWithKotlinBase.kt b/compiler/testData/codegen/box/signatureAnnotations/defaultWithKotlinBase.kt index 0a015dc5051..ac18373d15f 100644 --- a/compiler/testData/codegen/box/signatureAnnotations/defaultWithKotlinBase.kt +++ b/compiler/testData/codegen/box/signatureAnnotations/defaultWithKotlinBase.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // FILE: A.kt @@ -21,4 +20,4 @@ fun box(): String { } return "OK" -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapFull.kt b/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapFull.kt index 6829b8262f8..8beed0ad8ed 100644 --- a/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapFull.kt +++ b/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapFull.kt @@ -1,4 +1,5 @@ // FILE: AbstractSpecializedMap.java +// IGNORE_BACKEND_FIR: JVM_IR public abstract class AbstractSpecializedMap implements java.util.Map { public abstract double put(int x, double y); public abstract double remove(int k); diff --git a/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapPut.kt b/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapPut.kt index abeca9a65b3..fbd978d172a 100644 --- a/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapPut.kt +++ b/compiler/testData/codegen/boxAgainstJava/platformTypes/specializedMapPut.kt @@ -1,4 +1,5 @@ // FILE: A.java +// IGNORE_BACKEND_FIR: JVM_IR import java.util.HashMap; public class A extends HashMap { diff --git a/compiler/testData/codegen/boxAgainstJava/specialBuiltins/charBuffer.kt b/compiler/testData/codegen/boxAgainstJava/specialBuiltins/charBuffer.kt index 73db39b2d4d..4c5410fd6fa 100644 --- a/compiler/testData/codegen/boxAgainstJava/specialBuiltins/charBuffer.kt +++ b/compiler/testData/codegen/boxAgainstJava/specialBuiltins/charBuffer.kt @@ -1,4 +1,5 @@ // FILE: CharBuffer.java +// IGNORE_BACKEND_FIR: JVM_IR public abstract class CharBuffer implements CharSequence { public final int length() { diff --git a/compiler/testData/codegen/bytecodeText/builtinFunctions/contains.kt b/compiler/testData/codegen/bytecodeText/builtinFunctions/contains.kt index 5c27e142624..4422a29be04 100644 --- a/compiler/testData/codegen/bytecodeText/builtinFunctions/contains.kt +++ b/compiler/testData/codegen/bytecodeText/builtinFunctions/contains.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR abstract class A1 : MutableCollection { override fun contains(o: Q): Boolean { throw UnsupportedOperationException() diff --git a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.fir.kt b/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.fir.kt deleted file mode 100644 index 444e281e4cf..00000000000 --- a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.fir.kt +++ /dev/null @@ -1,57 +0,0 @@ -// FILE: A.java -// ANDROID_ANNOTATIONS - -import kotlin.annotations.jvm.internal.*; - -public class A { - public void foo(@DefaultNull Integer i) {} - - public void bar(@DefaultNull Integer a) {} - - public void bam(@DefaultNull Integer a) {} - - public void baz(@DefaultValue("42") Integer a) {} -} - -// FILE: AInt.java -import kotlin.annotations.jvm.internal.*; - -public interface AInt { - public void foo(@DefaultValue("42") Integer i) {} - public void bar(@DefaultNull Integer a) {} -} - -// FILE: B.java -import kotlin.annotations.jvm.internal.*; - -public class B extends A { - public void foo(Integer i) {} - - public void bar(@DefaultValue("42") Integer a) {} - - public void bam(@DefaultNull @DefaultValue("42") Integer a) {} -} - -// FILE: C.java -public class C extends A implements AInt { -} - -// FILE: test.kt - -fun test(b: B, c: C) { - b.foo() - b.foo(5) - b.bar() - b.bar(5) - b.bam() - b.bam(5) - - c.foo() - c.foo(5) - c.bar() - c.bar(5) - c.bam() - c.bam(5) - c.baz() - c.baz(42) -} diff --git a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.kt b/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.kt index c6f326e7877..f18e0a87d41 100644 --- a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.kt +++ b/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/defaultNullAndParameter.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // FILE: A.java // ANDROID_ANNOTATIONS diff --git a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/overridesDefaultValue.fir.kt b/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/overridesDefaultValue.fir.kt index a5f9e3e5c1f..fe5fecb195c 100644 --- a/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/overridesDefaultValue.fir.kt +++ b/compiler/testData/diagnostics/tests/j+k/signatureAnnotations/overridesDefaultValue.fir.kt @@ -87,14 +87,14 @@ fun main() { d.first() bd.first() - e.first() + e.first() val g = G() - g.foo() + g.foo() g.foo("ok") val m = M() - m.foo() + m.foo() }