Why does this produce a warning: ```open class Sup...
# getting-started
j
Why does this produce a warning:
Copy code
open class Super

class Container<T: Super>

fun test(thing: Any) {
    if (thing is Container<*>) {
        val thing2 = thing as Container<Super>
    }
}
But this does not?
Copy code
open class Super

class Container<T: Super>

fun test(thing: Any) {
    if (thing is Container<out Super>) {
        val thing2 = thing as Container<Super>
    }
}
Shouldn't
*
be equivalent to
out Super
in this case?
l
I'm more surprised that the second doesn't show a warning. You're doing
is
on an erased type.
1
Ah, it's because you are checking for exactly the toplevel type for
Container
.
j
Seems like a soundness hole to me.
s
That is weird...
Copy code
fun main() {
  test(mutableListOf<String>())
}

fun test(list: Any) {
  if (list is MutableList<out Any?>) {
    val list2 = list as MutableList<Any?>
    list2.add(null)
  }
}
Definitely feels like there's a warning missing here.
j
Does it happen on K1?
I can't test right now.
s
Oh good shout, it does indeed produce an unchecked cast warning when K2 mode is disabled
I guess it's a K2 bug then? You could create a YouTrack issue for it, or let me know if you would like me to create one
j
I'll do it. Thanks for checking!
KT-77444 K2: Casting from List to List should produce a warning
s
Nice 👍. Would it be better to explicitly mention
MutableList
in the description rather than just
List
? A
List<T>
is always a
List<out T>
so I suspect it wouldn't exhibit the bug.
j
Oh oops
Fixed!