Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

getOrElse is causing the JVM to crash - https://youtrack.jetbrains.com/issue/KT-29242 #14

Open
dashfwd opened this issue Jul 30, 2019 · 7 comments

Comments

@dashfwd
Copy link

dashfwd commented Jul 30, 2019

Running this code causes the JVM to crash:

    val some10 = Some(10)
    val some11 = some10.map { it + 1 }

    // if present, this next line with cause the crash
    some11.getOrElse { Some(0) }

The crash is this:

Error: Unable to initialize main class dashfwd.CrashDemoKt
Caused by: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    dashfwd/CrashDemoKt.main([Ljava/lang/String;)V @103: invokestatic
  Reason:
    Type 'java/lang/Object' (current frame, stack[0]) is not assignable to 'arrow/core/Option'

Full code to reproduce is available at https://github.com/dashfwd/arrow_crash

This is using:

  • MacOS 10.14.6
  • Kotlin 1.3.41 (seems to happen with other versions too)
  • Open JDK version 12.0.2 (also happens with OpenJDK 11.0.2)
@pakoito
Copy link
Member

pakoito commented Jul 30, 2019

This is a defect on the Kotlin inliner. On the next release we're not inlining getOrElse anymore. Meanwhile try a clean build and report the error to Jetbrains.

@dashfwd
Copy link
Author

dashfwd commented Jul 30, 2019

Thanks. A clean build didn't seem to impact things, I'll report it to JetBrains.

@dashfwd
Copy link
Author

dashfwd commented Jul 30, 2019

FYI: Looks like getOrElse isn't inlined:
fun <T> Option<T>.getOrElse(default: () -> T): T = fold({ default() }, ::identity)

fold() is though:

  inline fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R = when (this) {
    is None -> ifEmpty()
    is Some<A> -> ifSome(t)
  }

@dashfwd
Copy link
Author

dashfwd commented Jul 30, 2019

JetBrains issue link: https://youtrack.jetbrains.com/issue/KT-33054

@danimontoya
Copy link
Contributor

Without targeting:

compileKotlin {
         kotlinOptions.jvmTarget = "1.8"
}

everything seems fine.. but when targeting 1.8 there are many problems with inline functions

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option

@aballano
Copy link
Member

According to https://youtrack.jetbrains.com/issue/KT-29242 this is a bug in the language itself, so nothing specific to Arrow itself.

I guess we should revisit this when they release and we bump to Kotlin 1.3.70.

@aballano aballano changed the title getOrElse is causing the JVM to crash getOrElse is causing the JVM to crash (https://youtrack.jetbrains.com/issue/KT-29242) Jan 24, 2020
@aballano aballano changed the title getOrElse is causing the JVM to crash (https://youtrack.jetbrains.com/issue/KT-29242) getOrElse is causing the JVM to crash - https://youtrack.jetbrains.com/issue/KT-29242 Jan 24, 2020
@rachelcarmena rachelcarmena transferred this issue from arrow-kt/arrow Feb 20, 2020
@frikit
Copy link

frikit commented Oct 23, 2020

Bug fixed in kotlin 1.3.70+, tested in https://github.com/dashfwd/arrow_crash with 1.3.70 is working fine

I think the issue can be closed now

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants