@@ -223,16 +223,19 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
223223 /** Create an anonymous class `new Object { type MirroredMonoType = ... }`
224224 * and mark it with given attachment so that it is made into a mirror at PostTyper.
225225 */
226- private def anonymousMirror (monoType : Type , attachment : Property .StickyKey [Unit ], span : Span )(using Context ) =
226+ private def anonymousMirror (monoType : Type , attachment : Property .StickyKey [Unit ], tupleArity : Option [ Int ], span : Span )(using Context ) =
227227 if ctx.isAfterTyper then ctx.compilationUnit.needsMirrorSupport = true
228228 val monoTypeDef = untpd.TypeDef (tpnme.MirroredMonoType , untpd.TypeTree (monoType))
229- val newImpl = untpd.Template (
229+ var newImpl = untpd.Template (
230230 constr = untpd.emptyConstructor,
231231 parents = untpd.TypeTree (defn.ObjectType ) :: Nil ,
232232 derived = Nil ,
233233 self = EmptyValDef ,
234234 body = monoTypeDef :: Nil
235235 ).withAttachment(attachment, ())
236+ tupleArity.foreach { n =>
237+ newImpl = newImpl.withAttachment(GenericTupleArity , n)
238+ }
236239 typer.typed(untpd.New (newImpl).withSpan(span))
237240
238241 /** The mirror type
@@ -279,6 +282,20 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
279282 private [Synthesizer ] enum MirrorSource :
280283 case ClassSymbol (cls : Symbol )
281284 case Singleton (src : Symbol , tref : TermRef )
285+ case GenericTuple (tuplArity : Int , tpArgs : List [Type ])
286+
287+ def whyNotGenericProd (using Context ): Option [String ] = this match
288+ case GenericTuple (arity, _) =>
289+ val maxArity = Definitions .MaxTupleArity
290+ if arity <= maxArity then None
291+ else Some (i " it reduces to tuple with arity $arity, expected arity <= $maxArity" )
292+ case ClassSymbol (cls) => if cls.isGenericProduct then None else Some (cls.whyNotGenericProduct)
293+ case _ => None
294+
295+ /** tuple arity, works for TupleN classes and generic tuples */
296+ final def arity (using Context ): Int = this match
297+ case GenericTuple (arity, _) => arity
298+ case _ => - 1
282299
283300 /** A comparison that chooses the most specific MirrorSource, this is guided by what is necessary for
284301 * `Mirror.Product.fromProduct`. i.e. its result type should be compatible with the erasure of `mirroredType`.
@@ -289,12 +306,29 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
289306 case (ClassSymbol (cls1), ClassSymbol (cls2)) => cls1.isSubClass(cls2)
290307 case (Singleton (src1, _), Singleton (src2, _)) => src1 eq src2
291308 case (_ : ClassSymbol , _ : Singleton ) => false
309+ case _ => (this .arity, that.arity) match
310+ case (n, m) if n > 0 || m > 0 =>
311+ // we shortcut when at least one was a tuple.
312+ // This protects us from comparing classes for two TupleXXL with different arities.
313+ n == m
314+ case _ => false
315+
316+ def asClass (using Context ): Symbol = this match
317+ case ClassSymbol (cls) => cls
318+ case Singleton (src, _) => src.info.classSymbol
319+ case GenericTuple (arity, _) =>
320+ if arity <= Definitions .MaxTupleArity then defn.TupleType (arity).nn.classSymbol
321+ else defn.PairClass
292322
293323 def show (using Context ): String = this match
294324 case ClassSymbol (cls) => i " $cls"
295325 case Singleton (src, _) => i " $src"
326+ case GenericTuple (arity, _) =>
327+ if arity <= Definitions .MaxTupleArity then i " class Tuple $arity"
328+ else i " trait Tuple { def size: $arity } "
296329
297330 private [Synthesizer ] object MirrorSource :
331+ type WithArity = ClassSymbol | GenericTuple
298332
299333 /** Reduces a mirroredType to either its most specific ClassSymbol,
300334 * or a TermRef to a singleton value. These are
@@ -332,6 +366,9 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
332366 reduce(tp.underlying)
333367 case tp : HKTypeLambda if tp.resultType.isInstanceOf [HKTypeLambda ] =>
334368 Left (i " its subpart ` $tp` is not a supported kind (either `*` or `* -> *`) " )
369+ case tp @ GenericTupleType (args) =>
370+ val arity = args.size
371+ Right (MirrorSource .GenericTuple (arity, args))
335372 case tp : TypeProxy =>
336373 reduce(tp.underlying)
337374 case tp @ AndType (l, r) =>
@@ -354,10 +391,15 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
354391
355392 private def productMirror (mirroredType : Type , formal : Type , span : Span )(using Context ): TreeWithErrors =
356393
357- def makeProductMirror (cls : Symbol ): TreeWithErrors =
358- val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal ))
394+ def makeProductMirror (msrc : MirrorSource .WithArity ): TreeWithErrors =
395+ val mirroredClass = msrc.asClass
396+ val accessors = mirroredClass.caseAccessors.filterNot(_.isAllOf(PrivateLocal ))
359397 val elemLabels = accessors.map(acc => ConstantType (Constant (acc.name.toString)))
360- val nestedPairs = TypeOps .nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
398+ val (arity, nestedPairs) = msrc match
399+ case MirrorSource .GenericTuple (arity, args) =>
400+ (Some (arity), TypeOps .nestedPairs(args))
401+ case MirrorSource .ClassSymbol (cls) =>
402+ (None , TypeOps .nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr)))
361403 val (monoType, elemsType) = mirroredType match
362404 case mirroredType : HKTypeLambda =>
363405 (mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
@@ -367,12 +409,12 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
367409 checkRefinement(formal, tpnme.MirroredElemTypes , elemsType, span)
368410 checkRefinement(formal, tpnme.MirroredElemLabels , elemsLabels, span)
369411 val mirrorType =
370- mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, cls .name, formal)
412+ mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, mirroredClass .name, formal)
371413 .refinedWith(tpnme.MirroredElemTypes , TypeAlias (elemsType))
372414 .refinedWith(tpnme.MirroredElemLabels , TypeAlias (elemsLabels))
373415 val mirrorRef =
374- if cls .useCompanionAsProductMirror then companionPath(mirroredType, span)
375- else anonymousMirror(monoType, ExtendsProductMirror , span)
416+ if mirroredClass .useCompanionAsProductMirror then companionPath(mirroredType, span)
417+ else anonymousMirror(monoType, ExtendsProductMirror , arity, span)
376418 withNoErrors(mirrorRef.cast(mirrorType))
377419 end makeProductMirror
378420
@@ -389,9 +431,10 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
389431 else
390432 val mirrorType = mirrorCore(defn.Mirror_SingletonClass , mirroredType, mirroredType, singleton.name, formal)
391433 withNoErrors(singletonPath.cast(mirrorType))
392- case MirrorSource .ClassSymbol (cls) =>
393- if cls.isGenericProduct then makeProductMirror(cls)
394- else withErrors(i " $cls is not a generic product because ${cls.whyNotGenericProduct}" )
434+ case msrc : MirrorSource .WithArity =>
435+ msrc.whyNotGenericProd match
436+ case Some (err) => withErrors(i " ${msrc.asClass} is not a generic product because $err" )
437+ case _ => makeProductMirror(msrc)
395438 case Left (msg) =>
396439 withErrors(i " type ` $mirroredType` is not a generic product because $msg" )
397440 end productMirror
@@ -400,7 +443,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
400443
401444 val (acceptableMsg, cls) = MirrorSource .reduce(mirroredType) match
402445 case Right (MirrorSource .Singleton (_, tp)) => (i " its subpart ` $tp` is a term reference " , NoSymbol )
403- case Right (MirrorSource . ClassSymbol (cls)) => (" " , cls )
446+ case Right (msrc) => (" " , msrc.asClass )
404447 case Left (msg) => (msg, NoSymbol )
405448
406449 val clsIsGenericSum = cls.isGenericSum
@@ -457,7 +500,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
457500 .refinedWith(tpnme.MirroredElemLabels , TypeAlias (TypeOps .nestedPairs(elemLabels)))
458501 val mirrorRef =
459502 if cls.useCompanionAsSumMirror then companionPath(mirroredType, span)
460- else anonymousMirror(monoType, ExtendsSumMirror , span)
503+ else anonymousMirror(monoType, ExtendsSumMirror , tupleArity = None , span)
461504 withNoErrors(mirrorRef.cast(mirrorType))
462505 else if acceptableMsg.nonEmpty then
463506 withErrors(i " type ` $mirroredType` is not a generic sum because $acceptableMsg" )
0 commit comments