diff --git a/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala b/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala index fe4feecd6100..cb45e5343810 100644 --- a/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala @@ -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 diff --git a/tests/neg/scala_enum_testing.scala b/tests/neg/scala_enum_testing.scala new file mode 100644 index 000000000000..043995460742 --- /dev/null +++ b/tests/neg/scala_enum_testing.scala @@ -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 + } +} diff --git a/tests/run/scala_enum_testing.scala b/tests/run/scala_enum_testing.scala new file mode 100644 index 000000000000..f529b900e875 --- /dev/null +++ b/tests/run/scala_enum_testing.scala @@ -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]] + } +}