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