PatternMatchingTypingVisitor:

rewrite type inference for 'when' using special constructs.
This fixes several type inference issues for 'when':
KT-9929, KT-9972, KT-10439, KT-10463
along with some other diagnostics-related issues.
This commit is contained in:
Dmitry Petrov
2016-01-20 15:28:58 +03:00
parent b49e08fe04
commit f371e67ce8
23 changed files with 280 additions and 152 deletions
@@ -22,7 +22,7 @@ val wxx1 = <!NO_ELSE_IN_WHEN!>when<!> { true -> 42 }
val wxx2: Unit = <!NO_ELSE_IN_WHEN!>when<!> { true -> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>42<!> }
val wxx3 = idAny(<!NO_ELSE_IN_WHEN!>when<!> { true -> 42 })
val wxx4 = id(<!NO_ELSE_IN_WHEN!>when<!> { true -> 42 })
val wxx5 = idUnit(<!NO_ELSE_IN_WHEN!>when<!> { true -> 42 })
val wxx5 = idUnit(<!NO_ELSE_IN_WHEN!>when<!> { true -> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>42<!> })
val wxx6 = null ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> 42 }
val wxx7 = "" + <!NO_ELSE_IN_WHEN!>when<!> { true -> 42 }
@@ -4,8 +4,8 @@ fun fn(c: Char?): Any? =
if (c == null) TODO()
else when (<!DEBUG_INFO_SMARTCAST!>c<!>) {
'a' -> when (<!DEBUG_INFO_SMARTCAST!>c<!>) {
'B' -> <!IMPLICIT_CAST_TO_ANY!>1<!>
'C' -> <!IMPLICIT_CAST_TO_ANY!>"sdf"<!>
'B' -> 1
'C' -> "sdf"
else -> TODO()
}
else -> TODO()
@@ -1,11 +1,10 @@
fun foo(s: Any?): String {
val t = when {
// To resolve: String U Nothing? = String?
s is String -> s
s is String -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> null
} ?: ""
// Ideally we should have smart cast to String here
return <!TYPE_MISMATCH!>t<!>
return t
}
fun bar(s: Any?): String {
@@ -24,10 +23,10 @@ fun bar(s: Any?): String {
fun baz(s: String?, r: String?): String {
val t = r ?: when {
s != null -> s
s != null -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> ""
}
return <!DEBUG_INFO_SMARTCAST!>t<!>
return t
}
fun withNull(s: String?): String {
@@ -6,8 +6,8 @@ fun baz(s: String?): String {
val u: String? = null
when (u) {
null -> ""
else -> u
else -> <!DEBUG_INFO_SMARTCAST!>u<!>
}
}
return <!DEBUG_INFO_SMARTCAST!>t<!>
return t
}
@@ -1,15 +1,15 @@
fun foo(s: String) = s.length
fun baz(s: String?, r: String?): Int {
return foo(<!DEBUG_INFO_SMARTCAST!>r ?: when {
s != null -> s
return foo(r ?: when {
s != null -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> ""
}<!>)
})
}
fun bar(s: String?, r: String?): Int {
return <!DEBUG_INFO_SMARTCAST!>(r ?: when {
s != null -> s
return (r ?: when {
s != null -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> ""
})<!>.length
}).length
}
@@ -1,10 +1,10 @@
// Smart casts on complex expressions
fun baz(s: String?): Int {
if (s == null) return 0
return <!DEBUG_INFO_SMARTCAST!>when(<!DEBUG_INFO_SMARTCAST!>s<!>) {
"abc" -> s
return when(<!DEBUG_INFO_SMARTCAST!>s<!>) {
"abc" -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> "xyz"
}<!>.length
}.length
}
var ss: String? = null
@@ -3,16 +3,16 @@ fun baz(s: String?): String {
// if explicit type String is given for t, problem disappears
val t = when(<!DEBUG_INFO_SMARTCAST!>s<!>) {
// !! is detected as unnecessary here
"abc" -> s
"abc" -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> "xyz"
}
return <!DEBUG_INFO_SMARTCAST!>t<!>
return t
}
fun foo(s: String?): String {
val t = when {
s != null -> s
s != null -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> ""
}
return <!DEBUG_INFO_SMARTCAST!>t<!>
return t
}
@@ -1,11 +1,11 @@
fun foo(s: Any): String {
val x = when (s) {
is String -> s
is String -> <!DEBUG_INFO_SMARTCAST!>s<!>
is Int -> "$s"
else -> return ""
}
val y: String = <!DEBUG_INFO_SMARTCAST!>x<!> // should be Ok
val y: String = x // should be Ok
return y
}
+12
View File
@@ -0,0 +1,12 @@
fun foo(x: Int) = x
fun test0(flag: Boolean) {
foo(if (flag) <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!> else <!TYPE_MISMATCH!>""<!>)
}
fun test1(flag: Boolean) {
foo(when (flag) {
true -> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>
else -> <!TYPE_MISMATCH!>""<!>
})
}
+5
View File
@@ -0,0 +1,5 @@
package
public fun foo(/*0*/ x: kotlin.Int): kotlin.Int
public fun test0(/*0*/ flag: kotlin.Boolean): kotlin.Unit
public fun test1(/*0*/ flag: kotlin.Boolean): kotlin.Unit
+9
View File
@@ -0,0 +1,9 @@
val test: Int = if (true) {
when (2) {
1 -> 1
else -> <!NULL_FOR_NONNULL_TYPE!>null<!>
}
}
else {
2
}
+3
View File
@@ -0,0 +1,3 @@
package
public val test: kotlin.Int
+17
View File
@@ -0,0 +1,17 @@
fun test1(): Int {
val x: String = if (true) {
when {
true -> <!TYPE_MISMATCH!>Any()<!>
else -> <!NULL_FOR_NONNULL_TYPE!>null<!>
}
} else ""
return x.hashCode()
}
fun test2(): Int {
val x: String = when {
true -> <!TYPE_MISMATCH!>Any()<!>
else -> null
} ?: return 0
return x.hashCode()
}
+4
View File
@@ -0,0 +1,4 @@
package
public fun test1(): kotlin.Int
public fun test2(): kotlin.Int
@@ -0,0 +1,6 @@
val test: Int = listOf<Any>().map {
when (it) {
is Int -> <!DEBUG_INFO_SMARTCAST!>it<!>
else -> throw AssertionError()
}
}.sum()
@@ -0,0 +1,3 @@
package
public val test: kotlin.Int