From 3ac8d951cbf4979a2b5ea064a053f31546c1954c Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Fri, 15 Apr 2022 12:23:05 +0300 Subject: [PATCH] Fix exception when reporting WRONG_TYPE_PARAMETER_NULLABILITY_FOR_JAVA_OVERRIDE ^KT-51979 Fixed --- ...nosisCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ .../FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...ntendDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ ...aOverrideWithWrongNullabilityOverrideChecker.kt | 5 +++++ .../tests/j+k/types/noOverrideKeyword.fir.kt | 14 ++++++++++++++ .../tests/j+k/types/noOverrideKeyword.kt | 14 ++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 7 files changed, 57 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.fir.kt create mode 100644 compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 11ab9d87b00cc..e2bc0b7cdb01d 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -18556,6 +18556,12 @@ public void testConstantStaticStringField() throws Exception { runTest("compiler/testData/diagnostics/tests/j+k/types/constantStaticStringField.kt"); } + @Test + @TestMetadata("noOverrideKeyword.kt") + public void testNoOverrideKeyword() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt"); + } + @Test @TestMetadata("notNullTypeParameterWithKotlinNullable.kt") public void testNotNullTypeParameterWithKotlinNullable() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 81ea3450ff19e..6bee0ed360390 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -18556,6 +18556,12 @@ public void testConstantStaticStringField() throws Exception { runTest("compiler/testData/diagnostics/tests/j+k/types/constantStaticStringField.kt"); } + @Test + @TestMetadata("noOverrideKeyword.kt") + public void testNoOverrideKeyword() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt"); + } + @Test @TestMetadata("notNullTypeParameterWithKotlinNullable.kt") public void testNotNullTypeParameterWithKotlinNullable() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index a5ddce4270b29..85d10dd9ce656 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -18556,6 +18556,12 @@ public void testConstantStaticStringField() throws Exception { runTest("compiler/testData/diagnostics/tests/j+k/types/constantStaticStringField.kt"); } + @Test + @TestMetadata("noOverrideKeyword.kt") + public void testNoOverrideKeyword() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt"); + } + @Test @TestMetadata("notNullTypeParameterWithKotlinNullable.kt") public void testNotNullTypeParameterWithKotlinNullable() throws Exception { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaOverrideWithWrongNullabilityOverrideChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaOverrideWithWrongNullabilityOverrideChecker.kt index 8b29f9025fa88..18a4b0e7602b4 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaOverrideWithWrongNullabilityOverrideChecker.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaOverrideWithWrongNullabilityOverrideChecker.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.resolve.jvm.checkers import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.resolve.OverridingUtil @@ -27,6 +28,10 @@ object JavaOverrideWithWrongNullabilityOverrideChecker : DeclarationChecker { if (descriptor !is CallableMemberDescriptor) return if (descriptor.overriddenDescriptors.isEmpty()) return + val modifierList = declaration.modifierList + val hasOverrideNode = modifierList != null && modifierList.hasModifier(KtTokens.OVERRIDE_KEYWORD) + if (!hasOverrideNode) return + val containingClass = descriptor.containingDeclaration as? ClassDescriptor ?: return for (overriddenDescriptor in descriptor.overriddenDescriptors) { diff --git a/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.fir.kt b/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.fir.kt new file mode 100644 index 0000000000000..53a3cc3075550 --- /dev/null +++ b/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.fir.kt @@ -0,0 +1,14 @@ +// !SKIP_JAVAC +// SKIP_TXT +// !LANGUAGE: -ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +// FILE: JavaInterface.java +import org.jetbrains.annotations.NotNull; + +public interface JavaInterface { + void interfaceMethod(@NotNull V value); +} + +// FILE: main.kt +interface KotlinInterface : JavaInterface { + override <T> fun interfaceMethod(x: X) +} diff --git a/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt b/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt new file mode 100644 index 0000000000000..4123b04b5d19c --- /dev/null +++ b/compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt @@ -0,0 +1,14 @@ +// !SKIP_JAVAC +// SKIP_TXT +// !LANGUAGE: -ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +// FILE: JavaInterface.java +import org.jetbrains.annotations.NotNull; + +public interface JavaInterface { + void interfaceMethod(@NotNull V value); +} + +// FILE: main.kt +interface KotlinInterface : JavaInterface { + override <T> fun interfaceMethod(x: X) +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 7e97cc0d45da4..5305097b2d46e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -18562,6 +18562,12 @@ public void testConstantStaticStringField() throws Exception { runTest("compiler/testData/diagnostics/tests/j+k/types/constantStaticStringField.kt"); } + @Test + @TestMetadata("noOverrideKeyword.kt") + public void testNoOverrideKeyword() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/types/noOverrideKeyword.kt"); + } + @Test @TestMetadata("notNullTypeParameterWithKotlinNullable.kt") public void testNotNullTypeParameterWithKotlinNullable() throws Exception {