@@ -223,19 +223,16 @@ 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 ], tupleArity : Option [ Int ], span : Span )(using Context ) =
226
+ private def anonymousMirror (monoType : Type , attachment : Property .StickyKey [Unit ], 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
- var newImpl = untpd.Template (
229
+ val newImpl = untpd.Template (
230
230
constr = untpd.emptyConstructor,
231
231
parents = untpd.TypeTree (defn.ObjectType ) :: untpd.TypeTree (defn.JavaSerializableClass .typeRef) :: 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
- }
239
236
typer.typed(untpd.New (newImpl).withSpan(span))
240
237
241
238
/** The mirror type
@@ -385,12 +382,17 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
385
382
386
383
private def productMirror (mirroredType : Type , formal : Type , span : Span )(using Context ): TreeWithErrors =
387
384
385
+ /** `new scala.runtime.TupleMirror(arity)`
386
+ * using TupleMirror avoids generating anonymous classes for tuple mirrors.
387
+ */
388
+ def newTupleMirror (arity : Int ): Tree =
389
+ New (defn.RuntimeTupleMirrorTypeRef , Literal (Constant (arity)) :: Nil )
390
+
388
391
def makeProductMirror (cls : Symbol , tps : Option [List [Type ]]): TreeWithErrors =
389
392
val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal ))
390
393
val elemLabels = accessors.map(acc => ConstantType (Constant (acc.name.toString)))
391
- val nestedPairs =
392
- val elems = tps.getOrElse(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
393
- TypeOps .nestedPairs(elems)
394
+ val typeElems = tps.getOrElse(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
395
+ val nestedPairs = TypeOps .nestedPairs(typeElems)
394
396
val (monoType, elemsType) = mirroredType match
395
397
case mirroredType : HKTypeLambda =>
396
398
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
@@ -406,7 +408,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
406
408
}
407
409
val mirrorRef =
408
410
if cls.useCompanionAsProductMirror then companionPath(mirroredType, span)
409
- else anonymousMirror(monoType, ExtendsProductMirror , tps.map(_.size), span)
411
+ else if defn.isTupleClass(cls) then newTupleMirror(typeElems.size) // TODO: cls == defn.PairClass when > 22
412
+ else anonymousMirror(monoType, ExtendsProductMirror , span)
410
413
withNoErrors(mirrorRef.cast(mirrorType))
411
414
end makeProductMirror
412
415
@@ -508,7 +511,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
508
511
}
509
512
val mirrorRef =
510
513
if cls.useCompanionAsSumMirror then companionPath(mirroredType, span)
511
- else anonymousMirror(monoType, ExtendsSumMirror , None , span)
514
+ else anonymousMirror(monoType, ExtendsSumMirror , span)
512
515
withNoErrors(mirrorRef.cast(mirrorType))
513
516
else if acceptableMsg.nonEmpty then
514
517
withErrors(i " type ` $mirroredType` is not a generic sum because $acceptableMsg" )
0 commit comments