Skip to content

Commit 952eff7

Browse files
committed
Cleanup context bounds for poly functions implementation after review
1 parent f9db9fa commit 952eff7

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+36-14
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,13 @@ object desugar {
350350
val Function(vparams: List[untpd.ValDef] @unchecked, rhs) = fun: @unchecked
351351
val newParamss = paramssNoContextBounds(tparams :: vparams :: Nil)
352352
val params = evidenceParamBuf.toList
353-
if params.isEmpty then return meth
354-
val boundNames = getBoundNames(params, newParamss)
355-
val recur = fitEvidenceParams(params, nme.apply, boundNames)
356-
val (paramsFst, paramsSnd) = recur(newParamss)
357-
functionsOf((paramsFst ++ paramsSnd).filter(_.nonEmpty), rhs)
353+
if params.isEmpty then
354+
meth
355+
else
356+
val boundNames = getBoundNames(params, newParamss)
357+
val recur = fitEvidenceParams(params, nme.apply, boundNames)
358+
val (paramsFst, paramsSnd) = recur(newParamss)
359+
functionsOf((paramsFst ++ paramsSnd).filter(_.nonEmpty), rhs)
358360
end elimContextBounds
359361

360362
def addDefaultGetters(meth: DefDef)(using Context): Tree =
@@ -487,8 +489,27 @@ object desugar {
487489
case Ident(name: TermName) => names.contains(name)
488490
case _ => false
489491

490-
/** Fit evidence `params` into the `mparamss` parameter lists */
491-
private def fitEvidenceParams(params: List[ValDef], methName: Name, boundNames: Set[TermName])(mparamss: List[ParamClause])(using Context): (List[ParamClause], List[ParamClause]) = mparamss match
492+
/** Fit evidence `params` into the `mparamss` parameter lists, making sure
493+
* that all parameters referencing `params` are after them.
494+
* - for methods the final parameter lists are := result._1 ++ result._2
495+
* - for poly functions, each element of the pair contains at most one term
496+
* parameter list
497+
*
498+
* @param params the evidence parameters list that should fit into `mparamss`
499+
* @param methName the name of the method that `mparamss` belongs to
500+
* @param boundNames the names of the evidence parameters
501+
* @param mparamss the original parameter lists of the method
502+
* @return a pair of parameter lists containing all parameter lists in a
503+
* reference-correct order; make sure that `params` is always at the
504+
* intersection of the pair elements; this is relevant, for poly functions
505+
* where `mparamss` is guaranteed to have exectly one term parameter list,
506+
* then each pair element will have at most one term parameter list
507+
*/
508+
private def fitEvidenceParams(
509+
params: List[ValDef],
510+
methName: Name,
511+
boundNames: Set[TermName]
512+
)(mparamss: List[ParamClause])(using Context): (List[ParamClause], List[ParamClause]) = mparamss match
492513
case ValDefs(mparams) :: _ if mparams.exists(referencesName(_, boundNames)) =>
493514
(params :: Nil) -> mparamss
494515
case ValDefs(mparams @ (mparam :: _)) :: Nil if mparam.mods.isOneOf(GivenOrImplicit) =>
@@ -547,19 +568,20 @@ object desugar {
547568

548569
val boundNames = getBoundNames(params, meth.paramss)
549570

550-
val recur = fitEvidenceParams(params, meth.name, boundNames)
571+
val fitParams = fitEvidenceParams(params, meth.name, boundNames)
551572

552-
if meth.hasAttachment(PolyFunctionApply) then
553-
meth.removeAttachment(PolyFunctionApply)
554-
// for PolyFunctions we are limited to a single term param list, so we reuse the recur logic to compute the new parameter lists
555-
// and then we add the other parameter lists as function types to the return type
556-
val (paramsFst, paramsSnd) = recur(meth.paramss)
573+
if meth.removeAttachment(PolyFunctionApply).isDefined then
574+
// for PolyFunctions we are limited to a single term param list, so we
575+
// reuse the fitEvidenceParams logic to compute the new parameter lists
576+
// and then we add the other parameter lists as function types to the
577+
// return type
578+
val (paramsFst, paramsSnd) = fitParams(meth.paramss)
557579
if ctx.mode.is(Mode.Type) then
558580
cpy.DefDef(meth)(paramss = paramsFst, tpt = functionsOf(paramsSnd, meth.tpt))
559581
else
560582
cpy.DefDef(meth)(paramss = paramsFst, rhs = functionsOf(paramsSnd, meth.rhs))
561583
else
562-
val (paramsFst, paramsSnd) = recur(meth.paramss)
584+
val (paramsFst, paramsSnd) = fitParams(meth.paramss)
563585
cpy.DefDef(meth)(paramss = paramsFst ++ paramsSnd)
564586
end addEvidenceParams
565587

0 commit comments

Comments
 (0)