diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/LabelResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/LabelResolver.kt index 9acbd5a5baa..eef7fc2b983 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/LabelResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/LabelResolver.kt @@ -102,31 +102,29 @@ object LabelResolver { val labelName = expression.getLabelNameAsName() if (labelElement == null || labelName == null) return null + val element = resolveNamedLabel(labelName, labelElement, context.trace) + if (element != null) return element + val declarationsByLabel = context.scope.getDeclarationsByLabel(labelName) - val size = declarationsByLabel.size - - if (size > 1) { - BindingContextUtils.reportAmbiguousLabel(context.trace, labelElement, declarationsByLabel) - return null - } - if (size == 0) { - val element = resolveNamedLabel(labelName, labelElement, context.trace) - if (element == null) { + when (declarationsByLabel.size) { + 0 -> { context.trace.report(UNRESOLVED_REFERENCE.on(labelElement, labelElement)) + return null } - return element - } - - val declarationDescriptor = declarationsByLabel.iterator().next() - if (declarationDescriptor is FunctionDescriptor || declarationDescriptor is ClassDescriptor) { - val element = DescriptorToSourceUtils.descriptorToDeclaration(declarationDescriptor) - if (element is KtElement) { - context.trace.record(LABEL_TARGET, labelElement, element) - return element + 1 -> { + val declarationDescriptor = declarationsByLabel.single() + if (declarationDescriptor is FunctionDescriptor || declarationDescriptor is ClassDescriptor) { + val declarationElement = DescriptorToSourceUtils.descriptorToDeclaration(declarationDescriptor) + if (declarationElement is KtElement) { + context.trace.record(LABEL_TARGET, labelElement, declarationElement) + return declarationElement + } + } + } + else -> { + BindingContextUtils.reportAmbiguousLabel(context.trace, labelElement, declarationsByLabel) + return null } - } - else { - throw UnsupportedOperationException(declarationDescriptor.javaClass.toString()) // TODO } return null @@ -144,7 +142,7 @@ object LabelResolver { trace.report(LABEL_NAME_CLASH.on(labelExpression)) } - return list.single().also { trace.record(LABEL_TARGET, labelExpression, it) } + return list.first().also { trace.record(LABEL_TARGET, labelExpression, it) } } fun resolveThisOrSuperLabel( diff --git a/compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt b/compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt new file mode 100644 index 00000000000..ae2f251cc2c --- /dev/null +++ b/compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt @@ -0,0 +1,22 @@ +fun test1(): Boolean { + test1@ for(i in 1..2) { + continue@test1 + return false + } + + return true +} + +fun test2(): Boolean { + test2@ while (true) { + break@test2 + } + + return true +} + +fun box(): String { + if (!test1()) return "fail test1" + if (!test2()) return "fail test2" + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.kt b/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.kt new file mode 100644 index 00000000000..fb2f68d77b4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.kt @@ -0,0 +1,53 @@ +fun test1() { + test1@ for(i in 1..2) { + continue@test1 + } +} + +fun test2() { + test2@ while (true) { + break@test2 + } +} + +class Test3 { + fun Test3() { + Test3@ while (true) { + break@Test3 + } + } +} + +fun test4() { + break@test4 +} + +class Test5 { + fun Test5 { + return@Test5 + } +} + +class Test6 { + fun Test6() { + Test6@ while (true) { + break@Test6 + } + + Test6@ while (true) { + break@Test6 + } + } +} + +class Test7 { + fun Test7() { + Test8@ while (true) { + break@Test7 + } + + Test7@ while (true) { + break@Test8 + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.txt b/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.txt new file mode 100644 index 00000000000..e4a117d6cf1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.txt @@ -0,0 +1,37 @@ +package + +public fun test1(): kotlin.Unit +public fun test2(): kotlin.Unit +public fun test4(): kotlin.Unit + +public final class Test3 { + public constructor Test3() + public final fun Test3(): kotlin.Unit + 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 +} + +public final class Test5 { + public constructor Test5() + public final fun Test5(): kotlin.Unit + 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 +} + +public final class Test6 { + public constructor Test6() + public final fun Test6(): kotlin.Unit + 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 +} + +public final class Test7 { + public constructor Test7() + public final fun Test7(): kotlin.Unit + 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 +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index c260ef5159c..0d28e6ef1ba 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -10064,6 +10064,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/labels"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("controlLabelClashesWithFuncitonName.kt") + public void testControlLabelClashesWithFuncitonName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt"); + doTest(fileName); + } + @TestMetadata("labeledDeclarations.kt") public void testLabeledDeclarations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index e79f2fe5a8f..19ac0c20da6 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -4402,6 +4402,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("continueAndBreakLabelWithSameFunctionName.kt") + public void testContinueAndBreakLabelWithSameFunctionName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/continueAndBreakLabelWithSameFunctionName.kt"); + doTest(fileName); + } + @TestMetadata("emptyIf.kt") public void testEmptyIf() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/emptyIf.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 47a1415bc7d..afd5044084d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -10064,6 +10064,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/labels"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("controlLabelClashesWithFuncitonName.kt") + public void testControlLabelClashesWithFuncitonName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt"); + doTest(fileName); + } + @TestMetadata("labeledDeclarations.kt") public void testLabeledDeclarations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d2e17b54d1e..c914e338f5d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -10064,6 +10064,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/labels"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("controlLabelClashesWithFuncitonName.kt") + public void testControlLabelClashesWithFuncitonName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt"); + doTest(fileName); + } + @TestMetadata("labeledDeclarations.kt") public void testLabeledDeclarations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 661318e3ba8..e7c2e40d14e 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -11595,6 +11595,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/labels"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("controlLabelClashesWithFuncitonName.kt") + public void testControlLabelClashesWithFuncitonName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/controlLabelClashesWithFuncitonName.kt"); + doTest(fileName); + } + @TestMetadata("labeledDeclarations.kt") public void testLabeledDeclarations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations.kt");