Hi folks. I think I’ve uncovered a minor bug with ...
# k2-adopters
k
Hi folks. I think I’ve uncovered a minor bug with the Kotlin K2 compiler. I’ve enabled
-Werror
on my project and it instructed me to delete an
else
branch that was previously required for a
when
statement with an
expect enum class
as the subject. I flagged in in the #C0B8H786P channel here, and after discussing, I think there’s two issues: 1. This code shouldn’t compile at all since expect enums can have less entries than their actual counterparts. 2. The else branch shouldn’t have been flagged as unnecessary by the compiler. Does this seem like it’s an actual bug? Here’s the PR where I surfaced this inconsistency. cc @Anna Kozlova
d
Unfortunately, it's more or less expected behavior caused by the existing compilation scheme. Right now when you compile your KMP app to some platform, the compiler analyzes your sourcests which belong to this platform against the same dependencies from that platform. So in your case, when you compile your common code referencing the
Month
from
kotlinx.datetime
, it actualy resolves to the
actual enum class Month
from
kotlinx-datetime-jvm.jar
(or any other platform), so the compiler reports this
else
branch as redundant. But when you run
buildCommonMetadata
task, then your common code is analyzed against
expect enum class Month
from
kotlinx-datetime-common.klib
, so the
else
branch is required here. There is no proper workaround here except suppressing the warning (
@Suppress("REDUNDANT_ELSE_IN_WHEN")
).
But the good news is that right now we are actively working on the new compilation scheme (KT-75268), in which common code will be analyzed against common dependencies even during the platform compilation, so eventually this problem will gone. Can't give any proper estimates though, as the task is quite complex. Probably the new mode will be ready in some alpha/beta/preview state in 2.3, but I can't guarantee that
k
That’s really great to hear that this is being worked on and will be fixed eventually. Am I correct to say that the only reason this compiles for all platforms I currently support in my library is because
expect enum Month
doesn’t have any additional entries in any of the
actual class Month
instances?
d
Yes, that's right
k
Awesome, thanks for helping me understand.
👌 1
I’ll probably leave as-is for now because I don’t expect any new months to be added to the gregorian calendar, but this is useful knowledge for the future.
👍 1