@@ -223,16 +223,19 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
223
223
/** Create an anonymous class `new Object { type MirroredMonoType = ... }`
224
224
* and mark it with given attachment so that it is made into a mirror at PostTyper.
225
225
*/
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 ) =
227
227
if ctx.isAfterTyper then ctx.compilationUnit.needsMirrorSupport = true
228
228
val monoTypeDef = untpd.TypeDef (tpnme.MirroredMonoType , untpd.TypeTree (monoType))
229
- val newImpl = untpd.Template (
229
+ var newImpl = untpd.Template (
230
230
constr = untpd.emptyConstructor,
231
231
parents = untpd.TypeTree (defn.ObjectType ) :: Nil ,
232
232
derived = Nil ,
233
233
self = EmptyValDef ,
234
234
body = monoTypeDef :: Nil
235
235
).withAttachment(attachment, ())
236
+ tupleArity.foreach { n =>
237
+ newImpl = newImpl.withAttachment(GenericTupleArity , n)
238
+ }
236
239
typer.typed(untpd.New (newImpl).withSpan(span))
237
240
238
241
/** The mirror type
@@ -279,6 +282,20 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
279
282
private [Synthesizer ] enum MirrorSource :
280
283
case ClassSymbol (cls : Symbol )
281
284
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
282
299
283
300
/** A comparison that chooses the most specific MirrorSource, this is guided by what is necessary for
284
301
* `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):
289
306
case (ClassSymbol (cls1), ClassSymbol (cls2)) => cls1.isSubClass(cls2)
290
307
case (Singleton (src1, _), Singleton (src2, _)) => src1 eq src2
291
308
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
292
322
293
323
def show (using Context ): String = this match
294
324
case ClassSymbol (cls) => i " $cls"
295
325
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 } "
296
329
297
330
private [Synthesizer ] object MirrorSource :
331
+ type WithArity = ClassSymbol | GenericTuple
298
332
299
333
/** Reduces a mirroredType to either its most specific ClassSymbol,
300
334
* or a TermRef to a singleton value. These are
@@ -332,6 +366,9 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
332
366
reduce(tp.underlying)
333
367
case tp : HKTypeLambda if tp.resultType.isInstanceOf [HKTypeLambda ] =>
334
368
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))
335
372
case tp : TypeProxy =>
336
373
reduce(tp.underlying)
337
374
case tp @ AndType (l, r) =>
@@ -354,10 +391,15 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
354
391
355
392
private def productMirror (mirroredType : Type , formal : Type , span : Span )(using Context ): TreeWithErrors =
356
393
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 ))
359
397
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)))
361
403
val (monoType, elemsType) = mirroredType match
362
404
case mirroredType : HKTypeLambda =>
363
405
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
@@ -367,12 +409,12 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
367
409
checkRefinement(formal, tpnme.MirroredElemTypes , elemsType, span)
368
410
checkRefinement(formal, tpnme.MirroredElemLabels , elemsLabels, span)
369
411
val mirrorType =
370
- mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, cls .name, formal)
412
+ mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, mirroredClass .name, formal)
371
413
.refinedWith(tpnme.MirroredElemTypes , TypeAlias (elemsType))
372
414
.refinedWith(tpnme.MirroredElemLabels , TypeAlias (elemsLabels))
373
415
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)
376
418
withNoErrors(mirrorRef.cast(mirrorType))
377
419
end makeProductMirror
378
420
@@ -389,9 +431,10 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
389
431
else
390
432
val mirrorType = mirrorCore(defn.Mirror_SingletonClass , mirroredType, mirroredType, singleton.name, formal)
391
433
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)
395
438
case Left (msg) =>
396
439
withErrors(i " type ` $mirroredType` is not a generic product because $msg" )
397
440
end productMirror
@@ -400,7 +443,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
400
443
401
444
val (acceptableMsg, cls) = MirrorSource .reduce(mirroredType) match
402
445
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 )
404
447
case Left (msg) => (msg, NoSymbol )
405
448
406
449
val clsIsGenericSum = cls.isGenericSum
@@ -457,7 +500,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
457
500
.refinedWith(tpnme.MirroredElemLabels , TypeAlias (TypeOps .nestedPairs(elemLabels)))
458
501
val mirrorRef =
459
502
if cls.useCompanionAsSumMirror then companionPath(mirroredType, span)
460
- else anonymousMirror(monoType, ExtendsSumMirror , span)
503
+ else anonymousMirror(monoType, ExtendsSumMirror , tupleArity = None , span)
461
504
withNoErrors(mirrorRef.cast(mirrorType))
462
505
else if acceptableMsg.nonEmpty then
463
506
withErrors(i " type ` $mirroredType` is not a generic sum because $acceptableMsg" )
0 commit comments