Skip to content

Commit 4dae53b

Browse files
committed
Fix problem with parameterized enums
The logic for generating the result type of apply was wrong if value parameters were passed to the enum class. Test case in objXfun.scala.
1 parent 3bb3c2d commit 4dae53b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,18 @@ object desugar {
431431
// For all other classes, the parent is AnyRef.
432432
val companions =
433433
if (isCaseClass) {
434+
def extractType(t: Tree): Tree = t match {
435+
case Apply(t1, _) => extractType(t1)
436+
case TypeApply(t1, ts) => AppliedTypeTree(extractType(t1), ts)
437+
case Select(t1, nme.CONSTRUCTOR) => extractType(t1)
438+
case New(t1) => t1
439+
case t1 => t1
440+
}
434441
// The return type of the `apply` method
435442
val applyResultTpt =
436443
if (isEnumCase)
437444
if (parents.isEmpty) enumClassTypeRef
438-
else parents.reduceLeft(AndTypeTree)
445+
else parents.map(extractType).reduceLeft(AndTypeTree)
439446
else TypeTree()
440447

441448
val parent =

tests/pos/objXfun.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Foo extends (Int => Int) { // OK
2+
def apply(x: Int) = x
3+
}
4+
5+
enum class E(x: Int) // used to generate Int => new E(x) as the parent of object E --> crash
6+
object E {
7+
case C(x: Int) extends E(x)
8+
}

0 commit comments

Comments
 (0)