Skip to content

Commit 03d9ad1

Browse files
authored
Merge pull request #14525 from andrzejressel/no_sum_for_enum_case
Don't generate Mirror.Sum for simple enum cases
2 parents 0ddd27d + e80dcba commit 03d9ad1

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Synthesizer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,13 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
273273
val cls = mirroredType.classSymbol
274274
val useCompanion = cls.useCompanionAsSumMirror
275275

276-
if cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
276+
def acceptable(tp: Type): Boolean = tp match
277+
case tp: TermRef => false
278+
case tp: TypeProxy => acceptable(tp.underlying)
279+
case OrType(tp1, tp2) => acceptable(tp1) && acceptable(tp2)
280+
case _ => tp.classSymbol eq cls
281+
282+
if acceptable(mirroredType) && cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
277283
val elemLabels = cls.children.map(c => ConstantType(Constant(c.name.toString)))
278284

279285
def solve(sym: Symbol): Type = sym match

tests/neg/scala_enum_testing.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import scala.util.NotGiven
2+
import scala.compiletime.*
3+
import scala.deriving.Mirror
4+
5+
enum Color:
6+
case Red, Green, Blue
7+
end Color
8+
9+
enum Bar:
10+
case A(i: Int)
11+
case B(b: Boolean)
12+
case C(s: String)
13+
14+
object Singletons {
15+
object A
16+
object B
17+
}
18+
19+
type SumOfK1[F[_]] = Mirror.Sum { type MirroredType[T] = F[T] }
20+
21+
class refined extends scala.annotation.RefiningAnnotation
22+
23+
object Test {
24+
25+
def main(args: Array[String]): Unit = {
26+
summon[Mirror.ProductOf[Color]] // error
27+
summon[Mirror.SumOf[Color.Red.type]] // error
28+
summon[Mirror.SumOf[Color.Red.type | Color.Green.type]] // error
29+
summon[Mirror.SumOf[Bar.A | Bar.B]] // error
30+
summon[Mirror.SumOf[Singletons.A.type | Singletons.B.type]] // error
31+
summon[SumOfK1[[X] =>> Bar]]
32+
summon[SumOfK1[[X] =>> Bar.A | Bar.B]] // error
33+
summon[SumOfK1[[X] =>> (Bar.A | Bar.B) @refined]] // error
34+
object opaques {
35+
opaque type Query[X] = (Bar.A | Bar.B) @refined
36+
}
37+
summon[SumOfK1[opaques.Query]] // error
38+
summon[SumOfK1[[X] =>> (Bar.A @refined) | Bar.B]] // error
39+
}
40+
}

tests/run/scala_enum_testing.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.util.NotGiven
2+
import scala.compiletime.*
3+
import scala.deriving.Mirror
4+
5+
enum Color:
6+
case Red, Green, Blue
7+
end Color
8+
9+
object Test {
10+
11+
def main(args: Array[String]): Unit = {
12+
summon[Mirror.Of[Color.Red.type]]
13+
summon[Mirror.Of[Color]]
14+
summon[Mirror.ProductOf[Color.Red.type]]
15+
summon[Mirror.SumOf[Color]]
16+
}
17+
}

0 commit comments

Comments
 (0)