@@ -22,6 +22,7 @@ import parsing.Parsers
22
22
23
23
import scala .annotation .internal .sharable
24
24
import scala .annotation .threadUnsafe
25
+ import dotty .tools .dotc .quoted .QuoteUtils .treeOwner
25
26
26
27
object desugar {
27
28
import untpd .*
@@ -52,8 +53,12 @@ object desugar {
52
53
*/
53
54
val ContextBoundParam : Property .Key [Unit ] = Property .StickyKey ()
54
55
55
- /** An attachment key to indicate that a DefDef is a poly function apply
56
- * method definition.
56
+ /** When first desugaring a PolyFunction, this attachment is added to the
57
+ * PolyFunction `apply` method with an empty list value.
58
+ *
59
+ * Afterwards, the attachment is added to poly function type trees, with the
60
+ * list of their context bounds.
61
+ * //TODO(kπ) see if it has to be updated
57
62
*/
58
63
val PolyFunctionApply : Property .Key [List [ValDef ]] = Property .StickyKey ()
59
64
@@ -497,9 +502,9 @@ object desugar {
497
502
case Ident (name : TermName ) => boundNames.contains(name)
498
503
case _ => false
499
504
500
- def recur (mparamss : List [ParamClause ]): List [ParamClause ] = mparamss match
505
+ def recur (mparamss : List [ParamClause ]): ( List [ParamClause ], List [ ParamClause ]) = mparamss match
501
506
case ValDefs (mparams) :: _ if mparams.exists(referencesBoundName) =>
502
- params :: mparamss
507
+ ( params :: Nil ) -> mparamss
503
508
case ValDefs (mparams @ (mparam :: _)) :: Nil if mparam.mods.isOneOf(GivenOrImplicit ) =>
504
509
val normParams =
505
510
if params.head.mods.flags.is(Given ) != mparam.mods.flags.is(Given ) then
@@ -508,30 +513,71 @@ object desugar {
508
513
param.withMods(param.mods.withFlags(normFlags))
509
514
.showing(i " adapted param $result ${result.mods.flags} for ${meth.name}" , Printers .desugar)
510
515
else params
511
- (normParams ++ mparams) :: Nil
516
+ (( normParams ++ mparams) :: Nil ) -> Nil
512
517
case mparams :: mparamss1 =>
513
- mparams :: recur(mparamss1)
518
+ val (fst, snd) = recur(mparamss1)
519
+ (mparams :: fst) -> snd
514
520
case Nil =>
515
- params :: Nil
516
-
517
- def pushDownEvidenceParams (tree : Tree ): Tree = tree match
518
- case Function (params, body) =>
519
- cpy.Function (tree)(params, pushDownEvidenceParams(body))
520
- case Block (stats, expr) =>
521
- cpy.Block (tree)(stats, pushDownEvidenceParams(expr))
522
- case tree =>
521
+ Nil -> (params :: Nil )
522
+
523
+ // def pushDownEvidenceParams(tree: Tree): Tree = tree match
524
+ // case Function(mparams, body) if mparams.collect { case v: ValDef => v }.exists(referencesBoundName) =>
525
+ // ctxFunctionWithParams(tree)
526
+ // case Function(mparams, body) =>
527
+ // cpy.Function(tree)(mparams, pushDownEvidenceParams(body))
528
+ // case Block(stats, expr) =>
529
+ // cpy.Block(tree)(stats, pushDownEvidenceParams(expr))
530
+ // case tree =>
531
+ // ctxFunctionWithParams(tree)
532
+
533
+ // def ctxFunctionWithParams(tree: Tree): Tree =
534
+ // val paramTpts = params.map(_.tpt)
535
+ // val paramNames = params.map(_.name)
536
+ // val paramsErased = params.map(_.mods.flags.is(Erased))
537
+ // Function(params, tree).withSpan(tree.span).withAttachmentsFrom(tree)
538
+
539
+ def functionsOf (paramss : List [ParamClause ], rhs : Tree ): Tree = paramss match
540
+ case Nil => rhs
541
+ case ValDefs (head @ (fst :: _)) :: rest if fst.mods.isOneOf(GivenOrImplicit ) =>
523
542
val paramTpts = params.map(_.tpt)
524
543
val paramNames = params.map(_.name)
525
544
val paramsErased = params.map(_.mods.flags.is(Erased ))
526
- makeContextualFunction(paramTpts, paramNames, tree, paramsErased).withSpan(tree.span)
545
+ makeContextualFunction(paramTpts, paramNames, functionsOf(rest, rhs), paramsErased).withSpan(rhs.span)
546
+ case head :: rest =>
547
+ Function (head, functionsOf(rest, rhs))
527
548
528
549
if meth.hasAttachment(PolyFunctionApply ) then
529
- if ctx.mode.is(Mode .Type ) then
530
- cpy.DefDef (meth)(tpt = meth.tpt.withAttachment(PolyFunctionApply , params))
531
- else
532
- cpy.DefDef (meth)(rhs = pushDownEvidenceParams(meth.rhs))
550
+ println(i " ${recur(meth.paramss)}" )
551
+ recur(meth.paramss) match
552
+ case (paramsFst, Nil ) =>
553
+ cpy.DefDef (meth)(paramss = paramsFst)
554
+ case (paramsFst, paramsSnd) =>
555
+ if ctx.mode.is(Mode .Type ) then
556
+ cpy.DefDef (meth)(paramss = paramsFst, tpt = functionsOf(paramsSnd, meth.tpt))
557
+ else
558
+ cpy.DefDef (meth)(paramss = paramsFst, rhs = functionsOf(paramsSnd, meth.rhs))
559
+
560
+ // if ctx.mode.is(Mode.Type) then
561
+ // meth.removeAttachment(PolyFunctionApply)
562
+ // // should be kept on meth to see the current param types?
563
+ // meth.tpt.putAttachment(PolyFunctionApply, params)
564
+ // val newParamss = recur(meth.paramss)
565
+ // println(i"added PolyFunctionApply to ${meth.name}.tpt: ${meth.tpt} with $params")
566
+ // println(i"new paramss: $newParamss")
567
+ // meth
568
+ // else
569
+ // val newParamss = recur(meth.paramss)
570
+ // println(i"added PolyFunctionApply to ${meth.name} with $params")
571
+ // println(i"new paramss: $newParamss")
572
+ // val DefDef(_, mparamss, _ , _) = meth: @unchecked
573
+ // val tparams :: ValDefs(vparams) :: Nil = mparamss: @unchecked
574
+ // if vparams.exists(referencesBoundName) then
575
+ // cpy.DefDef(meth)(paramss = tparams :: params :: Nil, rhs = Function(vparams, meth.rhs))
576
+ // else
577
+ // cpy.DefDef(meth)(rhs = pushDownEvidenceParams(meth.rhs))
533
578
else
534
- cpy.DefDef (meth)(paramss = recur(meth.paramss))
579
+ val (paramsFst, paramsSnd) = recur(meth.paramss)
580
+ cpy.DefDef (meth)(paramss = paramsFst ++ paramsSnd)
535
581
end addEvidenceParams
536
582
537
583
/** The parameters generated from the contextual bounds of `meth`, as generated by `desugar.defDef` */
@@ -1265,17 +1311,20 @@ object desugar {
1265
1311
case ((p, paramFlags), n) => makeSyntheticParameter(n + 1 , p).withAddedFlags(paramFlags)
1266
1312
}.toList
1267
1313
1314
+ vparams.foreach(p => println(i " $p, ${p.mods.flags.flagsString}" ))
1268
1315
RefinedTypeTree (ref(defn.PolyFunctionType ), List (
1269
1316
DefDef (nme.apply, tparams :: vparams :: Nil , res, EmptyTree )
1270
1317
.withFlags(Synthetic )
1271
1318
.withAttachment(PolyFunctionApply , List .empty)
1272
1319
)).withSpan(tree.span)
1320
+ .withAttachment(PolyFunctionApply , tree.attachmentOrElse(PolyFunctionApply , List .empty))
1273
1321
case PolyFunction (tparams : List [untpd.TypeDef ] @ unchecked, res) =>
1274
1322
RefinedTypeTree (ref(defn.PolyFunctionType ), List (
1275
1323
DefDef (nme.apply, tparams :: Nil , res, EmptyTree )
1276
1324
.withFlags(Synthetic )
1277
1325
.withAttachment(PolyFunctionApply , List .empty)
1278
1326
)).withSpan(tree.span)
1327
+ .withAttachment(PolyFunctionApply , tree.attachmentOrElse(PolyFunctionApply , List .empty))
1279
1328
end makePolyFunctionType
1280
1329
1281
1330
/** Invent a name for an anonympus given of type or template `impl`. */
0 commit comments