BKMbigo / wasm-compose-crash

0 stars 0 forks source link

Compose/WASM crashes on `remember { null }` #1

Open BKMbigo opened 1 year ago

BKMbigo commented 1 year ago

Compose/WASM crashes whenever such references are encountered

import kotlinx.coroutines.Job

@Composable function {
   var job: Job? = remember { null }
}

Example:

// Will crash

import kotlinx.coroutines.Job

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var job: Job? = remember { null }
        Text("Good Morning")
    }
}

The canvas will not display the Text "Good Morning" and no exception is thrown

However, references in Side Effects do not throw the exception... Example:

// Does not crash

import kotlinx.coroutines.Job

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        LaunchedEffect(Unit) {
            var job: Job? = null
        }

        Text("Good Morning")
    }
}
BKMbigo commented 1 year ago
// Does not crash

import kotlinx.coroutines.Job

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var job: Job? =  null
        Text("Good Morning")
    }
}
BKMbigo commented 1 year ago

Turns out that the problem has nothing to do with Kotlin coroutines but is related to any call remember { null }

// Crashes

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var bar: Bar? = remember { null }

        Text("Good Morning")
    }
}

class Bar
BKMbigo commented 1 year ago

Creating a generic function that returns null

// Does not crash

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var bar: Bar? = remember { functionThatReturnsNull() }

        Text("Good Morning")
    }
}

class Bar

fun <T> functionThatReturnsNull(): T? = null

This can be used as a workaround if remember { null } is needed

BKMbigo commented 1 year ago

Trying kotlin Unit

// Does not crash

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var bar: Unit = remember { null }

        Text("Good Morning")
    }
}
BKMbigo commented 1 year ago

Trying kotlin stdlib classes

// Crashes

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var bar: Int? = remember { null }

        Text("Good Morning")
    }
}
BKMbigo commented 1 year ago

Explicitly stating type in remember does not help

// Crashes

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    CanvasBasedWindow {
        var bar: Int? = remember<Int?> { null }

        Text("Good Morning")
    }
}
BKMbigo commented 1 year ago

Crash still present on Kotlin 1.9.0 and compose 1.4.0-dev-wasm09