Skip to content

Commit

Permalink
Merge pull request #14525 from andrzejressel/no_sum_for_enum_case
Browse files Browse the repository at this point in the history
Don't generate Mirror.Sum for simple enum cases
  • Loading branch information
bishabosha authored Mar 14, 2022
2 parents 0ddd27d + e80dcba commit 03d9ad1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,13 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
val cls = mirroredType.classSymbol
val useCompanion = cls.useCompanionAsSumMirror

if cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
def acceptable(tp: Type): Boolean = tp match
case tp: TermRef => false
case tp: TypeProxy => acceptable(tp.underlying)
case OrType(tp1, tp2) => acceptable(tp1) && acceptable(tp2)
case _ => tp.classSymbol eq cls

if acceptable(mirroredType) && cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
val elemLabels = cls.children.map(c => ConstantType(Constant(c.name.toString)))

def solve(sym: Symbol): Type = sym match
Expand Down
40 changes: 40 additions & 0 deletions tests/neg/scala_enum_testing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import scala.util.NotGiven
import scala.compiletime.*
import scala.deriving.Mirror

enum Color:
case Red, Green, Blue
end Color

enum Bar:
case A(i: Int)
case B(b: Boolean)
case C(s: String)

object Singletons {
object A
object B
}

type SumOfK1[F[_]] = Mirror.Sum { type MirroredType[T] = F[T] }

class refined extends scala.annotation.RefiningAnnotation

object Test {

def main(args: Array[String]): Unit = {
summon[Mirror.ProductOf[Color]] // error
summon[Mirror.SumOf[Color.Red.type]] // error
summon[Mirror.SumOf[Color.Red.type | Color.Green.type]] // error
summon[Mirror.SumOf[Bar.A | Bar.B]] // error
summon[Mirror.SumOf[Singletons.A.type | Singletons.B.type]] // error
summon[SumOfK1[[X] =>> Bar]]
summon[SumOfK1[[X] =>> Bar.A | Bar.B]] // error
summon[SumOfK1[[X] =>> (Bar.A | Bar.B) @refined]] // error
object opaques {
opaque type Query[X] = (Bar.A | Bar.B) @refined
}
summon[SumOfK1[opaques.Query]] // error
summon[SumOfK1[[X] =>> (Bar.A @refined) | Bar.B]] // error
}
}
17 changes: 17 additions & 0 deletions tests/run/scala_enum_testing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.util.NotGiven
import scala.compiletime.*
import scala.deriving.Mirror

enum Color:
case Red, Green, Blue
end Color

object Test {

def main(args: Array[String]): Unit = {
summon[Mirror.Of[Color.Red.type]]
summon[Mirror.Of[Color]]
summon[Mirror.ProductOf[Color.Red.type]]
summon[Mirror.SumOf[Color]]
}
}

0 comments on commit 03d9ad1

Please sign in to comment.