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

Don't generate Mirror.Sum for simple enum cases #14525

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
bishabosha marked this conversation as resolved.
Show resolved Hide resolved
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]]
}
}