Skip to content

Commit 3857210

Browse files
fix defaultArgument body synthesis: assign type Any to all cases, so match tree does not try to find the lub
1 parent d9d65a4 commit 3857210

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,10 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
538538
val getterDenot = companionTree.tpe.member(getterName)
539539
companionTree.select(TermRef(companionTree.tpe, getterName, getterDenot))
540540

541-
val withDefaultCases =
542-
for (acc, idx) <- caseClass.caseAccessors.zipWithIndex if acc.is(HasDefault)
543-
yield CaseDef(Literal(Constant(idx)), EmptyTree, defaultArgumentGetter(idx))
541+
val withDefaultCases = for
542+
(acc, idx) <- caseClass.caseAccessors.zipWithIndex if acc.is(HasDefault)
543+
body = Typed(defaultArgumentGetter(idx), TypeTree(defn.AnyType)) // so match tree does try to find union of case types
544+
yield CaseDef(Literal(Constant(idx)), EmptyTree, body)
544545

545546
val withoutDefaultCase =
546547
val stringIndex = Apply(Select(index, nme.toString_), Nil)

tests/run/mirror-defaultArgument.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ object Test extends App:
4545
val m5 = summon[Mirror.Of[outer.Inner]]
4646
assert(m5.defaultArgument(1) == 3 + 1)
4747
outer.localTest(9d)
48+
49+
50+
// new defaultArgument match tree should be able to unify different default value types
51+
case class Foo[T](x: Int = 0, y: String = "hi")
52+

0 commit comments

Comments
 (0)