Fix type inference issues for 'if' and 'when'.
Use 'expectedType' (when present) as an explicit type argument for a special construct call. Unfortunately, this approach can't be used for elvis due to other elvis-related inference hacks. Fixes KT-10807, KT-10811. This also affects KT-6189: now we can infer proper type for 'if'. If type inference for special call failed, and we found no type errors in sub-expressions, report TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT error. This (and the hack above) fixes KT-10809: code no longer compiles.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -DEBUG_INFO_SMARTCAST
|
||||
|
||||
interface Data
|
||||
interface Item
|
||||
class FlagData(val value: Boolean) : Data
|
||||
class ListData<T : Item>(val list: List<T>) : Data
|
||||
|
||||
fun <T> listOf(vararg items: T): List<T> = null!!
|
||||
|
||||
fun test1(o: Any) = <!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>when<!> (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test1x(o: Any): Data? = when (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test2() =
|
||||
<!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>if<!> (true)
|
||||
ListData(listOf())
|
||||
else
|
||||
FlagData(true)
|
||||
|
||||
fun test2x(): Data =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2y(): Any =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2z(): Any =
|
||||
run { if (true) ListData(listOf()) else FlagData(true) }
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> listOf(/*0*/ vararg items: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
|
||||
public fun test1(/*0*/ o: kotlin.Any): ???
|
||||
public fun test1x(/*0*/ o: kotlin.Any): Data?
|
||||
public fun test2(): ???
|
||||
public fun test2x(): Data
|
||||
public fun test2y(): kotlin.Any
|
||||
public fun test2z(): kotlin.Any
|
||||
|
||||
public interface Data {
|
||||
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
|
||||
}
|
||||
|
||||
public final class FlagData : Data {
|
||||
public constructor FlagData(/*0*/ value: kotlin.Boolean)
|
||||
public final val value: kotlin.Boolean
|
||||
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
|
||||
}
|
||||
|
||||
public interface Item {
|
||||
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
|
||||
}
|
||||
|
||||
public final class ListData</*0*/ T : Item> : Data {
|
||||
public constructor ListData</*0*/ T : Item>(/*0*/ list: kotlin.collections.List<T>)
|
||||
public final val list: kotlin.collections.List<T>
|
||||
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,24 @@
|
||||
interface Maybe<T>
|
||||
class Some<T>(val value: T) : Maybe<T>
|
||||
class None<T> : Maybe<T>
|
||||
|
||||
fun <T> none() : None<T> = TODO()
|
||||
|
||||
fun test1() : Maybe<String?> = if (true) none() else Some("")
|
||||
|
||||
fun test2() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some("")
|
||||
}
|
||||
|
||||
fun test3() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some<String?>("")
|
||||
}
|
||||
|
||||
fun test4() : Maybe<String?> {
|
||||
when ("") {
|
||||
"a" -> return none()
|
||||
else -> return Some<String?>("")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> none(): None<T>
|
||||
public fun test1(): Maybe<kotlin.String?>
|
||||
public fun test2(): Maybe<kotlin.String?>
|
||||
public fun test3(): Maybe<kotlin.String?>
|
||||
public fun test4(): Maybe<kotlin.String?>
|
||||
|
||||
public interface Maybe</*0*/ T> {
|
||||
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
|
||||
}
|
||||
|
||||
public final class None</*0*/ T> : Maybe<T> {
|
||||
public constructor None</*0*/ T>()
|
||||
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
|
||||
}
|
||||
|
||||
public final class Some</*0*/ T> : Maybe<T> {
|
||||
public constructor Some</*0*/ T>(/*0*/ value: T)
|
||||
public final val value: T
|
||||
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,24 @@
|
||||
val test1: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> {{ false }}
|
||||
}
|
||||
|
||||
val test2: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test3: (String) -> Boolean =
|
||||
when {
|
||||
true -> { s -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test4: (String) -> Boolean =
|
||||
when {
|
||||
true -> { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>s1, <!CANNOT_INFER_PARAMETER_TYPE!>s2<!><!> -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package
|
||||
|
||||
public val test1: (kotlin.String) -> kotlin.Boolean
|
||||
public val test2: (kotlin.String) -> kotlin.Boolean
|
||||
public val test3: (kotlin.String) -> kotlin.Boolean
|
||||
public val test4: (kotlin.String) -> kotlin.Boolean
|
||||
Reference in New Issue
Block a user