Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSSStyleVariable APIs allow setting values of the wrong type #2763

Open
bitspittle opened this issue Feb 18, 2023 · 3 comments
Open

CSSStyleVariable APIs allow setting values of the wrong type #2763

bitspittle opened this issue Feb 18, 2023 · 3 comments
Assignees
Labels
bug Something isn't working submitted web

Comments

@bitspittle
Copy link

Simple example code to demonstrate the problem (here, setting a LineStyle variable with a DisplayStyle value):

val unexpectedVariableAssignment by variable<LineStyle>()

@Composable
fun demoUnexpectedVariableAssignment() {
    Div(attrs = {
        style { 
            unexpectedVariableAssignment(DisplayStyle.Flex)
        }
    })
}

Expected: The line unexpectedVariableAssignment(DisplayStyle.Flex) should not compile

Actual: The code compiles and runs fine.

@bitspittle bitspittle added bug Something isn't working submitted labels Feb 18, 2023
@bitspittle
Copy link
Author

bitspittle commented Feb 18, 2023

Digging a bit more, it seems this is because CSSStyleVariable's class declaration uses out variance:

class CSSStyleVariable<out TValue : StylePropertyValue>(override val name: String) : CSSVariable

and the call for invoking a variable has the following API:

operator fun <TValue : StylePropertyValue> CSSStyleVariable<TValue>.invoke(value: TValue) { ... }

so it seems like what is happening is the compiler sees the line unexpectedVariableAssignment(DisplayStyle.Flex) and sees that LineStyle and DisplayStyle are both StylePropertyEnums, so it chooses StylePropertyEnum as the generic type to satisfy invoking the variable.

@bitspittle
Copy link
Author

bitspittle commented Feb 18, 2023

Now I'm wondering why CSSStyleVariable even uses covariance?

What would happen if you took the out out?

class CSSStyleVariable<T : StylePropertyValue>(override val name: String) : CSSVariable

@okushnikov
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working submitted web
Projects
None yet
Development

No branches or pull requests

4 participants