-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Can't transform coproduct - Java Enum to Sealed trait #482
Comments
Hello! Thanks for the bug report and the reproduction. I am not sure when I'll have the time to take a look at this closer. If code compiles when you merely use the definition in scope, I suspect it might be a bug in compiler, that we'd have to workaround (and report). There was such case in Scala 2 macros - see scala/bug#7755 that we have to work around by some hack - perhaps similar issue exists in Scala 3, we'd have to create a test case for it and work around it (if it's even possible somehow). @jchyb have you heard of such issues? |
@frevib I tried to reproduce but when I compiled https://github.com/frevib/chimney-java-enum-bug-example/ there was no error. Is there something else I need to do to reproduce? |
I tried and got the same compiler error, i'm on mac M3:
|
@MateuszKubuszok no, just |
@frevib, @resilica What else can you tell me about your environment? From what I see both of you are using Java 17.0.9 (Temurin and Adoption), should I assume both are MacOS with M architecture? I ask because my result is:
So on my M2 it compiles without issues. If the same code might or might not compile on different laptops there must be something different about the environment. |
I tried it on an intel with the same sbt/scala/java versions and it seems to compile fine as well... Can you try it with 17.0.9? |
@MateuszKubuszok @resilica this is really confusing. I'm on Mac m3 (max). I've tried a couple of times and it succeeds, then fails. Here is my output, I've added
Intellij also thinks it's an error: |
I am pretty sure at this point that this is a compiler bug which for this code: def isSealed[A: Type]: Boolean = {
val flags = TypeRepr.of[A].typeSymbol.flags
flags.is(Flags.Enum) || flags.is(Flags.Sealed)
} sometimes returns def parse[A: Type]: Option[Enum[A]] =
// no need for separate java.lang.Enum handling contrary to Scala 2
if isSealed[A] then Some(symbolsToEnum(extractSealedSubtypes[A]))
else None into def parse[A: Type]: Option[Enum[A]] =
// we should have no need for separate java.lang.Enum handling contrary to Scala 2
// but there is a bug that makes it necessary
if isSealed[A] || isJavaEnum[A] then Some(symbolsToEnum(extractSealedSubtypes[A]))
else None but with no reproduction I can neither test if that would actually help nor report a bug to the Scala 3 compiler team. :/ |
@MateuszKubuszok for now the only way to reproduce this, is doing a lot of For now I’ll add an Enum instance in scope. |
I'm on a conference now, so I have very limited capability for debugging this. However I've run: for i in $(seq 1 100); do sbt clean compile; done and let the code Looking at the code I figured out that without a reproduction I am missing data for any workaround (just checking if class extends |
Thanks for your effort looking into it. For now I'll use the mitigation by placing |
I've made some blind best-effort attempt at https://github.com/scalalandio/chimney/compare/scala-3-java-enum-bugfix?expand=1 - if that would solve the issue, I could merge but, but if it doesn't then I am unfortunately out of options, and I can only hope that it will gets reproduced by someone in Scala 3 compiler team. |
My Intellij (Zinc compiler) is consistent in saying this is an error, sbt is not. I could check out and build above branch and see what Intellij has to say. This will not give 100% confidence, but it's something. |
@frevib were you able to check if the workaround helps with anything? |
Yes, adding an instance of the enum in scope solves the problem: val enumJava = EnumJava.ONE This can be added top-level or anywhere else in scope, e.g. inside the method. |
And how about the fix from the branch https://github.com/scalalandio/chimney/compare/scala-3-java-enum-bugfix?expand=1 ? Does it help with anything or is it a dead end? |
I did not try that yet. I got a bit stuck in building the Chimney package from source. Are there instructions on how to build Chimney? I will do it asap then. |
I think |
Unfortunately I cannot give a definitive answer. I've tried the new At this point I don't know any more. Intellij's UI keeps saying that @resilica can you try one more time? If no one else has any problems we can close this issue. |
git clone https://github.com/scalalandio/chimney.git
cd chimney
git checkout scala-3-java-enum-bugfix
export RELEASE=true # turn off -Xfatal-warnings which fails scaladoc (sic!) generation
sbt chimneyMacroCommons3/publishLocal chimney3/publishLocal
# then use chimney in version 1.0.0-M2-2-g775e2ce6-SNAPSHOT However, I am almost certain that it would be some weird bug in the compiler. I might have even more certainty (or rule that out) if failed logs were enriched with |
Yes, I also had to include the java-collections:
Then use the The error still occurs at random times. 95% of the times it works though. I was able to reproduce it for a while by deleting the |
If the workaround didn't help then I am not sure anymore where the bug occurs. If it's not the flags, then maybe the |
Thx for looking into it. @resilica where you able to test? |
Since there is nothing we can do do fix this, and the only solution I can see is fixing it in the compiler, I'll close it now. |
Checklist
TransformerF
s) orunsafeOption
flagsDescribe the bug
If we try to convert a Java Enum to a Sealed trait, we get
However, when we put one instance of the Java Enum in scope, e.g. by adding this to the top-level
val enumJava = EnumJava.ONE
, compilation succeeds.Reproduction
We have the Scala file in
src/main/scala
:And the Java Enum in the
src/main/java
directory:Compilation now fails with
can't transform coproduct instance com.eventlooopsoftware.javaenum.EnumJava to SealedTraitScala
.However if we add
val enumJava = EnumJava.ONE
somewhere in the file, compilation succeeds.Full example is here: https://github.com/frevib/chimney-java-enum-bug-example/blob/main/src/main/scala/Main.scala
Expected behavior
Chimney to successfully transform the Java Enum to Scala Sealed trait.
Actual behavior
can't transform coproduct instance com.eventlooopsoftware.javaenum.EnumJava to SealedTraitScala
Which Chimney version do you use
0.8.5 and 1.0.0-M1
Which platform do you use
If you checked JVM
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Temurin-17.0.9+9 (build 17.0.9+9)
OpenJDK 64-Bit Server VM Temurin-17.0.9+9 (build 17.0.9+9, mixed mode)
Additional context
I have an idea that Chimney might not be able to find the Java Enum on the classpath during compile time. I've tried several
CompileOrder
s, but to no use.The text was updated successfully, but these errors were encountered: