From 5b77210c95cd67913b9edfb110b17f925b2ca5ab Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 21 Aug 2012 19:49:14 +0400 Subject: [PATCH] KT-2632 Check multi-declaraions in for-loops #KT-2632 Fixed --- .../ControlStructureTypingVisitor.java | 8 +++ .../multiDeclarations/DoubleDeclForLoop.kt | 14 +++++ .../FolLoopTypeComponentTypeMismatch.kt | 14 +++++ .../ForLoopComponentFunctionAmbiguity.kt | 15 +++++ .../ForLoopComponentFunctionMissing.kt | 13 +++++ .../ForLoopWithExtensions.kt | 14 +++++ .../multiDeclarations/ForWithExplicitTypes.kt | 14 +++++ .../RedeclarationInForLoop.kt | 14 +++++ .../multiDeclarations/SingleDeclForLoop.kt | 13 +++++ .../checkers/JetDiagnosticsTestGenerated.java | 56 ++++++++++++++++++- 10 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/DoubleDeclForLoop.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/FolLoopTypeComponentTypeMismatch.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionAmbiguity.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionMissing.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopWithExtensions.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForWithExplicitTypes.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/RedeclarationInForLoop.kt create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/SingleDeclForLoop.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java index 59096f3be23..5f2c811db9f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java @@ -267,6 +267,14 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor { loopScope.addVariableDescriptor(variableDescriptor); } + else { + JetMultiDeclaration multiParameter = expression.getMultiParameter(); + if (multiParameter != null && loopRange != null) { + JetType elementType = expectedParameterType == null ? ErrorUtils.createErrorType("Loop range has no type") : expectedParameterType; + TransientReceiver iteratorNextAsReceiver = new TransientReceiver(elementType); + ExpressionTypingUtils.defineLocalVariablesFromMultiDeclaration(loopScope, multiParameter, iteratorNextAsReceiver, loopRange, context); + } + } JetExpression body = expression.getBody(); if (body != null) { diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/DoubleDeclForLoop.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/DoubleDeclForLoop.kt new file mode 100644 index 00000000000..54ad4676859 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/DoubleDeclForLoop.kt @@ -0,0 +1,14 @@ +class A { + fun component1() = 1 + fun component2() = 1 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x, y) in C()) { + + } +} diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/FolLoopTypeComponentTypeMismatch.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/FolLoopTypeComponentTypeMismatch.kt new file mode 100644 index 00000000000..411e9b020c1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/FolLoopTypeComponentTypeMismatch.kt @@ -0,0 +1,14 @@ +class A { + fun component1() = 1 + fun component2() = 1.0 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x: Double, y: Int) in C()) { + + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionAmbiguity.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionAmbiguity.kt new file mode 100644 index 00000000000..801fb10eeb6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionAmbiguity.kt @@ -0,0 +1,15 @@ +class A { + fun component1() = 1 + fun component1() = 1 + fun component2() = 1 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x, y) in C()) { + + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionMissing.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionMissing.kt new file mode 100644 index 00000000000..061d5704dee --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionMissing.kt @@ -0,0 +1,13 @@ +class A { + fun component1() = 1 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x, y) in C()) { + + } +} diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopWithExtensions.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopWithExtensions.kt new file mode 100644 index 00000000000..b2f98835ba3 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopWithExtensions.kt @@ -0,0 +1,14 @@ +class A { +} +fun A.component1() = 1 +fun A.component2() = 1 + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x, y) in C()) { + + } +} diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForWithExplicitTypes.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForWithExplicitTypes.kt new file mode 100644 index 00000000000..211225352ca --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForWithExplicitTypes.kt @@ -0,0 +1,14 @@ +class A { + fun component1() = 1 + fun component2() = 1.0 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x: Int, y: Double) in C()) { + + } +} diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/RedeclarationInForLoop.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/RedeclarationInForLoop.kt new file mode 100644 index 00000000000..489a1eafee1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/RedeclarationInForLoop.kt @@ -0,0 +1,14 @@ +class A { + fun component1() = 1 + fun component2() = 1 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x, x) in C()) { + + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/SingleDeclForLoop.kt b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/SingleDeclForLoop.kt new file mode 100644 index 00000000000..cdaf0a34991 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/SingleDeclForLoop.kt @@ -0,0 +1,13 @@ +class A { + fun component1() = 1 +} + +class C { + fun iterator(): Iterator = null!! +} + +fun test() { + for ((x) in C()) { + + } +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index eb5eb482806..fa04e2a2482 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1113,6 +1113,60 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/declarationChecks/RedeclarationsInMultiDecl.kt"); } + @TestMetadata("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations") + public static class MultiDeclarations extends AbstractDiagnosticsTestWithEagerResolve { + public void testAllFilesPresentInMultiDeclarations() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve", new File("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations"), "kt", false); + } + + @TestMetadata("DoubleDeclForLoop.kt") + public void testDoubleDeclForLoop() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/DoubleDeclForLoop.kt"); + } + + @TestMetadata("FolLoopTypeComponentTypeMismatch.kt") + public void testFolLoopTypeComponentTypeMismatch() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/FolLoopTypeComponentTypeMismatch.kt"); + } + + @TestMetadata("ForLoopComponentFunctionAmbiguity.kt") + public void testForLoopComponentFunctionAmbiguity() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionAmbiguity.kt"); + } + + @TestMetadata("ForLoopComponentFunctionMissing.kt") + public void testForLoopComponentFunctionMissing() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopComponentFunctionMissing.kt"); + } + + @TestMetadata("ForLoopWithExtensions.kt") + public void testForLoopWithExtensions() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForLoopWithExtensions.kt"); + } + + @TestMetadata("ForWithExplicitTypes.kt") + public void testForWithExplicitTypes() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/ForWithExplicitTypes.kt"); + } + + @TestMetadata("RedeclarationInForLoop.kt") + public void testRedeclarationInForLoop() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/RedeclarationInForLoop.kt"); + } + + @TestMetadata("SingleDeclForLoop.kt") + public void testSingleDeclForLoop() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations/SingleDeclForLoop.kt"); + } + + } + + public static Test innerSuite() { + TestSuite suite = new TestSuite("DeclarationChecks"); + suite.addTestSuite(DeclarationChecks.class); + suite.addTestSuite(MultiDeclarations.class); + return suite; + } } @TestMetadata("compiler/testData/diagnostics/tests/extensions") @@ -2972,7 +3026,7 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage suite.addTestSuite(ControlStructures.class); suite.addTestSuite(DataFlow.class); suite.addTestSuite(DataFlowInfoTraversal.class); - suite.addTestSuite(DeclarationChecks.class); + suite.addTest(DeclarationChecks.innerSuite()); suite.addTestSuite(Extensions.class); suite.addTestSuite(Generics.class); suite.addTest(IncompleteCode.innerSuite());