lgou2w/ldk

Kotlin @Suppress("UNCHECKED_CAST") causes 'CHECKCAST kotlin/Unit' bytecode problem

Closed this issue · 2 comments

See line 248:

fun <T, R> NBTTagCompound.removeIf(
key: String,
transform: Function<T, R>,
predicate: Predicate<R>?
): NBTTagCompound {
if (predicate == null) {
remove(key)
} else {
val value = get(key) ?: return this
@Suppress("UNCHECKED_CAST")
if (value.type.isWrapper() && predicate(transform(value as T)))
remove(key)
else if (predicate(transform(value.value as T)))
remove(key)
}
return this
}

After reviewing the bytecode, there will be an extra CHECKCAST kotlin/Unit directive under the remove(key) function.

See below:

L14
  LINENUMBER 252 L14
  ALOAD 0
  ALOAD 1
  INVOKEVIRTUAL com/lgou2w/ldk/nbt/NBTTagCompound.remove (Ljava/lang/Object;)Ljava/lang/Object;
  CHECKCAST kotlin/Unit // <--- HERE
  POP
  GOTO L15
L13
L16
  LINENUMBER 251 L16
L15
  LINENUMBER 249 L15
L12
  NOP
L17

It throws an java.lang.ClassCastException: com.lgou2w.ldk.nbt.NBTTagShort cannot be cast to kotlin.Unit error when I debug with the following code:

val compound = ofCompound { putShort("key", 1) }
  .removeIf<Short>("key") { it == 1.toShort() }

When I remove @Suppress("UNCHECKED_CAST"), the check command disappears, I think this is a problem with the kotlin compiler itself. But I will fix a version urgently and remove it. 😢

I'm not sure if the kotlin problem tracker has the same problem, but I'm sure this is the kotlin compiler's own problem.

Fixed in 0.1.8-beta12-hotfix2 .