From 5d391bc2618ad37b86c4cd4af5011d91afef9a39 Mon Sep 17 00:00:00 2001 From: Marco Pennekamp Date: Wed, 8 Feb 2023 18:41:43 +0100 Subject: [PATCH] [FIR] KT-56505 Fix `checkVisibilityModifier` taking wrong modifier list - KT-56505 occurred because `source.getChild(KtNodeTypes.MODIFIER_LIST)` returns any modifier list in the subtree of the source element, not necessarily the modifier list belonging to the checked element. `depth = 1` restricts the search to the modifier list belonging to the checked element itself. - For example, given `f1` from KT-56505, `getChild` would return the modifier list of `public var foo = 0`. Because it contains a visibility modifier, `f1` wasn't marked with `NO_EXPLICIT_VISIBILITY_IN_API_MODE`. ^KT-56505 fixed --- ...sCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...bilityModifierCheckerWithExplicitApiMode.kt | 6 +++--- ...FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...dDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../syntax/FirExplicitApiDeclarationChecker.kt | 7 +++---- .../tests/testsWithExplicitApi/kt56505.kt | 18 ++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 7 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.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 befe8192367..6cd35819347 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 @@ -32720,6 +32720,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt"); } + @Test + @TestMetadata("kt56505.kt") + public void testKt56505() throws Exception { + runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt"); + } + @Test @TestMetadata("localFunctions.kt") public void testLocalFunctions() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt index fe0fe9d6bc2..91de3cf3fde 100644 --- a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt +++ b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt @@ -1,6 +1,6 @@ // EXPLICIT_API_MODE: STRICT -fun f1() { +fun f1() { class LocalClass { public var foo = 0 } @@ -71,7 +71,7 @@ public interface I2 { public var baz2: Int = 0 -class J1 { +class J1 { protected val baz: Int = 0 protected get() = field * 2 var baf: Int = 0 @@ -80,7 +80,7 @@ class J1 { field = value } - var buf: Int = 0 + var buf: Int = 0 private get() = 42 protected set(value) { field = value 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 d4c2b3f47ed..631591cd018 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 @@ -32816,6 +32816,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt"); } + @Test + @TestMetadata("kt56505.kt") + public void testKt56505() throws Exception { + runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt"); + } + @Test @TestMetadata("localFunctions.kt") public void testLocalFunctions() 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 5b18465a3fc..3c3cfba58fc 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 @@ -32720,6 +32720,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt"); } + @Test + @TestMetadata("kt56505.kt") + public void testKt56505() throws Exception { + runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt"); + } + @Test @TestMetadata("localFunctions.kt") public void testLocalFunctions() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/syntax/FirExplicitApiDeclarationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/syntax/FirExplicitApiDeclarationChecker.kt index 6927a86faaa..44917435344 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/syntax/FirExplicitApiDeclarationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/syntax/FirExplicitApiDeclarationChecker.kt @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirPrimaryConstructor import org.jetbrains.kotlin.fir.declarations.utils.effectiveVisibility import org.jetbrains.kotlin.fir.declarations.utils.isData import org.jetbrains.kotlin.fir.declarations.utils.isOverride -import org.jetbrains.kotlin.fir.declarations.utils.visibility import org.jetbrains.kotlin.fir.resolve.transformers.publishedApiEffectiveVisibility import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtDeclaration @@ -48,8 +47,8 @@ object FirExplicitApiDeclarationChecker : FirDeclarationSyntaxCheckerfun f1() { + class LocalClass { + public var foo = 0 + } + LocalClass().foo = 1 +} + +class J1 { + var buf: Int = 0 + private get() = 42 + protected set(value) { + field = value + } +} 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 33df449b025..1f72e5319bf 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 @@ -32816,6 +32816,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/interfaces.kt"); } + @Test + @TestMetadata("kt56505.kt") + public void testKt56505() throws Exception { + runTest("compiler/testData/diagnostics/tests/testsWithExplicitApi/kt56505.kt"); + } + @Test @TestMetadata("localFunctions.kt") public void testLocalFunctions() throws Exception {