JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.03k stars 1.16k forks source link

CompositionLocalProvider related RuntimeError: illegal cast #5074

Closed bnorm closed 2 months ago

bnorm commented 3 months ago

Describe the bug

It seems that a CompositionLocalProvider nested within another CompositionLocalProvider is causing a crash on K/Wasm.

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    ComposeViewport(document.body!!) {
        MaterialTheme { // Anything that is or uses CompositionLocalProvider
            Example { // Important! MaterialTheme used directly works
                Text("Hello, World!")
            }
        }
    }
}

@Composable
fun Example(content: @Composable () -> Unit) {
    MaterialTheme {
        content()
    }
}

Affected platforms

Versions

To Reproduce Sample project using https://kmp.jetbrains.com/ : ComposeWasmCrash.zip

Expected behavior App to load and "Hello, World!" to be displayed.

Screenshots

image

From the browser console:

Uncaught (in promise) RuntimeError: illegal cast
    at <ComposeWasmCrash:composeApp>.androidx.compose.runtime.CompositionLocalProvider (composeApp.wasm:0x311483)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.PlatformMaterialTheme (MaterialTheme.js.kt:26:5)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.MaterialTheme$lambda$lambda.invoke (MaterialTheme.kt:80:13)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.MaterialTheme$lambda$lambda.invoke (composeApp.wasm:0x50ecd3)
    at <ComposeWasmCrash:composeApp>.androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (composeApp.wasm:0x365d61)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.ComposableLambda$invoke$ref.invoke (composeApp.wasm:0x50ed38)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.ComposableLambda$invoke$ref.invoke (composeApp.wasm:0x50eda1)
    at <ComposeWasmCrash:composeApp>.androidx.compose.runtime.CompositionLocalProvider (composeApp.wasm:0x311491)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.ProvideTextStyle (Text.kt:399:5)
    at <ComposeWasmCrash:composeApp>.androidx.compose.material.MaterialTheme$lambda.invoke (MaterialTheme.kt:79:9)
HamaniKhalil commented 3 months ago

Ha, I was about to create an issue like this one ! I confirm that this is happening in compose 1.7.0-alpha01. Because I had about the same stacktrace and it was fixed only by downgrading to compose 1.6.11 !!! In my case the CompositionLocalProvider that crashed was something related to Surface from Material3, also tried to downgrade to Surface from Material i.e. Material 2.

PMARZV commented 3 months ago

A workaround is here https://github.com/JetBrains/compose-multiplatform/issues/5045#issuecomment-2210683603 (my problem was with Surface Material 3)

eymar commented 2 months ago

Hi @bnorm! Thanks for reporting!

Indeed, 1.7.0-dev and alpha (and all furter builds) won't work in web with kotlin 2.0.0, because the compose compiler for web for 2.0.0 doesn't support a new API added in compose runtime 1.7.0

For now, you can use kotlin:

The fix can't be backported to 2.0.0, it will be available only in kotlin 2.0.10.

eymar commented 2 months ago

I'm closing this one as I see no particular actions on our side, except suggesting to use a correct Kotlin version. Feel free to add more questions or reopen if needed.

bnorm commented 2 months ago

I can confirm that version 2.0.10-RC-515 does indeed my issue. Thanks!

I did test this with 2.0.20-Beta1 and the issue was present there. I'm guessing the issue will be fixed in the next 2.0.20 release as well?

eymar commented 2 months ago

Yes, the fix was implemented a bit a later than 2.0.20-Beta1.

2.0.20-Beta2 should have it.

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.