Fix for KT-14597: "When" over smartcasted enum is broken and breaks all other "when"
#KT-14597 Fixed
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
enum class En { A, B, С }
|
||||
|
||||
fun box(): String {
|
||||
var res = ""
|
||||
// nullable variable
|
||||
val en2: Any? = En.A
|
||||
if (en2 is En) {
|
||||
when (en2) {
|
||||
En.A -> {res += "O"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
when (en2 as En) {
|
||||
En.A -> {res += "K"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
enum class En { A, B, С }
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var res1 = "fail"
|
||||
var res2 = "fail2"
|
||||
|
||||
val en: En = En.A
|
||||
when (en) {
|
||||
En.A -> {res1 = ""}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
when (en as En) {
|
||||
En.A -> {res1 += "O"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
|
||||
// nullable variable
|
||||
val en2: Any? = En.A
|
||||
if (en2 is En) {
|
||||
when (en2) {
|
||||
En.A -> {res1 += "K"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
when (en2 as En) {
|
||||
En.A -> {res2 = ""}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// not nullable variable
|
||||
val en1: Any = En.A
|
||||
if (en1 is En) {
|
||||
when (en1) {
|
||||
En.A -> {res2 += "O"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
// Working without other examples
|
||||
when (en1 as En) {
|
||||
En.A -> {res2 += "K"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
}
|
||||
|
||||
if (res1 != res2) return "different results: $res1 != $res2"
|
||||
return res1
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
class EncapsulatedEnum<T : Enum<T>>(val value: T)
|
||||
|
||||
enum class MyEnum(val value: String) {
|
||||
VALUE_A("OK"),
|
||||
VALUE_B("fail"),
|
||||
}
|
||||
|
||||
private fun crash(encapsulated: EncapsulatedEnum<*>) {
|
||||
val myEnum = encapsulated.value
|
||||
if (myEnum !is MyEnum) {
|
||||
return
|
||||
}
|
||||
|
||||
when (myEnum) {
|
||||
MyEnum.VALUE_A -> res = myEnum.value
|
||||
MyEnum.VALUE_B -> res = myEnum.value
|
||||
}
|
||||
}
|
||||
|
||||
var res = "fail"
|
||||
|
||||
fun box(): String {
|
||||
crash(EncapsulatedEnum(MyEnum.VALUE_A))
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
private fun Any?.doTheThing(): String {
|
||||
when (this) {
|
||||
is String -> return this
|
||||
is Level -> {
|
||||
when (this) {
|
||||
Level.O -> return Level.O.name
|
||||
Level.K -> return Level.K.name
|
||||
}
|
||||
}
|
||||
|
||||
else -> return "fail"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum class Level {
|
||||
O,
|
||||
K
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return "O".doTheThing() + Level.K.doTheThing()
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
enum class En { A, B, С }
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var res1 = "fail"
|
||||
var res2 = "fail2"
|
||||
|
||||
val en: En = En.A
|
||||
when (en) {
|
||||
En.A -> {res1 = ""}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
when (en as En) {
|
||||
En.A -> {res1 += "O"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
|
||||
// nullable variable
|
||||
val en2: Any? = En.A
|
||||
if (en2 is En) {
|
||||
when (en2) {
|
||||
En.A -> {res1 += "K"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
|
||||
when (en2 as En) {
|
||||
En.A -> {res2 = ""}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// not nullable variable
|
||||
val en1: Any = En.A
|
||||
if (en1 is En) {
|
||||
when (en1) {
|
||||
En.A -> {res2 += "O"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
// Working without other examples
|
||||
when (en1 as En) {
|
||||
En.A -> {res2 += "K"}
|
||||
En.B -> {}
|
||||
En.С -> {}
|
||||
}
|
||||
}
|
||||
|
||||
if (res1 != res2) return "different results: $res1 != $res2"
|
||||
return res1
|
||||
}
|
||||
|
||||
// 6 TABLESWITCH
|
||||
@@ -0,0 +1,27 @@
|
||||
class EncapsulatedEnum<T : Enum<T>>(val value: T)
|
||||
|
||||
enum class MyEnum(val value: String) {
|
||||
VALUE_A("OK"),
|
||||
VALUE_B("fail"),
|
||||
}
|
||||
|
||||
private fun crash(encapsulated: EncapsulatedEnum<*>) {
|
||||
val myEnum = encapsulated.value
|
||||
if (myEnum !is MyEnum) {
|
||||
return
|
||||
}
|
||||
|
||||
when (myEnum) {
|
||||
MyEnum.VALUE_A -> res = myEnum.value
|
||||
MyEnum.VALUE_B -> res = myEnum.value
|
||||
}
|
||||
}
|
||||
|
||||
var res = "fail"
|
||||
|
||||
fun box(): String {
|
||||
crash(EncapsulatedEnum(MyEnum.VALUE_A))
|
||||
return res
|
||||
}
|
||||
|
||||
// 1 LOOKUPSWITCH
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
private fun Any?.doTheThing(): String {
|
||||
when (this) {
|
||||
is String -> return this
|
||||
is Level -> {
|
||||
when (this) {
|
||||
Level.O -> return Level.O.name
|
||||
Level.K -> return Level.K.name
|
||||
}
|
||||
}
|
||||
|
||||
else -> return "fail"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum class Level {
|
||||
O,
|
||||
K
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return "O".doTheThing() + Level.K.doTheThing()
|
||||
}
|
||||
|
||||
// 1 LOOKUPSWITCH
|
||||
@@ -0,0 +1,14 @@
|
||||
@kotlin.Metadata
|
||||
public enum class En {
|
||||
public final static field A: En
|
||||
public final static field B: En
|
||||
public final static field С: En
|
||||
protected method <init>(p0: java.lang.String, p1: int): void
|
||||
public static method valueOf(p0: java.lang.String): En
|
||||
public static method values(): En[]
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class Kt14597Kt {
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
@kotlin.Metadata
|
||||
public enum class En {
|
||||
public final static field A: En
|
||||
public final static field B: En
|
||||
public final static field С: En
|
||||
protected method <init>(p0: java.lang.String, p1: int): void
|
||||
public static method valueOf(p0: java.lang.String): En
|
||||
public static method values(): En[]
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class Kt14597_fullKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method main(@org.jetbrains.annotations.NotNull p0: java.lang.String[]): void
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
@kotlin.Metadata
|
||||
public final class EncapsulatedEnum {
|
||||
private final @org.jetbrains.annotations.NotNull field value: java.lang.Enum
|
||||
public method <init>(@org.jetbrains.annotations.NotNull p0: java.lang.Enum): void
|
||||
public final @org.jetbrains.annotations.NotNull method getValue(): java.lang.Enum
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class Kt14802Kt {
|
||||
private static @org.jetbrains.annotations.NotNull field res: java.lang.String
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
private final static method crash(p0: EncapsulatedEnum): void
|
||||
public final static @org.jetbrains.annotations.NotNull method getRes(): java.lang.String
|
||||
public final static method setRes(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public enum class MyEnum {
|
||||
public final static field VALUE_A: MyEnum
|
||||
public final static field VALUE_B: MyEnum
|
||||
private final @org.jetbrains.annotations.NotNull field value: java.lang.String
|
||||
protected method <init>(@java.lang.Synthetic p0: java.lang.String, @java.lang.Synthetic p1: int, @org.jetbrains.annotations.NotNull p2: java.lang.String): void
|
||||
public final @org.jetbrains.annotations.NotNull method getValue(): java.lang.String
|
||||
public static method valueOf(p0: java.lang.String): MyEnum
|
||||
public static method values(): MyEnum[]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
@kotlin.Metadata
|
||||
public final class Kt15806Kt {
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
private final static method doTheThing(@org.jetbrains.annotations.Nullable p0: java.lang.Object): java.lang.String
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public enum class Level {
|
||||
public final static field K: Level
|
||||
public final static field O: Level
|
||||
protected method <init>(p0: java.lang.String, p1: int): void
|
||||
public static method valueOf(p0: java.lang.String): Level
|
||||
public static method values(): Level[]
|
||||
}
|
||||
Reference in New Issue
Block a user