@@ -22,6 +22,7 @@ import parsing.Parsers
2222
2323import scala .annotation .internal .sharable
2424import scala .annotation .threadUnsafe
25+ import dotty .tools .dotc .quoted .QuoteUtils .treeOwner
2526
2627object desugar {
2728 import untpd .*
@@ -52,8 +53,12 @@ object desugar {
5253 */
5354 val ContextBoundParam : Property .Key [Unit ] = Property .StickyKey ()
5455
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
5762 */
5863 val PolyFunctionApply : Property .Key [List [ValDef ]] = Property .StickyKey ()
5964
@@ -497,9 +502,9 @@ object desugar {
497502 case Ident (name : TermName ) => boundNames.contains(name)
498503 case _ => false
499504
500- def recur (mparamss : List [ParamClause ]): List [ParamClause ] = mparamss match
505+ def recur (mparamss : List [ParamClause ]): ( List [ParamClause ], List [ ParamClause ]) = mparamss match
501506 case ValDefs (mparams) :: _ if mparams.exists(referencesBoundName) =>
502- params :: mparamss
507+ ( params :: Nil ) -> mparamss
503508 case ValDefs (mparams @ (mparam :: _)) :: Nil if mparam.mods.isOneOf(GivenOrImplicit ) =>
504509 val normParams =
505510 if params.head.mods.flags.is(Given ) != mparam.mods.flags.is(Given ) then
@@ -508,30 +513,71 @@ object desugar {
508513 param.withMods(param.mods.withFlags(normFlags))
509514 .showing(i " adapted param $result ${result.mods.flags} for ${meth.name}" , Printers .desugar)
510515 else params
511- (normParams ++ mparams) :: Nil
516+ (( normParams ++ mparams) :: Nil ) -> Nil
512517 case mparams :: mparamss1 =>
513- mparams :: recur(mparamss1)
518+ val (fst, snd) = recur(mparamss1)
519+ (mparams :: fst) -> snd
514520 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 ) =>
523542 val paramTpts = params.map(_.tpt)
524543 val paramNames = params.map(_.name)
525544 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))
527548
528549 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))
533578 else
534- cpy.DefDef (meth)(paramss = recur(meth.paramss))
579+ val (paramsFst, paramsSnd) = recur(meth.paramss)
580+ cpy.DefDef (meth)(paramss = paramsFst ++ paramsSnd)
535581 end addEvidenceParams
536582
537583 /** The parameters generated from the contextual bounds of `meth`, as generated by `desugar.defDef` */
@@ -1265,17 +1311,20 @@ object desugar {
12651311 case ((p, paramFlags), n) => makeSyntheticParameter(n + 1 , p).withAddedFlags(paramFlags)
12661312 }.toList
12671313
1314+ vparams.foreach(p => println(i " $p, ${p.mods.flags.flagsString}" ))
12681315 RefinedTypeTree (ref(defn.PolyFunctionType ), List (
12691316 DefDef (nme.apply, tparams :: vparams :: Nil , res, EmptyTree )
12701317 .withFlags(Synthetic )
12711318 .withAttachment(PolyFunctionApply , List .empty)
12721319 )).withSpan(tree.span)
1320+ .withAttachment(PolyFunctionApply , tree.attachmentOrElse(PolyFunctionApply , List .empty))
12731321 case PolyFunction (tparams : List [untpd.TypeDef ] @ unchecked, res) =>
12741322 RefinedTypeTree (ref(defn.PolyFunctionType ), List (
12751323 DefDef (nme.apply, tparams :: Nil , res, EmptyTree )
12761324 .withFlags(Synthetic )
12771325 .withAttachment(PolyFunctionApply , List .empty)
12781326 )).withSpan(tree.span)
1327+ .withAttachment(PolyFunctionApply , tree.attachmentOrElse(PolyFunctionApply , List .empty))
12791328 end makePolyFunctionType
12801329
12811330 /** Invent a name for an anonympus given of type or template `impl`. */
0 commit comments