From 69e5444ddf98b619010eda39adbe37b38f1a1516 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 14 May 2014 13:57:14 +0400 Subject: [PATCH] Added tests on labels #KT-1703 Fixed #KT-361 Fixed #KT-3920 Fixed #KT-3988 Fixed #KT-4247 Fixed #KT-4586 Fixed #KT-4603 Fixed #KT-591 Fixed --- .../diagnostics/tests/labels/kt1703.kt | 15 +++++ .../diagnostics/tests/labels/kt361.kt | 12 ++++ .../diagnostics/tests/labels/kt3920.kt | 11 ++++ .../diagnostics/tests/labels/kt3988.kt | 21 +++++++ .../diagnostics/tests/labels/kt4247.kt | 11 ++++ .../diagnostics/tests/labels/kt4586.kt | 17 ++++++ .../diagnostics/tests/labels/kt4603.kt | 12 ++++ .../diagnostics/tests/labels/kt591.kt | 11 ++++ .../labelReferencesInsideObjectExpressions.kt | 25 +++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 56 ++++++++++++++++++- 10 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/labels/kt1703.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt361.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt3920.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt3988.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt4247.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt4586.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt4603.kt create mode 100644 compiler/testData/diagnostics/tests/labels/kt591.kt create mode 100644 compiler/testData/diagnostics/tests/labels/labelReferencesInsideObjectExpressions.kt diff --git a/compiler/testData/diagnostics/tests/labels/kt1703.kt b/compiler/testData/diagnostics/tests/labels/kt1703.kt new file mode 100644 index 00000000000..4b58f2f894b --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt1703.kt @@ -0,0 +1,15 @@ +//KT-1703 Reference to label is unresolved + +fun test() { + val ints = Array(2, { null }) + ints.forEach @lit { + if (it == null) return @lit + use(it + 5) + } +} + +fun Array.forEach(operation: (T) -> Unit) { + for (element in this) operation(element) +} + +fun use(a: Any?) = a \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/kt361.kt b/compiler/testData/diagnostics/tests/labels/kt361.kt new file mode 100644 index 00000000000..f0a4fef09f4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt361.kt @@ -0,0 +1,12 @@ +fun nonlocals(b : Boolean) { + @a{(): Int -> + fun foo() { + if (b) { + return@a 1 // The label must be resolved, but an error should be reported for a non-local return + } + } + + return@a 5 + } +} + diff --git a/compiler/testData/diagnostics/tests/labels/kt3920.kt b/compiler/testData/diagnostics/tests/labels/kt3920.kt new file mode 100644 index 00000000000..29a070c5fe8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt3920.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE +//KT-3920 Labeling information is lost when passing through some expressions + +fun test() { + run @f{ (): Int -> + val x = if (1 > 2) return@f 1 else 2 + 2 + } +} + +fun run(f: () -> T): T = f() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/kt3988.kt b/compiler/testData/diagnostics/tests/labels/kt3988.kt new file mode 100644 index 00000000000..55c97eaf2ff --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt3988.kt @@ -0,0 +1,21 @@ +//KT-3988 This@label for outer function not resolved + +class Comment() { + var article = "" + +} +class Comment2() { + var article2 = "" +} + +fun new(body: Comment.() -> Unit) = body + +fun new2(body: Comment2.() -> Unit) = body + +fun main(args: Array) { + new { + new2 { + this@new //UNRESOLVED REFERENCE + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/kt4247.kt b/compiler/testData/diagnostics/tests/labels/kt4247.kt new file mode 100644 index 00000000000..4160394e1f0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt4247.kt @@ -0,0 +1,11 @@ +//KT-4247 LABEL_NAME_CLASH + +fun foo(bar1: (String.() -> Int) -> Int) { + bar1 { + this.length + } + + bar1 { + this@bar1.length + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/kt4586.kt b/compiler/testData/diagnostics/tests/labels/kt4586.kt new file mode 100644 index 00000000000..e2d1eb488ff --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt4586.kt @@ -0,0 +1,17 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE +//KT-4586 this@ does not work for builders + +fun string(init: StringBuilder.() -> Unit): String{ + val answer = StringBuilder() + answer.init() + return answer.toString() +} + +val str = string @l{ + append("hello, ") + + val sub = string { + append("world!") + this@l.append(this) + } +} diff --git a/compiler/testData/diagnostics/tests/labels/kt4603.kt b/compiler/testData/diagnostics/tests/labels/kt4603.kt new file mode 100644 index 00000000000..41d17e014d5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt4603.kt @@ -0,0 +1,12 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE +//KT-4603 Labeling information is lost when passing through local classes or objects + +fun foo() { + val s = @l{ Int.() -> + class Local(val y: Int = this@l) { + fun bar() { + val x: Int = this@l //unresolved + } + } + } +} diff --git a/compiler/testData/diagnostics/tests/labels/kt591.kt b/compiler/testData/diagnostics/tests/labels/kt591.kt new file mode 100644 index 00000000000..9fb7f8fc60b --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/kt591.kt @@ -0,0 +1,11 @@ +//KT-591 Unresolved label in valid code + +fun test() { + val a = @a{(Int?).() -> + if (this != null) { + val b = {String.() -> + this@a.times(5) // @a Unresolved + } + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/labelReferencesInsideObjectExpressions.kt b/compiler/testData/diagnostics/tests/labels/labelReferencesInsideObjectExpressions.kt new file mode 100644 index 00000000000..9e958a6906e --- /dev/null +++ b/compiler/testData/diagnostics/tests/labels/labelReferencesInsideObjectExpressions.kt @@ -0,0 +1,25 @@ +trait A { + fun foo() +} + +trait B { + fun bar() +} + +fun B.b() { + object : A { + override fun foo() { + this@b.bar() + } + } +} + +fun test() { + @b { B.() -> + object : A { + override fun foo() { + this@b.bar() + } + } + } +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 9ca4a95b530..98e081d6f6d 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -33,7 +33,7 @@ import org.jetbrains.jet.checkers.AbstractJetDiagnosticsTest; @InnerTestClasses({JetDiagnosticsTestGenerated.Tests.class, JetDiagnosticsTestGenerated.Script.class, JetDiagnosticsTestGenerated.TailRecursion.class}) public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { @TestMetadata("compiler/testData/diagnostics/tests") - @InnerTestClasses({Tests.Annotations.class, Tests.BackingField.class, Tests.Cast.class, Tests.CheckArguments.class, Tests.ClassObjects.class, Tests.ControlFlowAnalysis.class, Tests.ControlStructures.class, Tests.CyclicHierarchy.class, Tests.DataClasses.class, Tests.DataFlow.class, Tests.DataFlowInfoTraversal.class, Tests.DeclarationChecks.class, Tests.DelegatedProperty.class, Tests.Deparenthesize.class, Tests.Enum.class, Tests.Evaluate.class, Tests.Extensions.class, Tests.FunctionLiterals.class, Tests.Generics.class, Tests.Imports.class, Tests.IncompleteCode.class, Tests.Inference.class, Tests.Infos.class, Tests.Inline.class, Tests.Inner.class, Tests.J_k.class, Tests.Jdk_annotations.class, Tests.Library.class, Tests.Multimodule.class, Tests.NamedArguments.class, Tests.NullabilityAndAutoCasts.class, Tests.NullableTypes.class, Tests.Numbers.class, Tests.Objects.class, Tests.OperatorsOverloading.class, Tests.Overload.class, Tests.Override.class, Tests.Recovery.class, Tests.Redeclarations.class, Tests.Regressions.class, Tests.Resolve.class, Tests.Scopes.class, Tests.SenselessComparison.class, Tests.Shadowing.class, Tests.SmartCasts.class, Tests.Substitutions.class, Tests.Subtyping.class, Tests.Suppress.class, Tests.ThisAndSuper.class, Tests.Typedefs.class, Tests.Unit.class, Tests.Varargs.class, Tests.When.class}) + @InnerTestClasses({Tests.Annotations.class, Tests.BackingField.class, Tests.Cast.class, Tests.CheckArguments.class, Tests.ClassObjects.class, Tests.ControlFlowAnalysis.class, Tests.ControlStructures.class, Tests.CyclicHierarchy.class, Tests.DataClasses.class, Tests.DataFlow.class, Tests.DataFlowInfoTraversal.class, Tests.DeclarationChecks.class, Tests.DelegatedProperty.class, Tests.Deparenthesize.class, Tests.Enum.class, Tests.Evaluate.class, Tests.Extensions.class, Tests.FunctionLiterals.class, Tests.Generics.class, Tests.Imports.class, Tests.IncompleteCode.class, Tests.Inference.class, Tests.Infos.class, Tests.Inline.class, Tests.Inner.class, Tests.J_k.class, Tests.Jdk_annotations.class, Tests.Labels.class, Tests.Library.class, Tests.Multimodule.class, Tests.NamedArguments.class, Tests.NullabilityAndAutoCasts.class, Tests.NullableTypes.class, Tests.Numbers.class, Tests.Objects.class, Tests.OperatorsOverloading.class, Tests.Overload.class, Tests.Override.class, Tests.Recovery.class, Tests.Redeclarations.class, Tests.Regressions.class, Tests.Resolve.class, Tests.Scopes.class, Tests.SenselessComparison.class, Tests.Shadowing.class, Tests.SmartCasts.class, Tests.Substitutions.class, Tests.Subtyping.class, Tests.Suppress.class, Tests.ThisAndSuper.class, Tests.Typedefs.class, Tests.Unit.class, Tests.Varargs.class, Tests.When.class}) public static class Tests extends AbstractJetDiagnosticsTest { @TestMetadata("Abstract.kt") public void testAbstract() throws Exception { @@ -4605,6 +4605,59 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { } } + @TestMetadata("compiler/testData/diagnostics/tests/labels") + public static class Labels extends AbstractJetDiagnosticsTest { + public void testAllFilesPresentInLabels() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/diagnostics/tests/labels"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("kt1703.kt") + public void testKt1703() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt1703.kt"); + } + + @TestMetadata("kt361.kt") + public void testKt361() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt361.kt"); + } + + @TestMetadata("kt3920.kt") + public void testKt3920() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt3920.kt"); + } + + @TestMetadata("kt3988.kt") + public void testKt3988() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt3988.kt"); + } + + @TestMetadata("kt4247.kt") + public void testKt4247() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt4247.kt"); + } + + @TestMetadata("kt4586.kt") + public void testKt4586() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt4586.kt"); + } + + @TestMetadata("kt4603.kt") + public void testKt4603() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt4603.kt"); + } + + @TestMetadata("kt591.kt") + public void testKt591() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/kt591.kt"); + } + + @TestMetadata("labelReferencesInsideObjectExpressions.kt") + public void testLabelReferencesInsideObjectExpressions() throws Exception { + doTest("compiler/testData/diagnostics/tests/labels/labelReferencesInsideObjectExpressions.kt"); + } + + } + @TestMetadata("compiler/testData/diagnostics/tests/library") public static class Library extends AbstractJetDiagnosticsTest { public void testAllFilesPresentInLibrary() throws Exception { @@ -7029,6 +7082,7 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { suite.addTest(Inner.innerSuite()); suite.addTestSuite(J_k.class); suite.addTest(Jdk_annotations.innerSuite()); + suite.addTestSuite(Labels.class); suite.addTestSuite(Library.class); suite.addTestSuite(Multimodule.class); suite.addTestSuite(NamedArguments.class);