From b1551c67e0d6324688330020e137a4345e7f173c Mon Sep 17 00:00:00 2001 From: Alejandro Serrano Mena Date: Thu, 21 Sep 2023 11:27:32 +0200 Subject: [PATCH] KT-59504 [FIR] Check _ on destructuring declarations Previously the checker was ignoring them, leading to missing diagnostics ^KT-59504 Fixed --- ...ticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...gnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...tTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...irPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirDestructuringDeclarationChecker.kt | 2 -- ...ructuringDeclarationAssignedUnresolved.fir.kt | 2 +- .../missingComponentN.fir.kt | 16 ++++++++++++++++ .../missingComponentN.kt | 16 ++++++++++++++++ .../missingComponentN.txt | 12 ++++++++++++ .../destructuringDeclarations/underscore.fir.kt | 2 +- .../destructuringInLambdas/underscore.fir.kt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 12 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.fir.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.txt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 8cca5e9fbbc..068bdeb0fb3 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -9191,6 +9191,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/lastDestructuringDeclarationInBlock.kt"); } + @Test + @TestMetadata("missingComponentN.kt") + public void testMissingComponentN() throws Exception { + runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt"); + } + @Test @TestMetadata("RedeclarationInForLoop.kt") public void testRedeclarationInForLoop() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index aab6e77f614..08ab3feeb9d 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -9191,6 +9191,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/lastDestructuringDeclarationInBlock.kt"); } + @Test + @TestMetadata("missingComponentN.kt") + public void testMissingComponentN() throws Exception { + runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt"); + } + @Test @TestMetadata("RedeclarationInForLoop.kt") public void testRedeclarationInForLoop() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index ed8d840634d..26c15316f83 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -9191,6 +9191,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/lastDestructuringDeclarationInBlock.kt"); } + @Test + @TestMetadata("missingComponentN.kt") + public void testMissingComponentN() throws Exception { + runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt"); + } + @Test @TestMetadata("RedeclarationInForLoop.kt") public void testRedeclarationInForLoop() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index cda51a883cf..5190602b652 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -9197,6 +9197,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/lastDestructuringDeclarationInBlock.kt"); } + @Test + @TestMetadata("missingComponentN.kt") + public void testMissingComponentN() throws Exception { + runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt"); + } + @Test @TestMetadata("RedeclarationInForLoop.kt") public void testRedeclarationInForLoop() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDestructuringDeclarationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDestructuringDeclarationChecker.kt index c2f0fb72d2e..e7c8b0eea73 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDestructuringDeclarationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDestructuringDeclarationChecker.kt @@ -26,13 +26,11 @@ import org.jetbrains.kotlin.fir.symbols.SymbolInternals import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase import org.jetbrains.kotlin.fir.types.* -import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.types.AbstractTypeChecker object FirDestructuringDeclarationChecker : FirPropertyChecker() { override fun check(declaration: FirProperty, context: CheckerContext, reporter: DiagnosticReporter) { val source = declaration.source ?: return - if (declaration.name == SpecialNames.UNDERSCORE_FOR_UNUSED_VAR) return // val (...) = `destructuring_declaration` if (source.elementType == KtNodeTypes.DESTRUCTURING_DECLARATION) { checkInitializer(source, declaration.initializer, reporter, context) diff --git a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/destructuringDeclarationAssignedUnresolved.fir.kt b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/destructuringDeclarationAssignedUnresolved.fir.kt index 2a5ec373b5a..f5f18e012d6 100644 --- a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/destructuringDeclarationAssignedUnresolved.fir.kt +++ b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/destructuringDeclarationAssignedUnresolved.fir.kt @@ -5,7 +5,7 @@ fun useDeclaredVariables() { } fun checkersShouldRun() { - val (@A a, _) = unresolved + val (@A a, _) = unresolved } annotation class A diff --git a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.fir.kt b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.fir.kt new file mode 100644 index 00000000000..94423dca08f --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.fir.kt @@ -0,0 +1,16 @@ +class A { + operator fun component1() = 1 + operator fun component2() = "" +} + +fun test() { + val (_, _) = A() + val (_, _, _) = A() + + val (_: Int, _: String) = A() + val (_: String, _) = A() + + val f: (A) -> Int = { (_, _) -> 1 } + val g: (A) -> Int = { (_, _, _) -> 2 } + val h: (A) -> Int = { (_: String, _) -> 3} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt new file mode 100644 index 00000000000..67ae8364e03 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt @@ -0,0 +1,16 @@ +class A { + operator fun component1() = 1 + operator fun component2() = "" +} + +fun test() { + val (_, _) = A() + val (_, _, _) = A() + + val (_: Int, _: String) = A() + val (_: String, _) = A() + + val f: (A) -> Int = { (_, _) -> 1 } + val g: (A) -> Int = { (_, _, _) -> 2 } + val h: (A) -> Int = { (_: String, _) -> 3} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.txt b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.txt new file mode 100644 index 00000000000..1344d14fac1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.txt @@ -0,0 +1,12 @@ +package + +public fun test(): kotlin.Unit + +public final class A { + public constructor A() + public final operator fun component1(): kotlin.Int + public final operator fun component2(): kotlin.String + 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 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/underscore.fir.kt b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/underscore.fir.kt index 8ff97d5ddd4..c7b43d3c277 100644 --- a/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/underscore.fir.kt +++ b/compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/underscore.fir.kt @@ -25,7 +25,7 @@ fun test() { foo(_, _) } - for ((_ : String, _ : Int) in C()) { + for ((_ : String, _ : Int) in C()) { foo(_, _) } diff --git a/compiler/testData/diagnostics/tests/functionLiterals/destructuringInLambdas/underscore.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/destructuringInLambdas/underscore.fir.kt index 2bdabaf1117..f3960bd8bd8 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/destructuringInLambdas/underscore.fir.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/destructuringInLambdas/underscore.fir.kt @@ -50,7 +50,7 @@ fun bar() { _ checkType { _() } } - foo { (_: String, b) -> + foo { (_: String, b) -> _.hashCode() b checkType { _() } } 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 67ebede4670..514c77d5a44 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 @@ -9197,6 +9197,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/lastDestructuringDeclarationInBlock.kt"); } + @Test + @TestMetadata("missingComponentN.kt") + public void testMissingComponentN() throws Exception { + runTest("compiler/testData/diagnostics/tests/declarationChecks/destructuringDeclarations/missingComponentN.kt"); + } + @Test @TestMetadata("RedeclarationInForLoop.kt") public void testRedeclarationInForLoop() throws Exception {