# Kotlin generates redundant bytecode

**URL:** <https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739>\
**Category:** Uncategorized\
**Created:** [December 9, 2017, 2:06am UTC](https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739 "2017-12-09T02:06:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ice1000](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ice1000/32/1981_2.png) [@ice1000](https://discuss.kotlinlang.org/u/ice1000)\
**Post date:** [December 9, 2017, 2:06am UTC](https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739/1 "2017-12-09T02:06:57Z")

</div>

Reference (it’s Chinese so I’ll translate the important part here): [https://lgzh1215.github.io/2017/10/18/huadian/](https://lgzh1215.github.io/2017/10/18/huadian/)

We have:

```kotlin
class A<T : Any>(val data: T)
class B(val string: String)

fun test(a: A<*>) {
    a.data as B // mention the bytecode corresponds to this line
    a.data.string // a.data is Smart casted to B
}

```

And we look at the generated bytecode:

```nohighlight
...
  L1
    LINENUMBER 5 L1
    ALOAD 0
    INVOKEVIRTUAL A.getData ()Ljava/lang/Object; // a.data
    DUP
    IFNONNULL L2 // <============================ null check
    NEW kotlin/TypeCastException
    DUP
    LDC "null cannot be cast to non-null type B"
    INVOKESPECIAL kotlin/TypeCastException.<init> (Ljava/lang/String;)V
    ATHROW
   L2
    CHECKCAST B // <============================ if NotNull then cast
    POP
   L3
...

```

It checks if `a.data` is null. If `not`, `throw TypeCastException("null cannot be cast to non-null type B")`.

However, according to the definition of `a.data`, it should always be a NotNull variable.  
So it’s impossible for `a.data` to be null.

Which means the null check is redundant.

Although I can cast it to a Nullable, this is an unignorable bug.

This happens in Kotlin 1.1 .

Any ideas?

---

<div class="post-metadata">

**Author:** ![Xerus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xerus/32/4499_2.png) [@Xerus](https://discuss.kotlinlang.org/u/Xerus)\
**Post date:** [December 9, 2017, 10:14pm UTC](https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739/2 "2017-12-09T22:14:24Z")

</div>

for cases like that I recommend you to create an issue over at [https://youtrack.jetbrains.com/issues](https://youtrack.jetbrains.com/issues), because this does not seem to need a discussion but a fix

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [December 13, 2017, 12:32pm UTC](https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739/3 "2017-12-13T12:32:28Z")

</div>

By default Kotlin is pessimistic on nullability. Your function a is public so could be called from Java with a null parameter. It will check explicitly for this (there is a way to disable this if desired). Note that CheckCast does not throw on null values so will let an actual null value through.

---

<div class="post-metadata">

**Author:** ![ice1000](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ice1000/32/1981_2.png) [@ice1000](https://discuss.kotlinlang.org/u/ice1000)\
**Post date:** [December 16, 2017, 9:08am UTC](https://discuss.kotlinlang.org/t/kotlin-generates-redundant-bytecode/5739/4 "2017-12-16T09:08:08Z")

</div>

This helps.  
Thanks! 😅
