Preliminary declaration visitor for estimating local variable's predictability for smart casts

Predictability estimation algorithm is completely new, but backward compatibility should present.
A large set of tests. Some updated tests.
Smart casts allowed for captured variables if they are not modified in closure #KT-9051 Fixed
Also #KT-8643 Fixed
Also #KT-7976 Fixed
Correct handling of lambda arguments in functions #KT-9143 Fixed
This commit is contained in:
Mikhail Glukhikh
2015-09-28 17:18:19 +03:00
parent de7d2978db
commit 4b35e3b135
84 changed files with 1047 additions and 43 deletions
@@ -0,0 +1,11 @@
// KT-7186: False "Type mismatch" error
fun indexOfMax(a: IntArray): Int? {
var maxI: Int? = null
a.forEachIndexed { i, value ->
if (maxI == null || value >= a[<!TYPE_MISMATCH!>maxI<!>]) {
maxI = i
}
}
return maxI
}
@@ -0,0 +1,3 @@
package
public fun indexOfMax(/*0*/ a: kotlin.IntArray): kotlin.Int?
@@ -0,0 +1,14 @@
// KT-7186: False "Type mismatch" error
fun indexOfMax(a: IntArray): Int? {
var maxI: Int? = 0
a.forEachIndexed { i, value ->
if (value >= a[<!TYPE_MISMATCH!>maxI<!>]) {
maxI = i
}
else if (value < 0) {
maxI = null
}
}
return maxI
}
@@ -0,0 +1,3 @@
package
public fun indexOfMax(/*0*/ a: kotlin.IntArray): kotlin.Int?
@@ -0,0 +1,11 @@
class My(val x: Int?) {
val y: Int? by lazy {
var z = x
while (z != null) {
z = <!DEBUG_INFO_SMARTCAST!>z<!>.hashCode()
if (<!DEBUG_INFO_SMARTCAST!>z<!> < 0) return@lazy z
if (z == 0) z = null
}
return@lazy null
}
}
@@ -0,0 +1,10 @@
package
public final class My {
public constructor My(/*0*/ x: kotlin.Int?)
public final val x: kotlin.Int?
public final val y: kotlin.Int?
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
}
@@ -0,0 +1,7 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun foo(y: String) {
var x: String? = null
y.let { x = it }
x<!UNSAFE_CALL!>.<!>length // Smart cast is not possible
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String): kotlin.Unit
@@ -0,0 +1,7 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun foo(y: String?) {
var x: String? = null
y?.let { x = it }
x<!UNSAFE_CALL!>.<!>length // Smart cast is not possible
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,9 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun foo(y: String?) {
var x: String? = ""
if (x != null) {
y?.let { x = null }
x<!UNSAFE_CALL!>.<!>length // Smart cast is not possible
}
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,11 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun bar(z: String?) = z
fun foo(y: String?) {
var x: String? = ""
if (x != null) {
bar(y?.let { x = null; it })<!UNSAFE_CALL!>.<!>length
x<!UNSAFE_CALL!>.<!>length // Smart cast is not possible
}
}
@@ -0,0 +1,4 @@
package
public fun bar(/*0*/ z: kotlin.String?): kotlin.String?
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,9 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun foo(y: String?) {
var x: String? = null
if (x != null) {
y?.let { x = it }
x<!UNSAFE_CALL!>.<!>length // not-null or not-null
}
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,10 @@
// KT-9051: Allow smart cast for captured variables if they are not modified
fun foo(y: String?) {
var x: String? = ""
if (x != null) {
y?.let { x != y }
// x is not changed, smart cast is possible
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,10 @@
fun foo(y: String?) {
var x: String? = ""
if (x != null) {
with(y?.let { x = null; it }) {
this<!UNSAFE_CALL!>.<!>length
x<!UNSAFE_CALL!>.<!>length
}
x<!UNSAFE_CALL!>.<!>length
}
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ y: kotlin.String?): kotlin.Unit