@@ -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 ),
0 commit comments