Files
kotlin-fork/compiler/testData/diagnostics/tests/when/DuplicatedLabels.kt
T
Tianyu Geng 4915d8dda3 FIR checker: support DUPLICATE_LABEL_IN_WHEN
Changes from FE1.0:
1. As discussed previously, no expression evaluation happens during this
check.
2. FE1.0 doesn't check redundant object comparisons.
2021-09-10 07:07:42 +03:00

71 lines
1.6 KiB
Kotlin
Vendored

/*
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-313
* PRIMARY LINKS: expressions, when-expression -> paragraph 5 -> sentence 1
* expressions, when-expression -> paragraph 9 -> sentence 1
* expressions, when-expression -> paragraph 2 -> sentence 4
* expressions, when-expression -> paragraph 2 -> sentence 5
*/
package test
const val four = 4
fun first(arg: Int) = when (arg) {
1 -> 2
2 -> 3
<!DUPLICATE_LABEL_IN_WHEN!>1<!> -> 4
4 -> 5
<!DUPLICATE_LABEL_IN_WHEN!>1<!> -> 6
<!DUPLICATE_LABEL_IN_WHEN!>2<!> -> 7
// Error should be here: see KT-11971
four -> 8
else -> 0
}
fun second(arg: String): Int {
when (arg) {
"ABC" -> return 0
"DEF" -> return 1
<!DUPLICATE_LABEL_IN_WHEN!>"ABC"<!> -> return -1
<!DUPLICATE_LABEL_IN_WHEN!>"DEF"<!> -> return -2
}
return 42
}
fun third(arg: Any?): Int {
when (arg) {
null -> return -1
is String -> return 0
is Double -> return 1
is <!DUPLICATE_LABEL_IN_WHEN!>Double<!> -> return 2
<!DUPLICATE_LABEL_IN_WHEN!>null<!> -> return 3
!is String -> return 4
else -> return 5
}
}
enum class Color { RED, GREEN, BLUE }
fun fourth(arg: Color) = when (arg) {
Color.RED -> "RED"
Color.GREEN -> "GREEN"
<!DUPLICATE_LABEL_IN_WHEN!>Color.RED<!> -> "BLUE"
Color.BLUE -> "BLUE"
}
fun fifth(arg: Any?) = when (arg) {
is Any -> "Any"
<!ELSE_MISPLACED_IN_WHEN!>else<!> -> ""
<!UNREACHABLE_CODE!>else -> null<!>
}
object Foo
fun sixth(arg: Any?) = when (arg) {
Foo -> ""
Foo -> ""
else -> null
}