Skip to content

Commit f2aa33c

Browse files
committed
Take names of synthesized contextual functions from expected type
If expected type has parameter names, use them for the generated contextual closure.
1 parent be26204 commit f2aa33c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ object desugar {
205205

206206
def makeImplicitParameters(
207207
tpts: List[Tree], implicitFlag: FlagSet,
208-
mkParamName: () => TermName,
208+
mkParamName: Int => TermName,
209209
forPrimaryConstructor: Boolean = false
210210
)(using Context): List[ValDef] =
211211
for (tpt, i) <- tpts.zipWithIndex yield {
212212
val paramFlags: FlagSet = if (forPrimaryConstructor) LocalParamAccessor else Param
213-
val epname = mkParamName()
213+
val epname = mkParamName(i)
214214
ValDef(epname, tpt, EmptyTree).withFlags(paramFlags | implicitFlag)
215215
}
216216

@@ -254,7 +254,7 @@ object desugar {
254254
// using clauses, we only need names that are unique among the
255255
// parameters of the method since shadowing does not affect
256256
// implicit resolution in Scala 3.
257-
mkParamName = () =>
257+
mkParamName = i =>
258258
val index = seenContextBounds + 1 // Start at 1 like FreshNameCreator.
259259
val ret = ContextBoundParamName(EmptyTermName, index)
260260
seenContextBounds += 1
@@ -1602,9 +1602,12 @@ object desugar {
16021602
case vd: ValDef => vd
16031603
}
16041604

1605-
def makeContextualFunction(formals: List[Tree], body: Tree, erasedParams: List[Boolean])(using Context): Function = {
1605+
def makeContextualFunction(formals: List[Tree], paramNamesOrNil: List[TermName], body: Tree, erasedParams: List[Boolean])(using Context): Function = {
16061606
val mods = Given
1607-
val params = makeImplicitParameters(formals, mods, mkParamName = () => ContextFunctionParamName.fresh())
1607+
val params = makeImplicitParameters(formals, mods,
1608+
mkParamName = i =>
1609+
if paramNamesOrNil.isEmpty then ContextFunctionParamName.fresh()
1610+
else paramNamesOrNil(i))
16081611
FunctionWithMods(params, body, Modifiers(mods), erasedParams)
16091612
}
16101613

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3204,6 +3204,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
32043204

32053205
protected def makeContextualFunction(tree: untpd.Tree, pt: Type)(using Context): Tree = {
32063206
val defn.FunctionOf(formals, _, true) = pt.dropDependentRefinement: @unchecked
3207+
val paramNamesOrNil = pt match
3208+
case RefinedType(_, _, rinfo: MethodType) => rinfo.paramNames
3209+
case _ => Nil
32073210

32083211
// The getter of default parameters may reach here.
32093212
// Given the code below
@@ -3236,7 +3239,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
32363239
case _ => paramTypes.map(_ => false)
32373240
}
32383241

3239-
val ifun = desugar.makeContextualFunction(paramTypes, tree, erasedParams)
3242+
val ifun = desugar.makeContextualFunction(paramTypes, paramNamesOrNil, tree, erasedParams)
32403243
typr.println(i"make contextual function $tree / $pt ---> $ifun")
32413244
typedFunctionValue(ifun, pt)
32423245
}

0 commit comments

Comments
 (0)