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 3 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
6 changes: 5 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,11 @@ 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 OrType(tp1, tp2) => acceptable(tp1) && acceptable(tp2)
case _ => !tp.termSymbol.isEnumCase && (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
28 changes: 28 additions & 0 deletions tests/neg/scala_enum_testing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
}

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
}
}
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]]
}
}