@@ -227,30 +227,41 @@ object desugar {
227
227
addDefaultGetters(elimContextBounds(meth, isPrimaryConstructor))
228
228
229
229
private def desugarContextBounds (
230
- tname : TypeName , rhs : Tree ,
230
+ tdef : TypeDef ,
231
231
evidenceBuf : ListBuffer [ValDef ],
232
232
flags : FlagSet ,
233
- freshName : => TermName )(using Context ): Tree = rhs match
234
- case ContextBounds (tbounds, cxbounds) =>
235
- val isMember = flags.isAllOf(DeferredGivenFlags )
236
- for bound <- cxbounds do
237
- val evidenceName = bound match
238
- case ContextBoundTypeTree (_, _, ownName) if ! ownName.isEmpty =>
239
- ownName
240
- case _ if ! isMember && cxbounds.tail.isEmpty && Feature .enabled(Feature .modularity) =>
241
- tname.toTermName
242
- case _ =>
243
- if isMember then inventGivenOrExtensionName(bound)
244
- else freshName
245
- val evidenceParam = ValDef (evidenceName, bound, EmptyTree ).withFlags(flags)
246
- evidenceParam.pushAttachment(ContextBoundParam , ())
247
- evidenceBuf += evidenceParam
248
- tbounds
249
- case LambdaTypeTree (tparams, body) =>
250
- cpy.LambdaTypeTree (rhs)(tparams,
251
- desugarContextBounds(tname, body, evidenceBuf, flags, freshName))
252
- case _ =>
253
- rhs
233
+ freshName : => TermName )(using Context ): TypeDef =
234
+
235
+ val evidenceNames = ListBuffer [TermName ]()
236
+
237
+ def desugarRhs (rhs : Tree ): Tree = rhs match
238
+ case ContextBounds (tbounds, cxbounds) =>
239
+ val isMember = flags.isAllOf(DeferredGivenFlags )
240
+ for bound <- cxbounds do
241
+ val evidenceName = bound match
242
+ case ContextBoundTypeTree (_, _, ownName) if ! ownName.isEmpty =>
243
+ ownName
244
+ case _ if ! isMember && cxbounds.tail.isEmpty && Feature .enabled(Feature .modularity) =>
245
+ tdef.name.toTermName
246
+ case _ =>
247
+ if isMember then inventGivenOrExtensionName(bound)
248
+ else freshName
249
+ evidenceNames += evidenceName
250
+ val evidenceParam = ValDef (evidenceName, bound, EmptyTree ).withFlags(flags)
251
+ evidenceParam.pushAttachment(ContextBoundParam , ())
252
+ evidenceBuf += evidenceParam
253
+ tbounds
254
+ case LambdaTypeTree (tparams, body) =>
255
+ cpy.LambdaTypeTree (rhs)(tparams, desugarRhs(body))
256
+ case _ =>
257
+ rhs
258
+
259
+ val tdef1 = cpy.TypeDef (tdef)(rhs = desugarRhs(tdef.rhs))
260
+ if evidenceNames.nonEmpty && ! evidenceNames.contains(tdef.name.toTermName) then
261
+ val witnessNamesAnnot = WitnessNamesAnnot (evidenceNames.toList).withSpan(tdef.span)
262
+ tdef1.withAddedAnnotation(witnessNamesAnnot)
263
+ else
264
+ tdef1
254
265
end desugarContextBounds
255
266
256
267
private def elimContextBounds (meth : DefDef , isPrimaryConstructor : Boolean )(using Context ): DefDef =
@@ -269,8 +280,7 @@ object desugar {
269
280
val iflag = if Feature .sourceVersion.isAtLeast(`future`) then Given else Implicit
270
281
val flags = if isPrimaryConstructor then iflag | LocalParamAccessor else iflag | Param
271
282
mapParamss(paramss) {
272
- tparam => cpy.TypeDef (tparam)(rhs =
273
- desugarContextBounds(tparam.name, tparam.rhs, evidenceParamBuf, flags, freshName))
283
+ tparam => desugarContextBounds(tparam, evidenceParamBuf, flags, freshName)
274
284
}(identity)
275
285
276
286
rhs match
@@ -485,9 +495,8 @@ object desugar {
485
495
486
496
def typeDef (tdef : TypeDef )(using Context ): Tree =
487
497
val evidenceBuf = new ListBuffer [ValDef ]
488
- val result = cpy.TypeDef (tdef)(rhs =
489
- desugarContextBounds(tdef.name, tdef.rhs, evidenceBuf,
490
- (tdef.mods.flags.toTermFlags & AccessFlags ) | Lazy | DeferredGivenFlags , EmptyTermName ))
498
+ val result = desugarContextBounds(tdef, evidenceBuf,
499
+ (tdef.mods.flags.toTermFlags & AccessFlags ) | Lazy | DeferredGivenFlags , EmptyTermName )
491
500
if evidenceBuf.isEmpty then result else Thicket (result :: evidenceBuf.toList)
492
501
493
502
/** The expansion of a class definition. See inline comments for what is involved */
0 commit comments