Files
kotlin-fork/compiler/testData/diagnostics/tests/smartCasts/elvisExprNotNull.kt
T
Dmitry Petrov f371e67ce8 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.
2016-01-22 10:41:55 +03:00

36 lines
784 B
Kotlin
Vendored

fun foo(s: Any?): String {
val t = when {
// To resolve: String U Nothing? = String?
s is String -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> null
} ?: ""
return t
}
fun bar(s: Any?): String {
// To resolve: String U Nothing? = String?
val t = (if (s == null) {
null
}
else {
val u: Any? = null
if (u !is String) return ""
u
}) ?: "xyz"
// Ideally we should have smart cast to String here
return <!TYPE_MISMATCH!>t<!>
}
fun baz(s: String?, r: String?): String {
val t = r ?: when {
s != null -> <!DEBUG_INFO_SMARTCAST!>s<!>
else -> ""
}
return t
}
fun withNull(s: String?): String {
val t = s ?: null
// Error: nullable
return <!TYPE_MISMATCH!>t<!>
}