Front-end fix: result type corrected for increment / decrement expressions. Smart cast allowed on a postfix increment result. See KT-7561.

Now result type is receiver type for postfix increment, or increment result type for prefix increment.
A set of relevant tests.
This commit is contained in:
Mikhail Glukhikh
2015-04-30 15:08:15 +03:00
parent ea6d0b464d
commit 5723c2a077
17 changed files with 212 additions and 3 deletions
@@ -0,0 +1,15 @@
// Test for a potential byte code mistake for a postfix operation on a smart casted variable
public fun box() : Int {
var i : Int?
i = 10
val ii: Int = <!DEBUG_INFO_SMARTCAST!>i<!>
// k also should be Int
val k : Int = <!DEBUG_INFO_SMARTCAST!><!DEBUG_INFO_SMARTCAST!>i<!>++<!>
// KT-7561: both i and i++ should be Int, otherwise VerifyError can arise here
// VerifyError reason: byte code tries to store (i++) result which is Int (smart cast)
// into a j which is Int?
val j = <!DEBUG_INFO_SMARTCAST!>i<!>++
// and m also
val m = ++<!DEBUG_INFO_SMARTCAST!>i<!>
return <!DEBUG_INFO_SMARTCAST!>j<!> + k + m + <!DEBUG_INFO_SMARTCAST!>i<!> + ii
}
@@ -0,0 +1,3 @@
package
public fun box(): kotlin.Int
@@ -0,0 +1,13 @@
class MyClass
// In principle it is not correct, MyClass? is not a subtype of MyClass
fun MyClass.inc(): MyClass? { return null }
public fun box() : MyClass? {
var i : MyClass?
i = MyClass()
// type of j can be inferred as MyClass()
var j = <!DEBUG_INFO_SMARTCAST!>i<!>++
<!DEBUG_INFO_SMARTCAST!>j<!>.hashCode()
return i
}
@@ -0,0 +1,11 @@
package
public fun box(): MyClass?
internal fun MyClass.inc(): MyClass?
internal final class MyClass {
public constructor MyClass()
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,12 @@
class MyClass
// Correct at compile time but wrong at run-time
fun MyClass?.inc(): MyClass? { return null }
public fun box() : MyClass? {
var i : MyClass?
i = MyClass()
var j = i++
<!DEBUG_INFO_SMARTCAST!>j<!>.hashCode()
return i
}
@@ -0,0 +1,11 @@
package
public fun box(): MyClass?
internal fun MyClass?.inc(): MyClass?
internal final class MyClass {
public constructor MyClass()
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,8 @@
fun Int?.inc(): Int? { return this }
public fun box(arg: Int?) : Int? {
var i : Int? = arg
var j = i++
j<!UNSAFE_CALL!>.<!>toInt()
return i
}
@@ -0,0 +1,4 @@
package
public fun box(/*0*/ arg: kotlin.Int?): kotlin.Int?
internal fun kotlin.Int?.inc(): kotlin.Int?
@@ -0,0 +1,13 @@
class MyClass
// In principle it is not correct, MyClass? is not a subtype of MyClass
fun MyClass.inc(): MyClass? { return null }
public fun box() {
var i : MyClass?
i = MyClass()
// Type of j should be inferred as MyClass?
var j = ++<!DEBUG_INFO_SMARTCAST!>i<!>
// j is null so call is unsafe
j<!UNSAFE_CALL!>.<!>hashCode()
}
@@ -0,0 +1,11 @@
package
public fun box(): kotlin.Unit
internal fun MyClass.inc(): MyClass?
internal final class MyClass {
public constructor MyClass()
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,13 @@
class MyClass
// Correct at compile time but wrong at run-time
fun MyClass?.inc(): MyClass? { return null }
public fun box() {
var i : MyClass?
i = MyClass()
// type of j should be MyClass?
var j = ++i
// j is null so call should be unsafe
j<!UNSAFE_CALL!>.<!>hashCode()
}
@@ -0,0 +1,11 @@
package
public fun box(): kotlin.Unit
internal fun MyClass?.inc(): MyClass?
internal final class MyClass {
public constructor MyClass()
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,8 @@
fun Int?.inc(): Int? { return this }
public fun box(arg: Int?) : Int? {
var i = arg
var j = ++i
j<!UNSAFE_CALL!>.<!>toInt()
return ++j
}
@@ -0,0 +1,4 @@
package
public fun box(/*0*/ arg: kotlin.Int?): kotlin.Int?
internal fun kotlin.Int?.inc(): kotlin.Int?