@@ -735,13 +735,26 @@ object desugar {
735
735
.withMods(appMods) :: Nil
736
736
}
737
737
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
+
738
747
val hasRepeatedParam = constrVparamss.head.exists {
739
748
case ValDef (_, tpt, _) => isRepeated(tpt)
740
749
}
741
750
val methName = if (hasRepeatedParam) nme.unapplySeq else nme.unapply
742
751
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)
744
756
val unapplyResTp = if (arity == 0 ) Literal (Constant (true )) else TypeTree ()
757
+
745
758
DefDef (
746
759
methName,
747
760
joinParams(derivedTparams, (unapplyParam :: Nil ) :: Nil ),
0 commit comments