@@ -228,46 +228,9 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
228
228
forwarderDef
229
229
}
230
230
231
- private def generateFromProduct (startParamIndices : List [Int ], paramCount : Int , defdef : DefDef )(using Context ) = {
232
- cpy.DefDef (defdef)(
233
- name = defdef.name,
234
- paramss = defdef.paramss,
235
- tpt = defdef.tpt,
236
- rhs = Match (
237
- ref(defdef.paramss.head.head.asInstanceOf [ValDef ].symbol).select(termName(" productArity" )),
238
- startParamIndices.map { paramIndex =>
239
- val Apply (select, args) = defdef.rhs: @ unchecked
240
- CaseDef (
241
- Literal (Constant (paramIndex)),
242
- EmptyTree ,
243
- Apply (
244
- select,
245
- args.take(paramIndex) ++
246
- Range (paramIndex, paramCount).map(n =>
247
- ref(defdef.symbol.owner.companionModule)
248
- .select(DefaultGetterName (defdef.symbol.owner.primaryConstructor.name.toTermName, n))
249
- )
250
- )
251
- )
252
- } :+ CaseDef (
253
- Underscore (defn.IntType ),
254
- EmptyTree ,
255
- defdef.rhs
256
- )
257
- )
258
- ).setDefTree
259
- }
260
-
261
- private enum Gen :
262
- case Substitute (origin : Symbol , newDef : DefDef )
263
- case Forwarders (origin : Symbol , forwarders : List [DefDef ])
231
+ case class Forwarders (origin : Symbol , forwarders : List [DefDef ])
264
232
265
- def origin : Symbol
266
- def extras : List [DefDef ] = this match
267
- case Substitute (_, d) => d :: Nil
268
- case Forwarders (_, ds) => ds
269
-
270
- private def generateSyntheticDefs (tree : Tree , compute : ComputeIndices )(using Context ): Option [Gen ] = tree match {
233
+ private def generateSyntheticDefs (tree : Tree , compute : ComputeIndices )(using Context ): Option [Forwarders ] = tree match {
271
234
case defdef : DefDef if defdef.paramss.nonEmpty =>
272
235
import dotty .tools .dotc .core .NameOps .isConstructorName
273
236
@@ -277,38 +240,29 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
277
240
val isCaseApply =
278
241
defdef.name == nme.apply && defdef.symbol.owner.companionClass.is(CaseClass )
279
242
280
- val isCaseFromProduct = defdef.name == nme.fromProduct && defdef.symbol.owner.companionClass.is(CaseClass )
281
-
282
243
val annotated =
283
244
if (isCaseCopy) defdef.symbol.owner.primaryConstructor
284
245
else if (isCaseApply) defdef.symbol.owner.companionClass.primaryConstructor
285
- else if (isCaseFromProduct) defdef.symbol.owner.companionClass.primaryConstructor
286
246
else defdef.symbol
287
247
288
248
compute(annotated) match {
289
249
case Nil => None
290
250
case (paramClauseIndex, annotationIndices) :: Nil =>
291
251
val paramCount = annotated.paramSymss(paramClauseIndex).size
292
- if isCaseFromProduct then
293
- Some (Gen .Substitute (
294
- origin = defdef.symbol,
295
- newDef = generateFromProduct(annotationIndices, paramCount, defdef)
296
- ))
297
- else
298
- val generatedDefs =
299
- val indices = (annotationIndices :+ paramCount).sliding(2 ).toList.reverse
300
- indices.foldLeft(List .empty[DefDef ]):
301
- case (defdefs, paramIndex :: nextParamIndex :: Nil ) =>
302
- generateSingleForwarder(
303
- defdef,
304
- paramIndex,
305
- paramCount,
306
- nextParamIndex,
307
- paramClauseIndex,
308
- isCaseApply
309
- ) :: defdefs
310
- case _ => unreachable(" sliding with at least 2 elements" )
311
- Some (Gen .Forwarders (origin = defdef.symbol, forwarders = generatedDefs))
252
+ val generatedDefs =
253
+ val indices = (annotationIndices :+ paramCount).sliding(2 ).toList.reverse
254
+ indices.foldLeft(List .empty[DefDef ]):
255
+ case (defdefs, paramIndex :: nextParamIndex :: Nil ) =>
256
+ generateSingleForwarder(
257
+ defdef,
258
+ paramIndex,
259
+ paramCount,
260
+ nextParamIndex,
261
+ paramClauseIndex,
262
+ isCaseApply
263
+ ) :: defdefs
264
+ case _ => unreachable(" sliding with at least 2 elements" )
265
+ Some (Forwarders (origin = defdef.symbol, forwarders = generatedDefs))
312
266
313
267
case multiple =>
314
268
report.error(" Cannot have multiple parameter lists containing `@unroll` annotation" , defdef.srcPos)
@@ -323,14 +277,12 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
323
277
val generatedBody = tmpl.body.flatMap(generateSyntheticDefs(_, compute))
324
278
val generatedConstr0 = generateSyntheticDefs(tmpl.constr, compute)
325
279
val allGenerated = generatedBody ++ generatedConstr0
326
- val bodySubs = generatedBody.collect({ case s : Gen .Substitute => s.origin }).toSet
327
- val otherDecls = tmpl.body.filterNot(d => d.symbol.exists && bodySubs(d.symbol))
328
280
329
281
if allGenerated.nonEmpty then
330
- val byName = (tmpl.constr :: otherDecls ).groupMap(_.symbol.name.toString)(_.symbol)
282
+ val byName = (tmpl.constr :: tmpl.body ).groupMap(_.symbol.name.toString)(_.symbol)
331
283
for
332
284
syntheticDefs <- allGenerated
333
- dcl <- syntheticDefs.extras
285
+ dcl <- syntheticDefs.forwarders
334
286
do
335
287
val replaced = dcl.symbol
336
288
byName.get(dcl.name.toString).foreach { syms =>
@@ -348,7 +300,7 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
348
300
tmpl.parents,
349
301
tmpl.derived,
350
302
tmpl.self,
351
- otherDecls ++ allGenerated.flatMap(_.extras )
303
+ tmpl.body ++ allGenerated.flatMap(_.forwarders )
352
304
)
353
305
}
354
306
0 commit comments