[FIR-TEST] Add new testdata generated after changes in previous commit

This commit is contained in:
Dmitriy Novozhilov
2019-12-11 16:16:22 +03:00
parent e9c02a1cca
commit 2536fa0cd5
4578 changed files with 104067 additions and 1 deletions
@@ -0,0 +1,22 @@
// !WITH_NEW_INFERENCE
// See also KT-7428
class Container<K>(val k: K)
// iterator() must be an extension, otherwise code will not compile
operator fun <K> Container<K>.iterator(): Iterator<K> = null!!
fun test() {
val container: Container<String>? = null
// Error
container.<!INAPPLICABLE_CANDIDATE!>iterator<!>()
// for extension iterator, this code compiles, but should not
<!INAPPLICABLE_CANDIDATE, UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>for (s in container) {}<!>
}
class OtherContainer<K>(val k: K) {
operator fun iterator(): Iterator<K> = null!!
}
fun test2() {
val other: OtherContainer<String>? = null
// Error
<!INAPPLICABLE_CANDIDATE, UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>for (s in other) {}<!>
}
@@ -0,0 +1,8 @@
// http://youtrack.jetbrains.net/issue/KT-552
// KT-552 For variable unresolved if loop body is not block
fun ff() {
var i = 1
for (j in 1..10)
i += j
}
@@ -0,0 +1,11 @@
fun foo1() = <!INFERENCE_ERROR, INFERENCE_ERROR, INFERENCE_ERROR!>while (b()) {}<!>
fun foo2() = <!UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>for (i in 10) {}<!>
fun foo3() = when (b()) {
true -> 1
else -> 0
}
fun b(): Boolean = true
@@ -0,0 +1,26 @@
// JAVAC_EXPECTED_FILE
// See KT-9816, KT-9742
// Not allowed in Java
class ZException<T>(val p: T) : Exception()
class YException<T>(val p: T): java.lang.RuntimeException()
class XException<T>(val p: T): Throwable()
fun bar() {
try {
throw ZException(11)
} catch (e: ZException<*>) {}
}
inline fun <reified E : Exception, R> tryCatch(lazy: () -> R, failure: (E) -> R): R =
try {
lazy()
} catch (e: E) {
failure(e)
}
fun <T : Throwable> tryCatch() {
try { } catch (e: T) { }
}
@@ -0,0 +1,41 @@
// !LANGUAGE: +ProhibitInnerClassesOfGenericClassExtendingThrowable
// !DIAGNOSTICS: -UNUSED_VARIABLE
// JAVAC_EXPECTED_FILE
class OuterGeneric<T> {
inner class ErrorInnerExn : Exception()
inner class InnerA {
inner class ErrorInnerExn2 : Exception()
}
class OkNestedExn : Exception()
val errorAnonymousObjectExn = object : Exception() {}
fun foo() {
class OkLocalExn : Exception()
val errorAnonymousObjectExn = object : Exception() {}
}
fun <X> genericFoo() {
class OkLocalExn : Exception()
class LocalGeneric<Y> {
inner class ErrorInnerExnOfLocalGeneric : Exception()
}
}
}
class Outer {
inner class InnerGeneric<T> {
inner class ErrorInnerExn : Exception()
}
}
fun <T> genericFoo() {
class ErrorLocalExnInGenericFun : Exception()
val errorkAnonymousObjectExnInGenericFun = object : Exception() {}
}
@@ -0,0 +1,41 @@
// !LANGUAGE: -ProhibitInnerClassesOfGenericClassExtendingThrowable
// !DIAGNOSTICS: -UNUSED_VARIABLE
// JAVAC_EXPECTED_FILE
class OuterGeneric<T> {
inner class ErrorInnerExn : Exception()
inner class InnerA {
inner class ErrorInnerExn2 : Exception()
}
class OkNestedExn : Exception()
val errorAnonymousObjectExn = object : Exception() {}
fun foo() {
class OkLocalExn : Exception()
val errorAnonymousObjectExn = object : Exception() {}
}
fun <X> genericFoo() {
class OkLocalExn : Exception()
class LocalGeneric<Y> {
inner class ErrorInnerExnOfLocalGeneric : Exception()
}
}
}
class Outer {
inner class InnerGeneric<T> {
inner class ErrorInnerExn : Exception()
}
}
fun <T> genericFoo() {
class ErrorLocalExnInGenericFun : Exception()
val errorkAnonymousObjectExnInGenericFun = object : Exception() {}
}
@@ -0,0 +1,3 @@
fun test() {
try { } catch (e: Exception = Exception()) { }
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +ProhibitInnerClassesOfGenericClassExtendingThrowable
package test
var global: Throwable? = null
fun <T> foo(x: Throwable, z: T, b: (T) -> Unit) {
class A(val y : T) : Exception()
try {
throw x
} catch (a: A) {
b(a.y)
} catch (e: Throwable) {
global = A(z)
}
}
fun main() {
foo(RuntimeException(), 1) { throw IllegalStateException() }
foo(global!!, "") { it.length } // (*)
}
// (*):
//Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
// at test.TestKt$main$2.invoke(test.kt)
// at test.TestKt.foo(test.kt:12)
// at test.TestKt.main(test.kt:21)
@@ -0,0 +1,30 @@
// KT-6774 Cannot find equals() when comparing with null
fun <T: Any> fn(t1: T, t2: T?) {
val x = if (true) t1 else t2
x == null
x?.equals(null)
x?.hashCode()
x.toString()
x!!.hashCode()
val y = t2 ?: t1
y == t1
y.equals(null)
y.hashCode()
y.toString()
y.hashCode()
}
interface Tr {
fun foo()
}
fun <T: Tr> fn(t1: T, t2: T?) {
val x = if (true) t1 else t2
x?.foo()
x!!.foo()
val y = t2 ?: t1
y.foo()
}
@@ -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<!SYNTAX!><!> {
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
}
}
}
@@ -0,0 +1,16 @@
fun foo(x: Unit) = x
fun test() {
if (false);
if (true);
val x = if (false);
foo(x)
val y: Unit = if (false);
foo(y)
foo(<!UNRESOLVED_REFERENCE!>{if (1==1);}()<!>)
return if (true);
}
@@ -0,0 +1,16 @@
class Coll {
operator fun iterator(): It = It()
}
class It {
operator fun next() = 1
operator fun hasNext() = false
}
fun test(c: Coll?) {
<!INAPPLICABLE_CANDIDATE, UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>for (x in c) {}<!>
if (c != null) {
for(x in c) {}
}
}
@@ -0,0 +1,12 @@
class Coll {
operator fun iterator(): It? = null
}
class It {
operator fun next() = 1
operator fun hasNext() = false
}
fun test() {
<!INAPPLICABLE_CANDIDATE, INAPPLICABLE_CANDIDATE!>for (x in Coll()) {}<!>
}
@@ -0,0 +1,55 @@
// !WITH_NEW_INFERENCE
// !CHECK_TYPE
// See also KT-10896: Wrong inference of if / else result type
interface Option<T>
class Some<T> : Option<T>
class None<T> : Option<T>
fun <T> bind(r: Option<T>): Option<T> {
return if (r is Some) {
// Ideally we should infer Option<T> here (see KT-10896)
(if (true) None() else r) checkType { <!UNRESOLVED_REFERENCE!>_<!><Option<T>>() }
// Works correctly
if (true) None() else r
}
else r
}
fun <T> bind2(r: Option<T>): Option<T> {
return if (r is Some) {
// Works correctly
if (true) None<T>() else r
}
else r
}
fun <T, R> bind3(r: Option<T>): Option<T> {
return if (r is Some) {
// Diagnoses an error correctly
if (true) None<R>() else r
}
else r
}
fun <T> bindWhen(r: Option<T>): Option<T> {
return when (r) {
is Some -> {
// Works correctly
if (true) None() else r
}
else -> r
}
}
interface SimpleOption
class SimpleSome : SimpleOption
class SimpleNone : SimpleOption
fun bindNoGeneric(r: SimpleOption): SimpleOption {
return if (r is SimpleSome) {
(if (true) SimpleNone() else r) checkType { <!UNRESOLVED_REFERENCE!>_<!><SimpleOption>() }
if (true) SimpleNone() else r
}
else r
}
@@ -0,0 +1,8 @@
// !WITH_NEW_INFERENCE
val test1 = { if (true) 1 else "" }
val test2 = { { if (true) 1 else "" } }
val test3: (Boolean) -> Any = { if (it) 1 else "" }
val test4: (Boolean) -> Any? = { if (it) 1 else "" }
@@ -0,0 +1,62 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
fun println() {}
fun foo(x: Any) {}
fun <T> fooGeneric(x: T) {}
fun testResultOfLambda1() =
run {
if (true) 42 else println()
}
fun testResultOfLambda2() =
run {
if (true) 42 else if (true) 42 else println()
}
fun testResultOfAnonFun1() =
run(fun () =
if (true) 42
else println()
)
fun testResultOfAnonFun2() =
run(fun () {
if (true) 42 else println()
})
fun testReturnFromAnonFun() =
run(fun () {
return if (true) 42 else println()
})
fun testReturn1() =
run {
return if (true) 42
else println()
}
fun testReturn2() =
run {
return if (true) 42
else if (true) 42
else println()
}
fun testUsage1() =
if (true) 42
else println()
fun testUsage2() =
foo(if (true) 42 else println())
fun testUsage2Generic() =
fooGeneric(if (true) 42 else println())
val testUsage3 =
if (true) 42
else println()
val testUsage4: Any get() =
if (true) 42 else println()
@@ -0,0 +1,31 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
fun println() {}
fun foo(x: Any) {}
fun <T> fooGeneric(x: T) {}
fun testMixedIfAndWhen() =
if (true)
when {
true -> if (true) 42
else 1
true -> if (true) 42
else println()
else -> if (true) println()
}
else println()
fun testWrappedExpressions() =
if (true) {
println()
if (true) {
println()
if (true) {
println()
}
else {}
}
}
else {
(((((42)) + 1)))
}
@@ -0,0 +1,86 @@
// !WITH_NEW_INFERENCE
fun idAny(x: Any) = x
fun <T> id(x: T) = x
fun idUnit(x: Unit) = x
class MList {
// MutableCollection<T>.add returns Boolean, but nobody cares
fun add(): Boolean = true
}
val mlist = MList()
fun work() {}
val xx1 = if (true) 42
val xx2: Unit = if (true) 42
val xx3 = idAny(if (true) 42)
val xx4 = id(if (true) 42)
val xx5 = idUnit(if (true) 42)
val xx6 = null ?: if (true) 42
val xx7 = "" + if (true) 42
val wxx1 = when { true -> 42 }
val wxx2: Unit = when { true -> 42 }
val wxx3 = idAny(when { true -> 42 })
val wxx4 = id(when { true -> 42 })
val wxx5 = idUnit(when { true -> 42 })
val wxx6 = null ?: when { true -> 42 }
val wxx7 = "" + when { true -> 42 }
val fn1 = { if (true) 42 }
val fn2 = { if (true) mlist.add() }
val fn3 = { if (true) work() }
val fn4 = { when { true -> 42 } }
val fn5 = { when { true -> mlist.add() } }
val fn6 = { when { true -> work() } }
val ufn1: () -> Unit = { if (true) 42 }
val ufn2: () -> Unit = { if (true) mlist.add() }
val ufn3: () -> Unit = { if (true) work() }
val ufn4: () -> Unit = { when { true -> 42 } }
val ufn5: () -> Unit = { when { true -> mlist.add() } }
val ufn6: () -> Unit = { when { true -> work() } }
fun f1() = if (true) work()
fun f2() = if (true) mlist.add()
fun f3() = if (true) 42
fun f4(): Unit = if (true) work()
fun f5(): Unit = if (true) mlist.add()
fun f6(): Unit = if (true) 42
fun g1() = when { true -> work() }
fun g2() = when { true -> mlist.add() }
fun g3() = when { true -> 42 }
fun g4(): Unit = when { true -> work() }
fun g5(): Unit = when { true -> mlist.add() }
fun g6(): Unit = when { true -> 42 }
fun foo1(x: String?) {
"" + if (true) 42
w@while (true) {
x ?: if (true) break
x ?: when { true -> break@w }
}
}
fun foo2() {
if (true) {
mlist.add()
}
else if (true) {
mlist.add()
}
else if (true) {
mlist.add()
}
when {
true -> mlist.add()
else -> when {
true -> mlist.add()
else -> when {
true -> mlist.add()
}
}
}
}
@@ -0,0 +1,34 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun example() {
val a = if (true) true else false
val b = if (true) else false
val c = if (true) true
val d = if (true) true else;
val e = if (true) {} else false
val f = if (true) true else {}
<!UNRESOLVED_REFERENCE!>{
if (true) true
}()<!>;
<!UNRESOLVED_REFERENCE!>{
if (true) true else false
}()<!>;
<!UNRESOLVED_REFERENCE!>{
if (true) {} else false
}()<!>;
<!UNRESOLVED_REFERENCE!>{
if (true) true else {}
}()<!>
fun t(): Boolean {
return if (true) true
}
return if (true) true else {}
}
@@ -0,0 +1,17 @@
fun call(f: () -> Unit) = f()
fun f1() {
outer@ while (true) {
call {
break@outer
}
}
}
fun f2() {
do {
fun inner() {
continue
}
} while (true)
}
@@ -0,0 +1,10 @@
fun test1() {
run {
if (true) {
if (true) {}
}
else {
1
}
}
}
@@ -0,0 +1,10 @@
fun fn(c: Char?): Any? =
if (c == null) TODO()
else when (c) {
'a' -> when (c) {
'B' -> 1
'C' -> "sdf"
else -> TODO()
}
else -> TODO()
}
@@ -0,0 +1,47 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNREACHABLE_CODE -UNUSED_PARAMETER -RETURN_NOT_ALLOWED
fun test1() = run {
return "OK"
}
fun test2() = run {
fun local(): String {
return ""
}
return ""
}
inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> = null!!
fun test3(a: List<String>, b: List<Int>) = a.map {
if (it.length == 3) return null
if (it.length == 4) return ""
if (it.length == 4) return 5
if (it.length == 4) return b
1
}
fun test4() = run {
fun test5() {
return
return@test4
return return@test4
return fun() { return; return@test4 "" }
}
return
3
}
val foo: Int
get() = run {
if (true) return ""
return
}
fun test(): Int = run {
return ""
}
@@ -0,0 +1,12 @@
// !WITH_NEW_INFERENCE
package kt1075
//KT-1075 No type check for 'in range' condition in 'when' expression
fun foo(b: String) {
if (b <!INAPPLICABLE_CANDIDATE!>in<!> 1..10) {} //type mismatch
when (b) {
<!INAPPLICABLE_CANDIDATE!>in<!> 1..10 -> 1 //no type mismatch, but it should be here
else -> 2
}
}
@@ -0,0 +1,16 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_EXPRESSION
// !CHECK_TYPE
// Issue: KT-30406
interface Option<out T> {
val s: String
}
class Some<T>(override val s: String) : Option<T>
class None(override val s: String = "None") : Option<Int>
fun test(a: Int): Option<Any> =
if (a == 239)
Some("239")
else
None()
@@ -0,0 +1,12 @@
// !WITH_NEW_INFERENCE
package f
fun test(a: Boolean, b: Boolean): Int {
return if(a) {
1
} else {
if (b) {
3
}
}
}
@@ -0,0 +1,20 @@
//KT-657 Semantic checks for when without condition
package kt657
class Pair<A, B>(a: A, b: B)
fun foo() =
when {
cond1() -> 12
cond2() -> 2
4 -> 34
Pair(1, 2) -> 3
in 1..10 -> 34
4 -> 38
is Int -> 33
else -> 34
}
fun cond1() = false
fun cond2() = true
@@ -0,0 +1,163 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNREACHABLE_CODE
package kt770_351_735
//KT-770 Reference is not resolved to anything, but is not marked unresolved
fun main() {
var i = 0
when (i) {
1 -> i--
2 -> i = 2 // i is surrounded by a black border
else -> <!UNRESOLVED_REFERENCE!>j<!> = 2
}
System.out.println(i)
}
//KT-351 Distinguish statement and expression positions
val w = <!INFERENCE_ERROR, INFERENCE_ERROR, INFERENCE_ERROR!>while (true) {}<!>
fun foo() {
var z = 2
val r = { // type fun(): Any is inferred
if (true) {
2
}
else {
z = 34
}
}
val f: ()-> Int = r
val g: ()-> Any = r
}
//KT-735 Statements without braces are prohibited on the right side of when entries.
fun box() : Int {
val d = 2
var z = 0
when(d) {
5, 3 -> z++
else -> z = -1000
}
return z
}
//More tests
fun test1() { while(true) {} }
fun test2(): Unit { while(true) {} }
fun testCoercionToUnit() {
val simple: ()-> Unit = {
41
}
val withIf: ()-> Unit = {
if (true) {
3
} else {
45
}
}
val i = 34
val withWhen : () -> Unit = {
when(i) {
1 -> {
val d = 34
"1"
doSmth(d)
}
2 -> '4'
else -> true
}
}
var x = 43
val checkType = {
if (true) {
x = 4
} else {
45
}
}
val f : () -> String = checkType
}
fun doSmth(i: Int) {}
fun testImplicitCoercion() {
val d = 21
var z = 0
var i = when(d) {
3 -> null
4 -> { val z = 23 }
else -> z = 20
}
var u = when(d) {
3 -> {
z = 34
}
else -> z--
}
var iff = if (true) {
z = 34
}
val g = if (true) 4
val h = if (false) 4 else {}
<!INAPPLICABLE_CANDIDATE!>bar<!>(if (true) {
4
}
else {
z = 342
})
}
fun fooWithAnyArg(arg: Any) {}
fun fooWithAnyNullableArg(arg: Any?) {}
fun testCoercionToAny() {
val d = 21
val x1: Any = if (1>2) 1 else 2.0
val x2: Any? = if (1>2) 1 else 2.0
val x3: Any? = if (1>2) 1 else (if (1>2) null else 2.0)
fooWithAnyArg(if (1>2) 1 else 2.0)
fooWithAnyNullableArg(if (1>2) 1 else 2.0)
fooWithAnyNullableArg(if (1>2) 1 else (if (1>2) null else 2.0))
val y1: Any = when(d) { 1 -> 1.0 else -> 2.0 }
val y2: Any? = when(d) { 1 -> 1.0 else -> 2.0 }
val y3: Any? = when(d) { 1 -> 1.0; 2 -> null; else -> 2.0 }
fooWithAnyArg(when(d) { 1 -> 1.0 else -> 2.0 })
fooWithAnyNullableArg(when(d) { 1 -> 1.0 else -> 2.0 })
fooWithAnyNullableArg(when(d) { 1 -> 1.0; 2 -> null; else -> 2.0 })
}
fun fooWithAnuNullableResult(s: String?, name: String, optional: Boolean): Any? {
return if (s == null) {
if (!optional) {
throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("Parameter '$name' was not found in the request")
}
null
} else {
name
}
}
fun bar(a: Unit) {}
fun testStatementInExpressionContext() {
var z = 34
val a1: Unit = <!INFERENCE_ERROR!>z = 334<!>
val f = for (i in 1..10) {}
if (true) return <!INFERENCE_ERROR, INFERENCE_ERROR!>z = 34<!>
return <!INFERENCE_ERROR, INFERENCE_ERROR!>while (true) {}<!>
}
fun testStatementInExpressionContext2() {
val a2: Unit = <!INFERENCE_ERROR!>while(true) {}<!>
}
@@ -0,0 +1,26 @@
package kt786
//KT-786 Exception on incomplete code with 'when'
fun foo() : Int {
val d = 2
var z = 0
when(d) {
5, 3 -> z++
else -> { z = -1000 }
return z -> 34
}
}
//test unreachable code
fun fff(): Int {
var d = 3
when(d) {
4 -> 21
return 2 -> return 47
bar() -> 45
444 -> true
}
return 34
}
fun bar(): Int = 8
@@ -0,0 +1,25 @@
//KT-799 Allow 'return' expressions in conditionals assigned to variables
package kt799
fun test() {
val a : Int = if (true) 6 else return // should be allowed
val b = if (true) 6 else return // should be allowed
doSmth(if (true) 3 else return)
<!INAPPLICABLE_CANDIDATE!>doSmth<!>(if (true) 3 else return, 1)
}
val a : Nothing = return 1
val b = return 1
val c = doSmth(if (true) 3 else return)
fun f(mi: Int = if (true) 0 else return) {}
fun doSmth(i: Int) {
}
@@ -0,0 +1,16 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNUSED_EXPRESSION
fun use(a: Any?) = a
fun test() {
{ }!!
use({ }!!);
// KT-KT-9070
{ } ?: 1
use({ 2 } ?: 1);
1 ?: { }
use(1 ?: { })
}
@@ -1,2 +1,2 @@
Failures detected in FirImplicitTypeBodyResolveTransformerAdapter, file: /localReturnInsidePropertyAccessor.kt
Failures detected in FirImplicitTypeBodyResolveTransformerAdapter, file: /localReturnInsidePropertyAccessor.fir.kt
Cause: kotlin.KotlinNullPointerException
@@ -0,0 +1,20 @@
interface ClassData
fun f() = object : ClassData {
val someInt: Int
get() {
return 5
}
}
fun g() = object : ClassData {
init {
if (true) {
return 0
}
}
fun some(): Int {
return 6
}
}
@@ -0,0 +1,62 @@
// !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) return@L
}
}
fun testValLabelInReturn() {
L@ val fn = { return@L }
fn()
}
fun testHighOrderFunctionCallLabelInReturn() {
L@ run {
return@L
}
}
@@ -0,0 +1,62 @@
// !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) return@L
}
}
fun testValLabelInReturn() {
L@ val fn = { return@L }
fn()
}
fun testHighOrderFunctionCallLabelInReturn() {
L@ run {
return@L
}
}
@@ -0,0 +1,39 @@
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class Ann
fun testLambdaLabel() = l@ { 42 }
fun testAnonymousFunctionLabel() = l@ fun() {}
fun testAnnotatedLambdaLabel() = lambda@ @Ann {}
fun testParenthesizedLambdaLabel() = lambda@ ( {} )
fun testLabelBoundToInvokeOperatorExpression() = l@ <!UNRESOLVED_REFERENCE!>{ 42 }()<!>
fun testLabelBoundToLambda() = <!UNRESOLVED_REFERENCE!>(l@ { 42 })()<!>
fun testWhileLoopLabel() {
L@ while (true) {}
}
fun testDoWhileLoopLabel() {
L@ do {} while (true)
}
fun testForLoopLabel(xs: List<Any>) {
L@ for (x in xs) {}
}
fun testValLabel() {
L@ val fn = {}
fn()
}
fun testHighOrderFunctionCallLabel() {
L@ run {}
}
fun testAnonymousObjectLabel() =
L@ object {}
@@ -0,0 +1,40 @@
// !WITH_NEW_INFERENCE
// FILE: J.java
import java.util.*;
public class J {
public static String s = null;
public static Map<String, String> m = null;
}
// FILE: k.kt
val testImplicitExclExcl1: String = J.s
val testImplicitExclExcl2: String? = J.s
val testImplicitExclExcl3: String = J.m[""]
val testImplicitExclExcl4: String? = J.m[""]
val testExclExcl1: String = J.s!!
val testExclExcl2: String? = J.s!!
val testExclExcl3: String = J.m[""]!!
val testExclExcl4: String? = J.m[""]!!
val testSafeCall1: String = J.s?.let { it }
val testSafeCall2: String? = J.s?.let { it }
val testSafeCall3: String = J.m[""]?.let { it }
val testSafeCall4: String? = J.m[""]?.let { it.toString() }
val testIf1: String = if (true) J.s else J.s
val testIf2: String? = if (true) J.s else J.s
val testIf3: String = if (true) J.m[""] else J.m[""]
val testIf4: String? = if (true) J.m[""] else J.m[""]
val testWhen1: String = when { else -> J.s }
val testWhen2: String? = when { else -> J.s }
val testWhen3: String = when { else -> J.m[""] }
val testWhen4: String? = when { else -> J.m[""] }
@@ -0,0 +1,20 @@
val ns: String? = null
val testElvis1: String? = ns ?: ""
val testElvis2: String = run { ns ?: "" }
val testElvis3: String? = run { ns ?: "" }
val testIf1: String? = if (true) "" else ""
val testIf2: String? = run { if (true) "" else "" }
val testIf3: String? = if (true) run { "" } else ""
val testIf4: String? = run { run { if (true) "" else "" } }
val testIf5: String? = run { if (true) run { "" } else "" }
val testWhen1: String? = when { else -> "" }
val testWhen2: String? = run { when { else -> "" } }
val testWhen3: String? = when { else -> run { "" } }
val testWhen4: String? = run { run { when { else -> "" } } }
val testWhen5: String? = run { when { else -> run { "" } } }
val testExcl1: String? = run { ns!! }
val testExcl2: String? = run { run { ns!! } }
@@ -0,0 +1,28 @@
// !WITH_NEW_INFERENCE
package a
fun foo() : Int {
try {
doSmth()
}
catch (e: Exception) {
return ""
}
finally {
return ""
}
}
fun bar() : Int =
try {
doSmth()
}
catch (e: Exception) {
""
}
finally {
""
}
fun doSmth() {}
@@ -0,0 +1,21 @@
// !WITH_NEW_INFERENCE
// !CHECK_TYPE
// FILE: A.java
public class A {
public static String foo() {
return "";
}
}
// FILE: b.kt
fun <T: Any> exclExcl(t: T?): T = t!!
fun test11() {
// not 'String!'
exclExcl(A.foo()) checkType { <!UNRESOLVED_REFERENCE!>_<!><String>() }
exclExcl(A.foo()) checkType { <!UNRESOLVED_REFERENCE!>_<!><String?>() }
// not 'String!'
A.foo()!! checkType { <!UNRESOLVED_REFERENCE!>_<!><String>() }
A.foo()!! checkType { <!UNRESOLVED_REFERENCE!>_<!><String?>() }
}
@@ -0,0 +1,13 @@
fun f() {
try {
} catch (val e: Exception) {
}
try {
} catch (var e: Exception) {
}
try {
} catch (e: Exception) {
}
}
@@ -0,0 +1,32 @@
class Pair {
operator fun component1(): Int = null!!
operator fun component2(): Int = null!!
}
class Coll {
operator fun iterator(): It = It()
}
class It {
operator fun next() = Pair()
operator fun hasNext() = false
}
fun f() {
for (val i in 1..4) {
}
for (var i in 1..4) {
}
for (val (i,j) in Coll()) {
}
for (var (i,j) in Coll()) {
}
}
@@ -0,0 +1,41 @@
//KT-234 Force when() expressions to have an 'else' branch
//KT-973 Unreachable code
package kt234_kt973
class Pair<A, B>(a: A, b: B)
fun test(t : Pair<Int, Int>) : Int {
when (t) {
Pair(10, 10) -> return 1
}
return 0 // unreachable code
}
fun test1(t : Pair<Int, Int>) : Int {
when (t) {
Pair(10, 10) -> return 1
else -> return 2
}
return 0 // unreachable code
}
//more tests
fun t1(x: Int) = when(x) {
else -> 1
}
fun t5(x: Int) = when (x) {
is Int -> 1
2 -> 2
}
fun foo3(x: Int) = when(x) {
else -> 1
2 -> 2
}
fun foo4(x: Int) = when(x) {
2 -> x
else -> 3
}
@@ -0,0 +1,17 @@
// !WITH_NEW_INFERENCE
val test1 = { when (true) { true -> 1; else -> "" } }
val test2 = { { when (true) { true -> 1; else -> "" } } }
val test3: (Boolean) -> Any = { when (true) { true -> 1; else -> "" } }
val test4: (Boolean) -> Any? = { when (true) { true -> 1; else -> "" } }
fun println() {}
val test5 = {
when (true) {
true -> println()
else -> println()
}
}
@@ -0,0 +1,83 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
fun println() {}
fun foo(x: Any) {}
fun <T> fooGeneric(x: T) {}
fun testResultOfLambda1() =
run {
when {
true -> 42
else -> println()
}
}
fun testResultOfLambda2() =
run {
when {
true -> 42
else ->
when {
true -> 42
else -> println()
}
}
}
fun testReturn1() =
run {
return when {
true -> 42
else -> println()
}
}
fun testReturn2() =
run {
return when {
true -> 42
else ->
when {
true -> 42
else -> println()
}
}
}
fun testUsage1() =
when {
true -> 42
else -> println()
}
fun testUsage2() =
foo(when {
true -> 42
else -> println()
})
fun testUsage2Generic() =
fooGeneric(when {
true -> 42
else -> println()
})
val testUsage3 =
when {
true -> 42
else -> println()
}
val testUsage4 =
when {
true -> 42
true -> 42
true -> 42
else -> println()
}
val testUsage5: Any get() =
when {
true -> 42
else -> println()
}