Skip to content

Commit d317135

Browse files
committed
Disallow Sum with enum or discointed class
1 parent d25fc49 commit d317135

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

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

276-
if (!mirroredType.termSymbol.isEnumCase && (cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner))) then
276+
val isDisjointed = mirroredType match {
277+
case OrType(t1, t2) => TypeComparer.provablyDisjoint(t1, t2)
278+
case _ => false
279+
}
280+
281+
def isSumWithSingleton(t: Type): Boolean = {
282+
t match {
283+
case OrType(t1, t2) => isSumWithSingleton(t1) || isSumWithSingleton(t2)
284+
case _ => t.termSymbol.isEnumCase
285+
}
286+
}
287+
288+
if (!isDisjointed && !isSumWithSingleton(mirroredType) && cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner)) then
277289
val elemLabels = cls.children.map(c => ConstantType(Constant(c.name.toString)))
278290

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

tests/neg/scala_enum_testing.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ enum Color:
66
case Red, Green, Blue
77
end Color
88

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+
919
object Test {
1020

1121
def main(args: Array[String]): Unit = {
1222
summon[Mirror.ProductOf[Color]] // error
1323
summon[Mirror.SumOf[Color.Red.type]] // error
24+
summon[Mirror.SumOf[Color.Red.type | Color.Green.type]] // error
25+
summon[Mirror.SumOf[Bar.A | Bar.B]] // error
26+
summon[Mirror.SumOf[Singletons.A.type | Singletons.B.type]] // error
1427
}
1528
}

0 commit comments

Comments
 (0)