@@ -240,11 +240,11 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
240
240
* <parent> {
241
241
* MirroredMonoType = <monoType>
242
242
* MirroredType = <mirroredType>
243
- * MirroredLabel = <label> }
243
+ * MirroredLabel = <label>
244
244
* }
245
245
*/
246
- private def mirrorCore (parentClass : ClassSymbol , monoType : Type , mirroredType : Type , label : Name , formal : Type )(using Context ) =
247
- formal & parentClass.typeRef
246
+ private def mirrorCore (parentClass : ClassSymbol , monoType : Type , mirroredType : Type , label : Name )(using Context ) =
247
+ parentClass.typeRef
248
248
.refinedWith(tpnme.MirroredMonoType , TypeAlias (monoType))
249
249
.refinedWith(tpnme.MirroredType , TypeAlias (mirroredType))
250
250
.refinedWith(tpnme.MirroredLabel , TypeAlias (ConstantType (Constant (label.toString))))
@@ -269,6 +269,13 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
269
269
then report.error(
270
270
em " $name mismatch, expected: $expected, found: $actual. " , ctx.source.atSpan(span))
271
271
272
+ extension (formal : Type )
273
+ /** `tp := op; tp <:< formal; formal & tp` */
274
+ private def constrained_& (op : Context ?=> Type )(using Context ): Type =
275
+ val tp = op
276
+ tp <:< formal
277
+ formal & tp
278
+
272
279
private def mkMirroredMonoType (mirroredType : HKTypeLambda )(using Context ): Type =
273
280
val monoMap = new TypeMap :
274
281
def apply (t : Type ) = t match
@@ -366,10 +373,11 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
366
373
val elemsLabels = TypeOps .nestedPairs(elemLabels)
367
374
checkRefinement(formal, tpnme.MirroredElemTypes , elemsType, span)
368
375
checkRefinement(formal, tpnme.MirroredElemLabels , elemsLabels, span)
369
- val mirrorType =
370
- mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, cls.name, formal )
376
+ val mirrorType = formal.constrained_& {
377
+ mirrorCore(defn.Mirror_ProductClass , monoType, mirroredType, cls.name)
371
378
.refinedWith(tpnme.MirroredElemTypes , TypeAlias (elemsType))
372
379
.refinedWith(tpnme.MirroredElemLabels , TypeAlias (elemsLabels))
380
+ }
373
381
val mirrorRef =
374
382
if cls.useCompanionAsProductMirror then companionPath(mirroredType, span)
375
383
else anonymousMirror(monoType, ExtendsProductMirror , span)
@@ -382,12 +390,15 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
382
390
val singleton = tref.termSymbol // prefer alias name over the orignal name
383
391
val singletonPath = pathFor(tref).withSpan(span)
384
392
if tref.classSymbol.is(Scala2x ) then // could be Scala 3 alias of Scala 2 case object.
385
- val mirrorType =
386
- mirrorCore(defn.Mirror_SingletonProxyClass , mirroredType, mirroredType, singleton.name, formal)
393
+ val mirrorType = formal.constrained_& {
394
+ mirrorCore(defn.Mirror_SingletonProxyClass , mirroredType, mirroredType, singleton.name)
395
+ }
387
396
val mirrorRef = New (defn.Mirror_SingletonProxyClass .typeRef, singletonPath :: Nil )
388
397
withNoErrors(mirrorRef.cast(mirrorType))
389
398
else
390
- val mirrorType = mirrorCore(defn.Mirror_SingletonClass , mirroredType, mirroredType, singleton.name, formal)
399
+ val mirrorType = formal.constrained_& {
400
+ mirrorCore(defn.Mirror_SingletonClass , mirroredType, mirroredType, singleton.name)
401
+ }
391
402
withNoErrors(singletonPath.cast(mirrorType))
392
403
case MirrorSource .ClassSymbol (cls) =>
393
404
if cls.isGenericProduct then makeProductMirror(cls)
@@ -452,9 +463,12 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
452
463
(mirroredType, elems)
453
464
454
465
val mirrorType =
455
- mirrorCore(defn.Mirror_SumClass , monoType, mirroredType, cls.name, formal)
466
+ val labels = TypeOps .nestedPairs(elemLabels)
467
+ formal.constrained_& {
468
+ mirrorCore(defn.Mirror_SumClass , monoType, mirroredType, cls.name)
456
469
.refinedWith(tpnme.MirroredElemTypes , TypeAlias (elemsType))
457
- .refinedWith(tpnme.MirroredElemLabels , TypeAlias (TypeOps .nestedPairs(elemLabels)))
470
+ .refinedWith(tpnme.MirroredElemLabels , TypeAlias (labels))
471
+ }
458
472
val mirrorRef =
459
473
if cls.useCompanionAsSumMirror then companionPath(mirroredType, span)
460
474
else anonymousMirror(monoType, ExtendsSumMirror , span)
@@ -463,7 +477,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
463
477
withErrors(i " type ` $mirroredType` is not a generic sum because $acceptableMsg" )
464
478
else if ! clsIsGenericSum then
465
479
withErrors(i " $cls is not a generic sum because ${cls.whyNotGenericSum}" )
466
- else
480
+ else
467
481
EmptyTreeNoError
468
482
end sumMirror
469
483
0 commit comments