Skip to content

Commit a47d0bc

Browse files
committed
Align case class unapply under -Yscala2-stdlib
1 parent c11d9a4 commit a47d0bc

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,26 @@ object desugar {
735735
.withMods(appMods) :: Nil
736736
}
737737
val unapplyMeth = {
738+
def scala2LibCompatUnapplyRhs(unapplyParamName: Name) =
739+
assert(arity <= Definitions.MaxTupleArity, "Unexpected case class with tuple larger than 22: "+ cdef.show)
740+
val optionApply = Select(Ident(nme.scala), "Option".toTermName)
741+
if arity == 1 then Apply(optionApply, Select(Ident(unapplyParamName), nme._1))
742+
else
743+
val tupleApply = Select(Ident(nme.scala), s"Tuple$arity".toTermName)
744+
val members = List.tabulate(arity) { n => Select(Ident(unapplyParamName), s"_${n+1}".toTermName) }
745+
Apply(optionApply, Apply(tupleApply, members))
746+
738747
val hasRepeatedParam = constrVparamss.head.exists {
739748
case ValDef(_, tpt, _) => isRepeated(tpt)
740749
}
741750
val methName = if (hasRepeatedParam) nme.unapplySeq else nme.unapply
742751
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
743-
val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name)
752+
val unapplyRHS =
753+
if (arity == 0) Literal(Constant(true))
754+
else if ctx.settings.Yscala2Stdlib.value then scala2LibCompatUnapplyRhs(unapplyParam.name)
755+
else Ident(unapplyParam.name)
744756
val unapplyResTp = if (arity == 0) Literal(Constant(true)) else TypeTree()
757+
745758
DefDef(
746759
methName,
747760
joinParams(derivedTparams, (unapplyParam :: Nil) :: Nil),

project/MiMaFilters.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ object MiMaFilters {
7474
// trait $init$
7575
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.*.$init$"),
7676

77-
// Case class unapply
78-
ProblemFilters.exclude[IncompatibleResultTypeProblem]("scala.*.unapply"),
79-
8077
// Value class extension methods
8178
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.*$extension"),
8279
ProblemFilters.exclude[IncompatibleResultTypeProblem]("scala.*$extension"),
@@ -188,9 +185,6 @@ object MiMaFilters {
188185
// Issue #17519: we do not set final on the default methods of final copy method.
189186
ProblemFilters.exclude[FinalMethodProblem]("scala.collection.immutable.Range.copy$default$*"),
190187

191-
// Case class unapply
192-
ProblemFilters.exclude[IncompatibleResultTypeProblem]("scala.*.unapply"),
193-
194188
// Value class extension methods
195189
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.*$extension"),
196190
ProblemFilters.exclude[IncompatibleResultTypeProblem]("scala.*$extension"),

0 commit comments

Comments
 (0)