@@ -228,46 +228,9 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
228228 forwarderDef
229229 }
230230
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 ])
264232
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 {
271234 case defdef : DefDef if defdef.paramss.nonEmpty =>
272235 import dotty .tools .dotc .core .NameOps .isConstructorName
273236
@@ -277,38 +240,29 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
277240 val isCaseApply =
278241 defdef.name == nme.apply && defdef.symbol.owner.companionClass.is(CaseClass )
279242
280- val isCaseFromProduct = defdef.name == nme.fromProduct && defdef.symbol.owner.companionClass.is(CaseClass )
281-
282243 val annotated =
283244 if (isCaseCopy) defdef.symbol.owner.primaryConstructor
284245 else if (isCaseApply) defdef.symbol.owner.companionClass.primaryConstructor
285- else if (isCaseFromProduct) defdef.symbol.owner.companionClass.primaryConstructor
286246 else defdef.symbol
287247
288248 compute(annotated) match {
289249 case Nil => None
290250 case (paramClauseIndex, annotationIndices) :: Nil =>
291251 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))
312266
313267 case multiple =>
314268 report.error(" Cannot have multiple parameter lists containing `@unroll` annotation" , defdef.srcPos)
@@ -323,14 +277,12 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
323277 val generatedBody = tmpl.body.flatMap(generateSyntheticDefs(_, compute))
324278 val generatedConstr0 = generateSyntheticDefs(tmpl.constr, compute)
325279 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))
328280
329281 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)
331283 for
332284 syntheticDefs <- allGenerated
333- dcl <- syntheticDefs.extras
285+ dcl <- syntheticDefs.forwarders
334286 do
335287 val replaced = dcl.symbol
336288 byName.get(dcl.name.toString).foreach { syms =>
@@ -348,7 +300,7 @@ class UnrollDefinitions extends MacroTransform, IdentityDenotTransformer {
348300 tmpl.parents,
349301 tmpl.derived,
350302 tmpl.self,
351- otherDecls ++ allGenerated.flatMap(_.extras )
303+ tmpl.body ++ allGenerated.flatMap(_.forwarders )
352304 )
353305 }
354306
0 commit comments