Files
kotlin-fork/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.kt
T
Tianyu Geng fadde98a86 FIR: fix label stealling and crash with multiple labels
Consider the code below

```
fun test() {
  a@ b@ {
    {}
  }
}
```

Currently when the code is converted to FIR, label `b` is bound to the
outer lambda and `a` gets bound to the inner lambda because it's not
consumed. This is wrong and also leads transfromation to fail with
exceptions because of the unexpected consumption of `a`.

This change fixes the above issue by designating a specific node in the
AST as the allowed user of a label when the label is added.
2021-10-01 17:21:40 +03:00

72 lines
1.3 KiB
Kotlin
Vendored

// !LANGUAGE: -RestrictReturnStatementTarget
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class Ann
fun testFunctionName() {
return@testFunctionName
}
fun testHighOrderFunctionName() {
run {
return@run
}
}
fun testLambdaLabel() =
lambda@ {
return@lambda
}
fun testParenthesizedLambdaLabel() =
lambda@ ( {
return@lambda
} )
fun testAnnotatedLambdaLabel() =
lambda@ @Ann {
return@lambda
}
fun testLambdaMultipleLabels1() =
lambda1@ lambda2@ {
return@lambda1
}
fun testLambdaMultipleLabels2() =
lambda1@ lambda2@ {
return@lambda2
}
fun testAnonymousFunctionLabel() =
anonFun@ fun() {
return@anonFun
}
fun testLoopLabelInReturn(xs: List<Int>) {
L@ for (x in xs) {
if (x > 0) <!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!>
}
}
fun testValLabelInReturn() {
<!REDUNDANT_LABEL_WARNING!>L@<!> val fn = { <!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!> }
fn()
}
fun testHighOrderFunctionCallLabelInReturn() {
<!REDUNDANT_LABEL_WARNING!>L@<!> run {
<!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!>
}
}
fun testMultipleLabelsWithNestedLambda() {
l1@ l2@{
{
<!RETURN_NOT_ALLOWED!>return@l1<!>
}
return@l2
}
}