@@ -257,7 +257,16 @@ object desugar {
257
257
case _ =>
258
258
rhs
259
259
260
- cpy.TypeDef (tdef)(rhs = desugarRhs(tdef.rhs))
260
+ val tdef1 = cpy.TypeDef (tdef)(rhs = desugarRhs(tdef.rhs))
261
+ if Feature .enabled(Feature .modularity)
262
+ && evidenceNames.nonEmpty
263
+ && ! evidenceNames.contains(tdef.name.toTermName)
264
+ && ! allParamss.nestedExists(_.name == tdef.name.toTermName)
265
+ then
266
+ tdef1.withAddedAnnotation:
267
+ WitnessNamesAnnot (evidenceNames.toList).withSpan(tdef.span)
268
+ else
269
+ tdef1
261
270
end desugarContextBounds
262
271
263
272
private def elimContextBounds (meth : DefDef , isPrimaryConstructor : Boolean )(using Context ): DefDef =
@@ -322,9 +331,9 @@ object desugar {
322
331
323
332
def getterParamss (n : Int ): List [ParamClause ] =
324
333
mapParamss(takeUpTo(paramssNoRHS, n)) {
325
- tparam => dropContextBounds(toDefParam(tparam, keepAnnotations = true ))
334
+ tparam => dropContextBounds(toDefParam(tparam, KeepAnnotations . All ))
326
335
} {
327
- vparam => toDefParam(vparam, keepAnnotations = true , keepDefault = false )
336
+ vparam => toDefParam(vparam, KeepAnnotations . All , keepDefault = false )
328
337
}
329
338
330
339
def defaultGetters (paramss : List [ParamClause ], n : Int ): List [DefDef ] = paramss match
@@ -429,7 +438,12 @@ object desugar {
429
438
private def addEvidenceParams (meth : DefDef , params : List [ValDef ])(using Context ): DefDef =
430
439
if params.isEmpty then return meth
431
440
432
- val boundNames = params.map(_.name).toSet
441
+ var boundNames = params.map(_.name).toSet
442
+ for mparams <- meth.paramss; mparam <- mparams do
443
+ mparam match
444
+ case tparam : TypeDef if tparam.mods.annotations.exists(WitnessNamesAnnot .unapply(_).isDefined) =>
445
+ boundNames += tparam.name.toTermName
446
+ case _ =>
433
447
434
448
// println(i"add ev params ${meth.name}, ${boundNames.toList}")
435
449
@@ -462,16 +476,26 @@ object desugar {
462
476
463
477
@ sharable private val synthetic = Modifiers (Synthetic )
464
478
479
+ /** Which annotations to keep in derived parameters */
480
+ private enum KeepAnnotations :
481
+ case None , All , WitnessOnly
482
+
465
483
/** Filter annotations in `mods` according to `keep` */
466
- private def filterAnnots (mods : Modifiers , keep : Boolean )(using Context ) =
467
- if keep then mods else mods.withAnnotations(Nil )
484
+ private def filterAnnots (mods : Modifiers , keep : KeepAnnotations )(using Context ) = keep match
485
+ case KeepAnnotations .None => mods.withAnnotations(Nil )
486
+ case KeepAnnotations .All => mods
487
+ case KeepAnnotations .WitnessOnly =>
488
+ mods.withAnnotations:
489
+ mods.annotations.filter:
490
+ case WitnessNamesAnnot (_) => true
491
+ case _ => false
468
492
469
- private def toDefParam (tparam : TypeDef , keepAnnotations : Boolean )(using Context ): TypeDef =
470
- val mods = filterAnnots(tparam.rawMods, keepAnnotations )
493
+ private def toDefParam (tparam : TypeDef , keep : KeepAnnotations )(using Context ): TypeDef =
494
+ val mods = filterAnnots(tparam.rawMods, keep )
471
495
tparam.withMods(mods & EmptyFlags | Param )
472
496
473
- private def toDefParam (vparam : ValDef , keepAnnotations : Boolean , keepDefault : Boolean )(using Context ): ValDef = {
474
- val mods = filterAnnots(vparam.rawMods, keepAnnotations )
497
+ private def toDefParam (vparam : ValDef , keep : KeepAnnotations , keepDefault : Boolean )(using Context ): ValDef = {
498
+ val mods = filterAnnots(vparam.rawMods, keep )
475
499
val hasDefault = if keepDefault then HasDefault else EmptyFlags
476
500
// Need to ensure that tree is duplicated since term parameters can be watched
477
501
// and cloning a term parameter will copy its watchers to the clone, which means
@@ -572,7 +596,7 @@ object desugar {
572
596
// Annotations on class _type_ parameters are set on the derived parameters
573
597
// but not on the constructor parameters. The reverse is true for
574
598
// annotations on class _value_ parameters.
575
- val constrTparams = impliedTparams.map(toDefParam(_, keepAnnotations = false ))
599
+ val constrTparams = impliedTparams.map(toDefParam(_, KeepAnnotations . WitnessOnly ))
576
600
val constrVparamss =
577
601
if (originalVparamss.isEmpty) { // ensure parameter list is non-empty
578
602
if (isCaseClass)
@@ -583,7 +607,7 @@ object desugar {
583
607
report.error(CaseClassMissingNonImplicitParamList (cdef), namePos)
584
608
ListOfNil
585
609
}
586
- else originalVparamss.nestedMap(toDefParam(_, keepAnnotations = true , keepDefault = true ))
610
+ else originalVparamss.nestedMap(toDefParam(_, KeepAnnotations . All , keepDefault = true ))
587
611
val derivedTparams =
588
612
constrTparams.zipWithConserve(impliedTparams)((tparam, impliedParam) =>
589
613
derivedTypeParam(tparam).withAnnotations(impliedParam.mods.annotations))
@@ -605,7 +629,7 @@ object desugar {
605
629
defDef(
606
630
addEvidenceParams(
607
631
cpy.DefDef (ddef)(paramss = joinParams(constrTparams, ddef.paramss)),
608
- evidenceParams(constr1).map(toDefParam(_, keepAnnotations = false , keepDefault = false )))))
632
+ evidenceParams(constr1).map(toDefParam(_, KeepAnnotations . None , keepDefault = false )))))
609
633
case stat =>
610
634
stat
611
635
}
0 commit comments