Skip to content

Commit 73d21ee

Browse files
committed
Change return type of apply.
In an enum case like case C() extends P1 with ... with Pn ... apply now returns `P1 & ... & Pn`, where before it was just P1. Also, add to Option test.
1 parent 879854d commit 73d21ee

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,6 @@ object desugar {
344344
// new C[Ts](paramss)
345345
lazy val creatorExpr = New(classTypeRef, constrVparamss nestedMap refOfDef)
346346

347-
// The return type of the `apply` and `copy` methods
348-
val applyResultTpt =
349-
if (isEnumCase)
350-
if (parents.isEmpty) enumClassTypeRef
351-
else parents.head
352-
else TypeTree()
353-
354347
// Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams)
355348
// def isDefined = true
356349
// def productArity = N
@@ -436,6 +429,13 @@ object desugar {
436429
// For all other classes, the parent is AnyRef.
437430
val companions =
438431
if (isCaseClass) {
432+
// The return type of the `apply` method
433+
val applyResultTpt =
434+
if (isEnumCase)
435+
if (parents.isEmpty) enumClassTypeRef
436+
else parents.reduceLeft(AndTypeTree)
437+
else TypeTree()
438+
439439
val parent =
440440
if (constrTparams.nonEmpty ||
441441
constrVparamss.length > 1 ||

tests/run/enum-Option.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ enum class Option[+T] extends Serializable {
22
def isDefined: Boolean
33
}
44
object Option {
5+
def apply[T](x: T): Option[T] = if (x == null) None else Some(x)
56
case Some(x: T) {
67
def isDefined = true
78
}
@@ -11,6 +12,8 @@ object Option {
1112
}
1213

1314
object Test {
14-
def main(args: Array[String]) =
15+
def main(args: Array[String]) = {
1516
assert(Some(None).isDefined)
17+
Option(22) match { case Option.Some(x) => assert(x == 22) }
18+
}
1619
}

0 commit comments

Comments
 (0)