iRevive / union-derivation

A micro-library to derive a typeclass for Scala 3 Union types.
MIT License
29 stars 1 forks source link

Regression in 0.2.0 #176

Open majk-p opened 3 hours ago

majk-p commented 3 hours ago

Problem definition

After upgrading union-derivation to 0.2.0 I started seeing following error on my local and CI builds:

[error] UnionDerivation cannot derive an instance of trait Encoder for the type `scala.Int | java.lang.String`.
[error] Reason: the abstract method without the polymorphic param isn't supported.
[error] Hint: check the example below where the instance cannot be derived
[error] 
[error] trait Typeclass[A] {
[error]   def magic(a: Int): String
[error]   //              ^
[error]   // Polymorphic param of type A is missing
[error] }
[error] 
[error]   summon[Encoder[Int | String]].encode(1)
[error]                                ^
Error compiling

Here's the closest possible simplification of the codebase: https://gist.github.com/majk-p/c54649500f2a81df9e749254fbea532e

The extra issue is that the compilation error doesn't always happen with the gist, eg running scala-cli run https://gist.github.com/majk-p/c54649500f2a81df9e749254fbea532e usually ends up with successful compilation and execution.

The same example saved locally in a fresh folder would fail compilation at times.

This issue has never happened to me before when using 0.1.0

On -Yretain-trees

Locally when I added //> using options "-Yretain-trees" to the scala-cli repro example it started working again, but the same solution didn't work for my real-world big sbt project. I suspect that adding the flag did something to the compiler rendering the bug intermittent.

Please let me know if there are other diagnostics I could do in the sbt project where the problem is repeatable.

Related reports

This issue seems to be closely related to what https://github.com/iRevive/union-derivation/issues/169#issuecomment-2478914761 reported by @matwojcik

iRevive commented 2 hours ago

Thanks for the example. I will take a look this week.