[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,349 @@
// !WITH_NEW_INFERENCE
package uninitialized_reassigned_variables
fun doSmth(s: String) {}
fun doSmth(i: Int) {}
// ------------------------------------------------
// uninitialized variables
fun t1(b : Boolean) {
val v : Int
if (v == 0) {}
var u: String
if (b) {
u = "s"
}
doSmth(u)
var r: String
if (b) {
r = "s"
}
else {
r = "tg"
}
doSmth(r)
var t: String
if (b)
doSmth(t)
else
t = "ss"
doSmth(t) //repeat for t
val i = 3
doSmth(i)
if (b) {
return;
}
doSmth(i)
if (i is Int) {
return;
}
}
fun t2() {
val s = "ss"
for (i in 0..2) {
doSmth(s)
}
}
class A() {}
fun t4(a: A) {
a = A()
}
// ------------------------------------------------
// reassigned vals
fun t1() {
val a : Int = 1
a = 2
var b : Int = 1
b = 3
}
enum class ProtocolState {
WAITING {
override fun signal() = ProtocolState.TALKING
},
TALKING {
override fun signal() = ProtocolState.WAITING
};
abstract fun signal() : ProtocolState
}
fun t3() {
val x: ProtocolState = ProtocolState.WAITING
x = x.signal()
x = x.signal() //repeat for x
}
fun t4() {
val x = 1
<!VARIABLE_EXPECTED!>x<!> += 2
val y = 3
<!VARIABLE_EXPECTED!>y<!> *= 4
var z = 5
z -= y
}
fun t5() {
for (i in 0..2) {
<!VARIABLE_EXPECTED!>i<!> += 1
fun t5() {
<!VARIABLE_EXPECTED!>i<!> += 3
}
}
}
// ------------------------------------------------
// backing fields
var x = 10
val y = 10
val z = 10
class AnonymousInitializers(var a: String, val b: String) {
init {
a = "30"
a = "s"
b = "3"
b = "tt" //repeat for b
}
val i: Int
init {
i = 121
}
init {
x = 11
z = 10
}
val j: Int
get() = 20
init {
i = 13
j = 34
}
val k: String
init {
if (1 < 3) {
k = "a"
}
else {
k = "b"
}
}
val l: String
init {
if (1 < 3) {
l = "a"
}
else {
l = "b"
}
}
val o: String
init {
if (1 < 3) {
o = "a"
}
}
var m: Int = 30
init {
m = 400
}
val n: Int
init {
while (n == 0) {
}
n = 10
while (n == 0) {
}
}
var p = 1
init {
p++
}
}
fun reassignFunParams(a: Int) {
a = 1
}
open class Open(a: Int, w: Int) {}
class LocalValsVsProperties(val a: Int, w: Int) : Open(a, w) {
val x : Int
val y : Int
init {
x = 1
val b = x
}
val b = a
fun foo() {
val r : Int
doSmth(x)
doSmth(y)
doSmth(r)
doSmth(a)
}
var xx = w
var yy : Int
init {
<!VARIABLE_EXPECTED!>w<!> += 1
yy = w
}
}
class Outer() {
val a : Int
var b : Int
init {
a = 1
b = 1
}
inner class Inner() {
init {
a++
b++
}
}
fun foo() {
a++
b++
}
}
class ForwardAccessToBackingField() { //kt-147
val a = a // error
val b = c // error
val c = 1
}
class ClassObject() {
companion object {
val x : Int
init {
x = 1
}
fun foo() {
val a : Int
doSmth(a)
}
}
}
fun foo() {
val a = object {
val x : Int
val y : Int
val z : Int
init {
x = 1
z = 3
}
fun foo() {
y = 10
z = 13
}
}
}
class TestObjectExpression() {
val a : Int
fun foo() {
val a = object {
val x : Int
val y : Int
init {
if (true)
x = 12
else
x = 1
}
fun inner1() {
y = 101
a = 231
}
fun inner2() {
y = 101
a = 231
}
}
}
}
object TestObjectDeclaration {
val x : Int
val y : Int
init {
x = 1
}
fun foo() {
y = 10
val i: Int
if (1 < 3) {
i = 10
}
doSmth(i)
}
}
fun func() {
val b = 1
val a = object {
val x = b
init {
b = 4
}
}
}
// ------------------------------------------------
// dot qualifiers
class M() {
val x = 11
var y = 12
}
fun test(m : M) {
m.x = 23
m.y = 23
}
fun test1(m : M) {
m.x++
m.y--
}
@@ -0,0 +1,10 @@
fun test5() {
var a: Int
try {
a = 3
}
finally {
a = 5
}
a.hashCode()
}
@@ -0,0 +1,67 @@
// Tests for KT-13597 (val assignment inside local object in constructor)
class Test {
val a: String
init {
val t = object {
fun some() {
// See KT-13597
a = "12"
}
}
a = "2"
t.some()
}
}
class Test2 {
init {
val t = object {
fun some() {
a = "12"
}
}
a = "2"
t.some()
}
val a: String
}
// Tests for KT-14381 (val assignment inside lambda in constructor)
fun <T> exec(f: () -> T): T = f()
class Test4 {
val a: String
init {
exec {
// See KT-14381
a = "12"
}
a = "34"
}
}
// Additional tests to prevent something broken
class Test5 {
val y: Int
val z: String
init {
val x: String
<!UNRESOLVED_REFERENCE!>x<!> = ""
z = <!UNRESOLVED_REFERENCE!>x<!>
}
constructor(y: Int) {
this.y = y
}
}
@@ -0,0 +1,23 @@
// !LANGUAGE: +RestrictionOfValReassignmentViaBackingField
package a
import java.util.HashSet
val a: MutableSet<String>? = null
get() {
if (a == null) {
field = HashSet()
}
return a
}
class R {
val b: String? = null
get() {
if (b == null) {
field = "b"
}
return b
}
}
@@ -0,0 +1,23 @@
// !LANGUAGE: -RestrictionOfValReassignmentViaBackingField
package a
import java.util.HashSet
val a: MutableSet<String>? = null
get() {
if (a == null) {
field = HashSet()
}
return a
}
class R {
val b: String? = null
get() {
if (b == null) {
field = "b"
}
return b
}
}
@@ -0,0 +1,24 @@
fun foo() {
outer@while (true) {
try {
while (true) {
continue@outer
}
} finally {
break
}
}
"OK".hashCode()
}
fun bar(): String {
outer@while (true) {
try {
while (true) {
continue@outer
}
} finally {
return "OK"
}
}
}
@@ -0,0 +1,86 @@
fun test() {
while (true) {
fun local1() {
break
}
}
}
fun test2() {
while (true) {
{
continue
}
}
}
fun test3() {
while (true) {
class LocalClass {
init {
continue
}
fun foo() {
break
}
}
}
}
fun test4() {
while (true) {
object: Any() {
init {
break
}
}
}
}
fun test5() {
while (true) {
class LocalClass(val x: Int) {
constructor() : this(42) {
break
}
constructor(y: Double) : this(y.toInt()) {
continue
}
}
}
}
fun test6() {
while (true) {
class LocalClass(val x: Int) {
init {
break
}
init {
continue
}
}
}
}
fun test7() {
while (true) {
class LocalClass {
val x: Int = if (true) {
break
}
else {
continue
}
}
}
}
fun test8() {
while (true) {
class LocalClass(val x: Int) {
constructor() : this(if (true) { 42 } else { break })
}
}
}
@@ -0,0 +1,34 @@
fun test() {
l@ for (i in if (true) 1..10 else continue@l) {}
for (i in if (true) 1..10 else continue) {}
while (break) {}
l@ while (break@l) {}
do {} while (continue)
l@ do {} while (continue@l)
//KT-5704
var i = 0
while (if(i++ == 10) break else continue) {}
}
fun test2(b: Boolean) {
while (b) {
while (break) {}
}
do {
while (continue) {}
} while (b)
while (b) {
do {} while (break)
}
for (i in 1..10) {
for (j in if (true) 1..10 else continue) {
}
}
}
@@ -0,0 +1,12 @@
package c
fun test() {
val x = 10
fun inner1() {
fun inner2() {
fun inner3() {
val y = x
}
}
}
}
@@ -0,0 +1,38 @@
package d
val a: Int
get() {
val b: Int
val c: Int
42
fun bar(): Int {
val d: Int
42
return d
}
return b
}
class A {
val a: Int
get() {
val b: Int
val c: Int
42
return b
}
fun foo() {
class B {
val a: Int
get() {
val b: Int
val c: Int
42
return b
}
}
}
}
@@ -0,0 +1,8 @@
// See KT-12809
open class A(val a: Any) {
override fun toString() = a.toString()
}
object B : A(B.foo) { // call B.foo should be not-allowed
val foo = 4
}
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCommasAndWhitespaces() {
fun bar(i: Int, s: String, x: Any) {}
bar( 1 , todo() , "" )
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,22 @@
package a
fun test1() {
bar(
11,
todo(),//comment1
""//comment2
)
}
fun test2() {
bar(11, todo()/*comment1*/, ""/*comment2*/)
}
fun test3() {
bar(11, l@(todo()/*comment*/), "")
}
fun todo(): Nothing = throw Exception()
fun bar(i: Int, s: String, a: Any) {}
@@ -0,0 +1,11 @@
fun testInvoke() {
operator fun Nothing.invoke(): Nothing = this
todo()()
}
fun testInvokeWithLambda() {
operator fun Nothing.invoke(i: Int, f: () -> Int) = f
todo()(1){ 42 }
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,11 @@
fun test11() {
fun Any.bar(i: Int) {}
todo().bar(1)
}
fun test12() {
fun Any.bar(i: Int) {}
todo()?.bar(1)
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,168 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun t1() : Int{
return 0
1
}
fun t1a() : Int {
return
return 1
1
}
fun t1b() : Int {
return 1
return 1
1
}
fun t1c() : Int {
return 1
return
1
}
fun t2() : Int {
if (1 > 2)
return 1
else return 1
1
}
fun t2a() : Int {
if (1 > 2) {
return 1
1
} else { return 1
2
}
1
}
fun t3() : Any {
if (1 > 2)
return 2
else return ""
1
}
fun t4(a : Boolean) : Int {
do {
return 1
}
while (a)
1
}
fun t4break(a : Boolean) : Int {
do {
break
}
while (a)
return 1
}
fun t5() : Int {
do {
return 1
2
}
while (1 > 2)
return 1
}
fun t6() : Int {
while (1 > 2) {
return 1
2
}
return 1
}
fun t6break() : Int {
while (1 > 2) {
break
2
}
return 1
}
fun t7(b : Int) : Int {
for (i in 1..b) {
return 1
2
}
return 1
}
fun t7break(b : Int) : Int {
for (i in 1..b) {
return 1
2
}
return 1
}
fun t7() : Int {
try {
return 1
2
}
catch (e : Any) {
2
}
return 1 // this is OK, like in Java
}
fun t8() : Int {
try {
return 1
2
}
catch (e : Any) {
return 1
2
}
return 1
}
fun blockAndAndMismatch() : Boolean {
(return true) || (return false)
return true
}
fun tf() : Int {
try {return 1} finally{return 1}
return 1
}
fun failtest(a : Int) : Int {
if (fail() || true) {
}
return 1
}
fun foo(a : Nothing) : Unit {
1
a
2
}
fun fail() : Nothing {
throw java.lang.<!UNRESOLVED_REFERENCE!>RuntimeException<!>()
}
fun nullIsNotNothing() : Unit {
val x : Int? = 1
if (x != null) {
return
}
fail()
}
fun returnInWhile(a: Int) {
do {return}
while (1 > a)
}
@@ -0,0 +1,23 @@
package c
fun test1() {
val r: Nothing = null!!
}
fun test2(a: A) {
a + a
bar()
}
fun test3() {
null!!
bar()
}
fun throwNPE(): Nothing = null!!
class A {
operator fun plus(a: A): Nothing = throw Exception()
}
fun bar() {}
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArrayAccess1(array: Array<Any>) {
array[todo()]
}
fun testArrayAccess2() {
operator fun Nothing.get(i: Int, s: String) {}
todo()[1, ""]
}
fun testAraryAccess3() {
operator fun Nothing.get(n: Nothing) {}
todo()[todo()]
}
fun testArrayAssignment1(array: Array<Any>) {
array[todo()] = 11
}
fun testArrayAssignment2(array: Array<Any>) {
array[1] = todo()
}
fun testArrayAssignment3(n: Nothing) {
operator fun Nothing.set(i: Int, j: Int) {}
n[1] = 2
}
fun testArrayAssignment4(n: Nothing) {
operator fun Nothing.set(i: Int, a: Any) {}
n[1] = todo()
}
fun testArrayPlusAssign(array: Array<Any>) {
operator fun Any.plusAssign(a: Any) {}
array[1] += todo()
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,18 @@
fun testAssignment() {
var a = 1
a = todo()
}
fun testVariableDeclaration() {
val a = todo()
}
fun testPlusAssign() {
operator fun Int.plusAssign(i: Int) {}
var a = 1
a += todo()
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,39 @@
fun testBinary1() {
operator fun Int.times(s: String) {}
todo() * ""
}
fun testBinary2() {
"1" + todo()
}
fun testElvis1() {
todo() ?: ""
}
fun testElvis2(s: String?) {
s ?: todo()
bar()
}
fun testAnd1(b: Boolean) {
b && todo()
bar()
}
fun testAnd2(b: Boolean) {
todo() && b
}
fun returnInBinary1(): Boolean {
(return true) && (return false)
}
fun returnInBinary2(): Boolean {
(return true) || (return false)
}
fun todo(): Nothing = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArgumentInCall() {
fun bar(i: Int, s: String, x: Any) {}
bar(1, todo(), "")
}
fun testArgumentInVariableAsFunctionCall(f: (Any) -> Unit) {
f(todo())
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,24 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun unreachable0() {
return
return todo()
}
fun unreachable2() {
return
val a = todo()
}
fun unreachable3() {
return
bar(todo())
}
fun unreachable4(array: Array<Any>) {
return
array[todo()]
}
fun bar(a: Any) {}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,12 @@
fun testIf() {
if (todo()) 1 else 2
}
fun testIf1(b: Boolean) {
if (b) todo() else 1
bar()
}
fun todo(): Nothing = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCompound() {
operator fun Nothing.get(i: Int) {}
todo()!![12]
}
fun testCompound1() {
operator fun Int.times(s: String): Array<String> = throw Exception()
(todo() * "")[1]
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,36 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testObject() {
object : Foo(todo()) {
fun foo() = 1
}
}
fun testObjectExpression() {
val a = object : Foo(todo()) {
fun foo() = 1
}
}
fun testObjectExpression1() {
fun bar(i: Int, x: Any) {}
bar(1, object : Foo(todo()) {
fun foo() = 1
})
}
fun testClassDeclaration() {
class C : Foo(todo()) {}
bar()
}
fun testFunctionDefaultArgument() {
fun foo(x: Int = todo()) { bar() }
}
open class Foo(i: Int) {}
fun todo(): Nothing = throw Exception()
fun bar() {}
@@ -0,0 +1,21 @@
fun testFor() {
operator fun Nothing.iterator() = (0..1).iterator()
for (i in todo()) {}
}
fun testWhile() {
while (todo()) {
}
}
fun testDoWhile() {
do {
} while(todo())
bar()
}
fun todo(): Nothing = throw Exception()
fun bar() {}
@@ -0,0 +1,5 @@
fun testReturn() {
return todo()
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,15 @@
fun testPrefix() {
operator fun Any.not() {}
!todo()
}
fun testPostfixWithCall(n: Nothing) {
operator fun Nothing.inc(): Nothing = this
n++
}
fun testPostfixSpecial() {
todo()!!
}
fun todo(): Nothing = throw Exception()
@@ -0,0 +1,55 @@
fun foo(a: Any) {}
fun bar(a: Any, b: Any) {}
fun test(arr: Array<Int>) {
while (true) {
foo(break)
}
while (true) {
bar(arr, break)
}
while (true) {
arr[break]
}
while (true) {
arr[1] = break
}
while (true) {
break
foo(1)
}
while (true) {
var x = 1
break
x = 2
}
while (true) {
var x = 1
x = break
}
// TODO: bug, should be fixed in CFA
while (true) {
if (1 > 2 && break && 2 > 3) {
}
}
// TODO: bug, should be fixed in CFA
while (true) {
if (1 > 2 || break || 2 > 3) {
}
}
while (true) {
break ?: null
}
}
@@ -0,0 +1,8 @@
fun main() {
"".run {
""
}
}
fun <T> T.run(f: (T) -> Unit): Unit = f(this)
@@ -0,0 +1,12 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(x: String): String {
try {
throw RuntimeException()
} finally {
try {
} catch (e: Exception) {
}
return x // <- Wrong UNREACHABLE_CODE
}
}
@@ -0,0 +1,23 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo() {
try {
throw RuntimeException()
} catch (e: Exception) {
return // <- Wrong UNREACHABLE_CODE
} finally {
while (true);
}
}
fun bar() {
try {
throw RuntimeException()
} catch (e: Exception) {
return // <- Wrong UNREACHABLE_CODE
} finally {
while (cond());
}
}
fun cond() = true
@@ -0,0 +1,9 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(x: String): String {
try {
throw RuntimeException() //should be marked as unreachable, but is not
} finally {
throw NullPointerException()
}
}
@@ -0,0 +1,12 @@
//KT-3162 More precise try-finally error marking
fun foo(x: String) : String {
val a = try {
x
} finally {
try {
} catch (e: Exception) {
}
return x
}
}
@@ -0,0 +1,25 @@
//KT-5200 Mark unreachable code in lambdas
fun test1(): String {
doCall local@ {
throw NullPointerException()
"b3" //unmarked
}
return "OK"
}
fun test2(nonLocal: String, b: Boolean): String {
doCall local@ {
if (b) {
return@local "b1"
} else {
return@local "b2"
}
"b3" //unmarked
}
return nonLocal
}
inline fun doCall(block: ()-> String) = block()
@@ -0,0 +1,11 @@
inline fun myRun(b: () -> Unit) = b()
fun foo() {
var a: Int
return
myRun {
return
}
}
@@ -0,0 +1,13 @@
interface X {
fun f(): Boolean
}
val m = object : X {
override fun f(): Boolean {
}
fun foo() {
fun local(): Int {
}
}
}
@@ -0,0 +1,17 @@
package f
//KT-3444 Front-end doesn't check if local function or function of anonymous class returns value
fun box(): Int {
fun local(): Int {
}
return local()
}
fun main() {
box()
}
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// KT-4034 An expression of type Nothing may not affect 'definite return' analysis
interface JavaClassifierType
interface TypeUsage
interface JetType
private fun transformClassifierType(classifierType: JavaClassifierType, howThisTypeIsUsed: TypeUsage): JetType? {
null!!
}
@@ -0,0 +1,19 @@
// FILE: a.kt
package test
class A {}
// FILE: b.kt
package test.p; class C {fun f() {}}
// FILE: c.kt
package test.p; open class G<T> {open fun f(): T {} fun a() {}}
// FILE: d.kt
package test.p; class G2<E> : G<E> { fun g() : E {} override fun f() : E {}}
// FILE: e.kt
package test.p; fun foo() {}
// FILE: f.kt
package test.p; fun foo(a: C) {}
@@ -0,0 +1,6 @@
fun illegalWhenBlock(a: Any): Int {
when(a) {
is Int -> return a
is String -> return a.length
}
}
@@ -0,0 +1,32 @@
// See also KT-10869: Accessing lazy properties from init causes IllegalArgumentException
import kotlin.reflect.KProperty
class CustomDelegate {
operator fun getValue(thisRef: Any?, prop: KProperty<*>): String = prop.name
}
class Kaboom() {
// Here and below we should have errors for simple AND delegated
init {
delegated.hashCode()
simple.hashCode()
withGetter.hashCode()
}
val other = delegated
val another = simple
val something = withGetter
val delegated: String by CustomDelegate()
val simple = "xyz"
val withGetter: String
get() = "abc"
// No error should be here
val after = delegated
}
@@ -0,0 +1,23 @@
// See KT-15334: incorrect reassignment in do...while
fun test() {
do {
val s: String
s = ""
} while (s == "")
}
fun test2() {
do {
val s: String
s = "1"
s = s + "2"
} while (s == "1")
}
fun test3() {
val s: String
do {
s = ""
} while (s != "")
}
@@ -0,0 +1,6 @@
fun test(cond1: Boolean) {
do {
if (cond1) continue
val cond2 = false
} while (cond2) // cond2 may be not defined here
}
@@ -0,0 +1,39 @@
// !WITH_NEW_INFERENCE
// See KT-8277
// NI_EXPECTED_FILE
val v = { true } ?: ( { true } ?:null!! )
val w = if (true) {
{ true }
}
else {
{ true } ?: null!!
}
val ww = if (true) {
{ true } ?: null!!
}
else if (true) {
{ true } ?: null!!
}
else {
null!!
}
val n = null ?: (null ?: { true })
fun l(): (() -> Boolean)? = null
val b = null ?: ( l() ?: false)
val bb = null ?: ( l() ?: null!!)
val bbb = null ?: ( l() ?: null)
val bbbb = ( l() ?: null) ?: ( l() ?: null)
fun f(x : Long?): Long {
var a = x ?: (fun() {} ?: fun() {})
return a
}
@@ -0,0 +1,55 @@
enum class B(val x: Int) {
B1(1),
B2(2);
companion object {
val SUM = B1.x + B2.x
val COPY = B1
}
}
enum class C(val x: Int) {
C1(SUM),
C2(1);
companion object {
val COPY = C2
val SUM = C1.x + COPY.x
}
}
// From KT-11769
enum class Fruit(personal: Int) {
APPLE(1);
companion object {
val common = 20
}
val score = personal + common
}
// Another example from KT-11769
enum class EnumCompanion1(val x: Int) {
INSTANCE(<!AMBIGUITY!>Companion<!>.<!UNRESOLVED_REFERENCE!>foo<!>()),
ANOTHER(foo());
companion object {
fun foo() = 42
}
}
// Also should be reported for implicit receiver
enum class EnumCompanion2(val x: Int) {
INSTANCE(foo());
companion object {
fun foo() = 42
}
}
// But not for another enum
enum class EnumCompanion3(val x: Int) {
INSTANCE(EnumCompanion1.foo()),
ANOTHER(EnumCompanion2.foo());
companion object
}
@@ -0,0 +1,35 @@
enum class A(val v: A) {
A1(A2),
A2(A1)
}
enum class D(val x: Int) {
D1(D2.x),
D2(D1.x)
}
enum class E(val v: Int) {
// KT-11769 related: there is no predictable initialization order for enum entry with non-companion object
E1(Nested.COPY);
object Nested {
val COPY = E1.v
}
}
// From KT-13322: cross reference should not be reported here
object Object1 {
val y: Any = Object2.z
object Object2 {
val z: Any = Object1.y
}
}
// From KT-6054
enum class MyEnum {
A, B;
val x = when(this) {
A -> 1
B -> 2
else -> 3
}
}
@@ -0,0 +1,37 @@
// !WITH_NEW_INFERENCE
// See KT-15566
// NI_EXPECTED_FILE
import DefaultHttpClient.client
interface HttpClient
class HttpClientImpl : HttpClient
// Below we should have initialization error for both (!) delegates
object DefaultHttpClient : HttpClient by client {
val client = HttpClientImpl()
}
object DefaultHttpClientWithGetter : HttpClient by client {
val client get() = HttpClientImpl()
}
object DefaultHttpClientWithFun : HttpClient by fClient() {
}
private fun fClient() = HttpClientImpl()
private fun <T> lazy(init: () -> T): kotlin.Lazy<T> {
init()
null!!
}
object DefaultHttpClientWithBy : HttpClient by client {
val client by lazy { HttpClientImpl() }
}
object DefaultFqHttpClient : HttpClient by DefaultFqHttpClient.client {
val client = HttpClientImpl()
}
@@ -0,0 +1,55 @@
fun unreachable() {}
fun a() {
do {
} while (true)
unreachable()
}
fun b() {
while (true) {
}
unreachable()
}
fun c() {
do {} while (1 == 1)
}
fun d() {
while (2 == 2) {}
}
fun use(arg: Any) = arg
fun f(cond: Boolean) {
val bar: Any
do {
if (cond) {
bar = "value"
break
}
} while (true)
use(bar) // should work
val foo: Any
while (true) {
if (cond) {
foo = "value"
break
}
}
use(foo) // should work
}
fun g(): Int {
do {
if (true) return 12
} while (true)
} // should work
fun h(): Int {
while (true) {
if (true) return 12
}
} // should work
@@ -0,0 +1,77 @@
// !DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE
fun ignoreIt(f: () -> Unit) {}
fun exec(f: () -> Unit) = f()
fun foo() {
var x: Int
ignoreIt() {
// Ok
x = 42
}
// Error!
x.hashCode()
}
fun bar() {
val x: Int
exec {
x = 13
}
}
fun bar2() {
val x: Int
fun foo() {
x = 3
}
foo()
}
class My(val cond: Boolean) {
val y: Int
init {
val x: Int
if (cond) {
exec {
}
x = 1
}
else {
x = 2
}
y = x
}
constructor(): this(false) {
val x: Int
x = 2
exec {
x.hashCode()
}
}
}
class Your {
val y = if (true) {
val xx: Int
exec {
xx = 42
}
24
}
else 0
}
val z = if (true) {
val xx: Int
exec {
<!UNRESOLVED_REFERENCE!>xx<!> = 24
}
42
}
else 0
@@ -0,0 +1,90 @@
// !DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE
fun foo() {
var x: String
class A {
init {
x = ""
}
}
// Error! See KT-10042
x.length
}
fun bar() {
var x: String
object: Any() {
init {
x = ""
}
}
// Ok
x.length
}
fun gav() {
val x: String
class B {
init {
// Error! See KT-10445
x = ""
}
}
// Error! See KT-10042
x.length
val y: String
class C(val s: String) {
constructor(): this("") {
// Error!
y = s
}
}
y.length
}
open class Gau(val s: String)
fun gau() {
val x: String
object: Any() {
init {
// Ok
x = ""
}
}
// Ok
x.length
val y: String
fun local() {
object: Any() {
init {
// Error!
y = ""
}
}
}
val z: String
object: Gau(if (true) {
z = ""
z
}
else "") {}
}
class My {
init {
val x: String
class Your {
init {
// Error! See KT-10445
x = ""
}
}
}
}
val top: Int
fun init() {
top = 1
}
@@ -0,0 +1,8 @@
fun foo() {
var x: Int
fun bar() {
x = 42
}
x.hashCode()
bar()
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +ReadDeserializedContracts +UseCallsInPlaceEffect
// See KT-17479
class Test {
val str: String
init {
run {
this@Test.str = "A"
}
run {
// Not sure do we need diagnostic also here
this@Test.str = "B"
}
str = "C"
}
}
@@ -0,0 +1,18 @@
// !LANGUAGE: -ReadDeserializedContracts -UseCallsInPlaceEffect
// See KT-17479
class Test {
val str: String
init {
run {
this@Test.str = "A"
}
run {
// Not sure do we need diagnostic also here
this@Test.str = "B"
}
str = "C"
}
}
@@ -0,0 +1,34 @@
//KT-1001 Argument 2 for @NotNull parameter of JetFlowInformationProvider.checkIsInitialized must not be null
package kt1001
fun foo(c: Array<Int>) {
return
for (i in c) {}
for (i in c) {}
}
//more tests
fun t1() : Int {
try {
return 1
}
catch (e : Exception) {
return 2
}
return 3
}
fun t2() : Int {
try {
return 1
}
finally {
doSmth()
}
return 2
}
fun doSmth() {}
@@ -0,0 +1,48 @@
//KT-1027 Strange selection of unreachable code
package kt1027
fun foo(c: List<Int>) {
var i = 2
return
for (j in c) { //strange selection of unreachable code
i += 23
}
}
fun t1() {
return
while(true) {
doSmth()
}
}
fun t2() {
return
do {
doSmth()
} while (true)
}
fun t3() {
return
try {
doSmth()
}
finally {
doSmth()
}
}
fun t4() {
return
(43)
}
fun doSmth() {}
@@ -0,0 +1,34 @@
//KT-1066 false 'Variable cannot be initialized before declaration'
package kt1066
fun randomDigit() = 0.toChar()
fun foo(excluded: Set<Char>) {
var digit : Char
do {
digit = randomDigit()
// ^^^^^ here!
} while (excluded.contains(digit))
}
fun test() {
var sum : Int = 0
var first : Int = 1
var second : Int = 2
var temp : Int //= 0 // variable 'temp' initializer is redundant
while (true)
{
if (second > 4000000)
break
if (second % 2 == 0)
sum += second
temp = second
second = first + second
first = temp
}
}
@@ -0,0 +1,13 @@
// !WITH_NEW_INFERENCE
// AssertionError for nested ifs with lambdas and Nothing as results
// NI_EXPECTED_FILE
val fn = if (true) {
{ true }
}
else if (true) {
{ true }
}
else {
null!!
}
@@ -0,0 +1,33 @@
fun find2(): Any? {
fun visit(element: Any) {
return@find2 element
}
return null
}
// For find(): AssertionError at ControlFlowInstructionsGeneratorWorker.getExitPoint()
fun find(): Any? {
object : Any() {
fun visit(element: Any) {
return@find element
}
}
return null
}
fun find4(): Any? {
inline fun visit(element: Any) {
return@find4 element
}
return null
}
fun find3(): Any? {
object : Any() {
inline fun visit(element: Any) {
return@find3 element
}
}
return null
}
@@ -0,0 +1,7 @@
//KT-1156 Throwing exception on the right side of elvis operator marks code unreachable
fun foo(maybe: Int?) {
val i : Int = maybe ?: throw RuntimeException("No value")
System.out.println(i)
}
@@ -0,0 +1,47 @@
//KT-1185 Support full enumeration check for 'when'
package kt1185
enum class Direction {
NORTH,
SOUTH,
WEST,
EAST
}
class A {
companion object {
}
}
enum class Color(val rgb : Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
fun foo(d: Direction) = when(d) { //no 'else' should be requested
Direction.NORTH -> 1
Direction.SOUTH -> 2
A -> 1
Direction.WEST -> 3
Direction.EAST -> 4
}
fun foo1(d: Direction) = when(d) {
Direction.NORTH -> 1
Direction.SOUTH -> 2
Direction.WEST -> 3
}
fun bar(c: Color) = when (c) {
Color.RED -> 1
Color.GREEN -> 2
Color.BLUE -> 3
}
fun bar1(c: Color) = when (c) {
Color.RED -> 1
Color.GREEN -> 2
}
@@ -0,0 +1,38 @@
//KT-1189 StackOverflow in ide
package kt1189
import java.util.concurrent.locks.ReentrantReadWriteLock
inline fun <T> ReentrantReadWriteLock.write(action: ()->T) : T {
val rl = readLock()
var readCount = 0
val writeCount = getWriteHoldCount()
if(writeCount == 0) {
readCount = getReadHoldCount()
if(readCount > 0)
for(i in 1..readCount)
rl.unlock()
}
val wl = writeLock()
wl.lock()
try {
return action()
}
finally {
if(readCount > 0) {
for(j in 1..readCount) {
rl.lock()
}
}
wl.unlock()
}
}
fun foo() {
try {
return
}
finally {
for (i in 1..10) {}
}
}
@@ -0,0 +1,21 @@
//KT-1191 Wrong detection of unused parameters
package kt1191
interface FunctionalList<T> {
val size: Int
val head: T
val tail: FunctionalList<T>
}
fun <T> FunctionalList<T>.plus(element: T) : FunctionalList<T> = object: FunctionalList<T> {
override val size: Int
get() = 1 + this@plus.size
override val tail: FunctionalList<T>
get() = this@plus
override val head: T
get() = element
}
fun foo(unused: Int) = object {
val a : Int get() = unused
}
@@ -0,0 +1,25 @@
//KT-1219 Incorrect 'unused value' error in closures
package kt1219
fun <T, R> Iterable<T>.fold(a: R, op: (T, R) -> R) : R {
var r = a
this.foreach { r = op(it, r) } //unused value here
return r
}
//KT-1301 Modification of local of outer function in a local function should not be marked as unused assignment
fun foo(){
var local = 0
fun bar(){
local = 1
}
bar()
System.out.println(local)
}
fun <T> Iterable<T>.foreach(operation: (element: T) -> Unit) {
for (elem in this)
operation(elem)
}
@@ -0,0 +1,42 @@
//KT-1571 Frontend fails to check val reassigment for operator overloading.
package kt1571
var c0 = 0
var c1 = 0
var c2 = 0
class A() {
var p = 0
operator fun divAssign(a : Int) {
c1++;
}
operator fun times(a : Int) : A {
c2++;
return this;
}
}
val a : A = A()
get() {
c0++
return field
}
fun box() : String {
a /= 3
if (c0 != 1) {
return "1"
}
if (c1 != 1) {
return "2"
}
<!VARIABLE_EXPECTED!>a<!> *= 3 // a = a * 3, shouldn't be able to do this on val
if (c0 != 2) {
return "3"
}
if (c2 != 1) {
return "4"
}
return "OK"
}
@@ -0,0 +1,51 @@
package kt1977
//KT-1977 Wrong 'unused expression' in catch
fun strToInt(s : String) : Int? =
try {
Integer.parseInt(s)
} catch(e : NumberFormatException) {
null
}
//more tests
fun test1(s : String) : Int? {
return try {
88
Integer.parseInt(s)
22
}
catch (e: NumberFormatException) {
44
}
finally {
22
}
}
fun test2(s : String) : Int? {
return try {
88
Integer.parseInt(s)
22
} finally {
{
x : Int -> x
}
}
}
//KT-2015 False "Expression is unused" warnings
fun foo() {
val i : Int = try{
bar()
1
}
catch(e : Exception){
0
}
}
fun bar() {
}
@@ -0,0 +1,17 @@
package a
//KT-2166 Control flow analysis doesn't detect that a 'while(true)' loop never terminates
fun foo(): Int {
while (true) {
}
}
//KT-2103 Compiler requires return statement after loop which never exits
fun foo1() : Boolean{
while(true){
if (bar()) continue
return true
}
}
fun bar() : Boolean = true
@@ -0,0 +1,15 @@
//KT-2226 Parameter used as delegation by object marked as unused
package a
interface A {
fun foo() : Int
}
class B : A {
override fun foo() = 10
}
fun foo(b: B) : Int {
val o = object : A by b {
}
return o.foo()
}
@@ -0,0 +1,53 @@
//KT-2330 Check visibility of getters and setters correspondingly
package a
class P {
var x : Int = 0
private set
var y : Int = 0
val other = P();
init {
x = 23
other.x = 4
}
val testInGetter : Int
get() {
x = 33
return 3
}
}
fun foo() {
val p = P()
p.x = 34 //should be an error here
p.y = 23
fun inner() {
p.x = 44
}
}
class R {
val p = P();
init {
p.x = 42
}
val testInGetterInOtherClass : Int
get() {
p.x = 33
return 3
}
}
fun test() {
val o = object {
fun run() {
<!UNRESOLVED_REFERENCE!>p<!>.<!UNRESOLVED_REFERENCE!>x<!> = 43
}
}
}
@@ -0,0 +1,5 @@
//KT-2334 An error 'local function without body' is not reported
fun foo() {
fun bar()
}
@@ -0,0 +1,13 @@
//KT-2369 Variable is not marked as uninitialized in 'finally' section
fun main() {
var x : Int
try {
throw Exception()
}
finally {
doSmth(x + 1)
}
}
fun doSmth(a: Any?) = a
@@ -0,0 +1,18 @@
//KT-2845 Wrong cf-analysys for variable initialization in try..finally
package h
import java.util.ArrayList
private fun doTest() : Int {
var list : MutableList<Int>? ;
try {
list = ArrayList()
// Not-null was just assigned to the list
list.add(3)
return 0 ;
}
finally {
if(list != null) { // Must be an ERROR
}
}
}
@@ -0,0 +1,36 @@
//KT-2960 Perform control flow checks for package property initializers
package b
class P {
var x : Int = 0
private set
}
val p = P()
var f = { -> p.x = 32 }
val o = object {
fun run() {
p.x = 4
val z : Int
doSmth(z)
}
}
val g = { ->
val x: Int
doSmth(x)
}
class A {
val a : Int = 1
get() {
val x : Int
doSmth(x)
return field
}
}
fun doSmth(i: Int) = i
@@ -0,0 +1,28 @@
//KT-2972 Wrong "unused value" warning when finally is present
import java.io.Closeable
public inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
closed = true // warning here
try {
this.close()
} catch (closeException: Exception) {
// eat the closeException as we are already throwing the original cause
// and we don't want to mask the real exception
// TODO on Java 7 we should call
// e.addSuppressed(closeException)
// to work like try-with-resources
// http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions
}
throw e
} finally {
if (!closed) {
this.close()
}
}
}
@@ -0,0 +1,13 @@
fun box() {
fun local():Int {
}
}
interface X {
fun f(): Boolean
}
val m = object : X {
override fun f(): Boolean {
}
}
@@ -0,0 +1,9 @@
//KT-3501 Variable/parameter is highlighted as unused if it is used in member of local class
fun f(p: String) { // "p" is marked as unused
class LocalClass {
fun f() {
p
}
}
}
@@ -0,0 +1,3 @@
public data class ProductGroup(val short_name: String, val parent: ProductGroup?) {
val name: String = if (parent == null) short_name else "${parent.name} $short_name"
}
@@ -0,0 +1,30 @@
//KT-4405 Control-flow analysis is not performed for some local declarations
package d
val closure = {
val x4 = "" // error: should be UNUSED_VARIABLE
fun g() {
val x6 = "" // error: should be UNUSED_VARIABLE
}
fun h(): Int { // error: should be NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY
}
}
class A {
init {
fun foo(): Int {
}
val closure = {
val x = ""
fun local(): Int {
}
}
val y = ""
}
}
@@ -0,0 +1,18 @@
//KT-510 `this.` allows initialization without backing field
package kt510
public open class Identifier1() {
var field : Boolean
init {
field = false; // error
}
}
public open class Identifier2() {
var field : Boolean
init {
this.field = false;
}
}
@@ -0,0 +1,18 @@
//KT-607 Val reassignment is not marked as an error
package kt607
fun foo(a: A) {
val o = object {
val y : Int
get() = 42
}
a.z = 23
o.y = 11 //Should be an error here
}
class A() {
val z : Int
get() = 3
}
@@ -0,0 +1,20 @@
//KT-609 Analyze not only local variables, but function parameters as well in 'unused values' analysis
package kt609
fun test(a: Int) {
var aa = a
aa = 324 //should be an 'unused value' warning here
}
class C() {
fun foo(s: String) {} //should be an 'unused variable' warning
}
open class A() {
open fun foo(s : String) {} //should not be a warning
}
class B() : A() {
final override fun foo(s : String) {} //should not be a warning
}
@@ -0,0 +1,10 @@
//KT-610 Distinguish errors 'unused variable' and 'variable is assigned but never accessed'
package kt610
fun foo() {
var j = 9 //'unused variable' error
var i = 1 //should be an error 'variable i is assigned but never accessed'
i = 2
}
@@ -0,0 +1,10 @@
class A(val next: A? = null) {
val x: String
init {
next?.x = "a"
}
}
class B(val next: B? = null) {
var x: String = next?.x ?: "default" // it's ok to use `x` of next
}
@@ -0,0 +1,38 @@
//KT-776 Wrong detection of unreachable code
package kt776
fun test5() : Int {
var x = 0
while(true) {
try {
if(x < 10) {
x++
continue
}
else {
break
}
}
finally {
x++
}
}
return x
}
fun test1() : Int {
try {
if (true) {
return 1
}
else {
return 2
}
}
finally {
doSmth() //unreachable
}
}
fun doSmth() {}
@@ -0,0 +1,8 @@
//KT-843 Don't highlight incomplete variables as unused
package kt843
fun main() {
// Integer type
val<!SYNTAX!><!> // this word is grey, which looks strange
}
@@ -0,0 +1,20 @@
//KT-897 Don't allow assignment to a property before it is defined
package kt897
class A() {
init {
i = 11
}
val i : Int? = null // must be an error
init {
j = 1
}
var j : Int = 2
init {
k = 3
}
val k : Int
}
@@ -0,0 +1,26 @@
package f
fun f() {
class LocalClass() {
init {
val x1 = "" // ok: unused
fun loc1(): Int {
val x1_ = "" // ok: unused
}
}
fun f() {
val x2 = "" // error: should be UNUSED_VARIABLE
fun loc2(): Int {
val x2_ = "" // error: should be UNUSED_VARIABLE
}
}
val v: String
get() {
val x3 = "" // ok: unused
}
}
}
@@ -0,0 +1,87 @@
fun println(obj: Any?) = obj
class Demo0 {
private val some = object {
fun foo() {
println(state)
}
}
private var state: Boolean = true
}
class Demo1 {
private val some = object {
fun foo() {
if (state)
state = true
println(state)
}
}
private var state: Boolean = true
}
class Demo1A {
fun foo() {
if (state)
state = true
println(state)
}
private var state: Boolean = true
}
class Demo2 {
private val some = object {
fun foo() {
if (state)
state = true
else
state = false
println(state)
}
}
private var state: Boolean = true
}
class Demo3 {
private val some = run {
if (state)
state = true
println(state)
}
private var state: Boolean = true
}
fun <T> exec(f: () -> T): T = f()
class Demo4 {
private val some = exec {
if (state)
state = true
println(state)
}
private var state: Boolean = true
}
class Demo5 {
private var state: Boolean = true
private val some = object {
fun foo() {
if (state)
state = true
println(state)
}
}
}
@@ -0,0 +1,5 @@
// !LANGUAGE: +WarningOnMainUnusedParameter
fun main(args: Array<String>) {
}
@@ -0,0 +1,5 @@
// !LANGUAGE: -WarningOnMainUnusedParameter
fun main(args: Array<String>) {
}
@@ -0,0 +1,24 @@
fun use(arg: String?) = arg
fun sample(): String? {
try {
if (false) {
return "fail"
} else {
if (false) {
if (false) {
var foo: String? = null
try {
foo = "test"
} catch (e: Exception) {
return "fail"
} finally {
use(foo) // 'foo' is initialized here
}
}
return "fail"
}
}
} finally {}
return null
}
@@ -0,0 +1,26 @@
// !WITH_NEW_INFERENCE
// See also KT-5198 / KT-10186
inline fun doCall(f: () -> Unit) = f()
fun test1(nonLocal: String): String {
val localResult = doCall {
return nonLocal //unreachable
}
return "NON_LOCAL_FAILED $localResult" //unreachable
}
fun doSomething() {}
fun test2() {
fun f(x: Any?) = x
f(null?.let { return })
// false unreachable here
doSomething()
}
fun test3(x: Any?): Boolean =
x?.let {
return true
} ?: false
@@ -0,0 +1,10 @@
fun test(name: String?) {
try {
name?.let {
return
}
}
finally {
name?.hashCode()
}
}
@@ -0,0 +1,17 @@
open class X(s : String) {
public var n: String = s
private set
}
class Z : X("subclass") {
fun print(): String {
n = n
return n;
}
}
fun box() : String {
return Z().print() //error
}
@@ -0,0 +1,12 @@
class A(val next: A? = null) {
val x: String
init {
next?.x = "a"
x = "b"
this.x = "c"
x = "d" // don't repeat the same diagnostic again with this receiver
this.x = "e"
next?.x = "f"
}
}
@@ -0,0 +1,21 @@
class Outer {
val outerProp: String
inner class Inner(inner: Inner, outer: Outer) {
val innerProp: String
init {
outerProp // use of outerProp is ok because we're suppose that Outer instance should be initialized
this@Outer.outerProp
this@Outer.outerProp = "1"
outerProp = "2" // do not repeat the same diagnostic with this receiver of outer class
outer.outerProp = "3"
innerProp = "4" + inner.innerProp
this@Inner.innerProp = "5"
innerProp = "6" // do not repeat the same diagnostic with this receiver
this@Inner.innerProp = "7"
inner.innerProp = "8"
}
}
}
@@ -0,0 +1,12 @@
class A(val next: A? = null) {
val x: String
init {
next?.x = "a"
this@A.x = "b"
this.x = "c"
x = "d" // don't repeat the same diagnostic again with this receiver
this@A.x = "e"
next?.x = "f"
}
}
@@ -0,0 +1,4 @@
package a
val a : Int = b
val b : Int = a
@@ -0,0 +1,102 @@
// KT-13612 related tests (reassignment in try-catch-finally)
fun f1() {
val n: Int
try {
n = 1
throw Exception()
}
catch (e: Exception) {
// KT-13612: reassignment
n = 2
}
n.hashCode()
}
fun f2() {
val n: Int
try {
n = 1
throw Exception()
}
finally {
n = 2
}
n.hashCode()
}
fun g1(flag: Boolean) {
val n: Int
try {
if (flag) throw Exception()
n = 1
}
catch (e: Exception) {
// KT-13612: ? reassignment or definite assignment ?
n = 2
}
n.hashCode()
}
fun g2(flag: Boolean) {
val n: Int
try {
if (flag) throw Exception()
n = 1
}
finally {
n = 2
}
n.hashCode()
}
fun h1(flag: Boolean) {
val n = try {
if (flag) throw Exception()
1
}
catch (e: Exception) {
2
}
n.hashCode()
}
fun h2(flag: Boolean) {
val n = try {
if (flag) throw Exception()
1
}
finally {
2
}
n.hashCode()
}
fun j(flag: Boolean) {
if (flag) throw Exception()
}
fun k1(flag: Boolean) {
val n: Int
try {
n = 1
j(flag)
}
catch (e: Exception) {
// KT-13612: reassignment
n = 2
}
n.hashCode()
}
fun k2(flag: Boolean) {
val n: Int
try {
n = 1
j(flag)
}
finally {
n = 2
}
n.hashCode()
}
@@ -0,0 +1,55 @@
fun exc(flag: Boolean) {
if (flag) throw Exception()
}
fun f1(flag: Boolean) {
val n: Int
try {
if (flag) {
n = 1
exc(flag)
return
}
}
catch (e: Exception) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
}
fun f2(flag: Boolean) {
while (true) {
val n: Int
try {
if (flag) {
n = 1
exc(flag)
break
}
}
catch (e: Exception) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
}
}
fun f3(flag: Boolean) {
while (true) {
val n: Int
try {
if (flag) {
n = 1
exc(flag)
continue
}
}
catch (e: Exception) {
// KT-13612: reassignment
n = 3
}
n.hashCode()
}
}

Some files were not shown because too many files have changed in this diff Show More