Skip to content

Commit 8c7e8bd

Browse files
Parametrize resolveOverloaded by the inner resolve function
used for the two trials: without and with implicits and conversions and analogously parametrize `narrowByNextParamClause`
1 parent 3f8ef66 commit 8c7e8bd

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ object Trees {
19081908
case MethodTpe(_, _, x: MethodType) => !x.isImplicitMethod
19091909
case _ => true
19101910
}}
1911-
val alternatives = ctx.typer.resolveOverloaded(allAlts, proto, receiver.srcPos)
1911+
val alternatives = ctx.typer.resolveOverloaded()(allAlts, proto, receiver.srcPos)
19121912
assert(alternatives.size == 1,
19131913
i"${if (alternatives.isEmpty) "no" else "multiple"} overloads available for " +
19141914
i"$method on ${receiver.tpe.widenDealiasKeepAnnots} with targs: $targs%, %; args: $args%, %; expectedType: $expectedType." +

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,8 +2115,11 @@ trait Applications extends Compatibility {
21152115
/** Resolve overloaded alternative `alts`, given expected type `pt`.
21162116
* Two trials: First, without implicits or SAM conversions enabled. Then,
21172117
* if the first finds no eligible candidates, with implicits and SAM conversions enabled.
2118+
* Each trial applies the `resolve` parameter.
21182119
*/
2119-
def resolveOverloaded(alts: List[TermRef], pt: Type, srcPos: SrcPos)(using Context): List[TermRef] =
2120+
def resolveOverloaded
2121+
(resolve: (List[TermRef], Type, SrcPos) => Context ?=> List[TermRef] = resolveOverloaded1)
2122+
(alts: List[TermRef], pt: Type, srcPos: SrcPos)(using Context): List[TermRef] =
21202123
record("resolveOverloaded")
21212124

21222125
/** Is `alt` a method or polytype whose result type after the first value parameter
@@ -2154,31 +2157,31 @@ trait Applications extends Compatibility {
21542157
case Nil => chosen
21552158
case alt2 :: Nil => alt2
21562159
case alts2 =>
2157-
resolveOverloaded(alts2, pt, srcPos) match {
2160+
resolveOverloaded(resolve)(alts2, pt, srcPos) match {
21582161
case alt2 :: Nil => alt2
21592162
case _ => chosen
21602163
}
21612164
}
21622165
case _ => chosen
21632166
}
21642167

2165-
def resolve(alts: List[TermRef]): List[TermRef] =
2168+
def resolve1(alts: List[TermRef]): List[TermRef] =
21662169
pt match
21672170
case pt: FunProto =>
21682171
if pt.applyKind == ApplyKind.Using then
21692172
val alts0 = alts.filterConserve(_.widen.stripPoly.isImplicitMethod)
2170-
if alts0 ne alts then return resolve(alts0)
2173+
if alts0 ne alts then return resolve1(alts0)
21712174
else if alts.exists(_.widen.stripPoly.isContextualMethod) then
2172-
return resolveMapped(alt => stripImplicit(alt.widen))(alts, pt, srcPos)
2175+
return resolveMapped(alt => stripImplicit(alt.widen), resolve)(alts, pt, srcPos)
21732176
case _ =>
21742177

2175-
var found = withoutMode(Mode.ImplicitsEnabled)(resolveOverloaded1(alts, pt, srcPos))
2178+
var found = withoutMode(Mode.ImplicitsEnabled)(resolve(alts, pt, srcPos))
21762179
if found.isEmpty && ctx.mode.is(Mode.ImplicitsEnabled) then
2177-
found = resolveOverloaded1(alts, pt, srcPos)
2180+
found = resolve(alts, pt, srcPos)
21782181
found match
21792182
case alt :: Nil => adaptByResult(alt, alts) :: Nil
21802183
case _ => found
2181-
end resolve
2184+
end resolve1
21822185

21832186
/** Try an apply method, if
21842187
* - the result is applied to value arguments and alternative is not a method, or
@@ -2211,9 +2214,9 @@ trait Applications extends Compatibility {
22112214

22122215
if (alts.exists(tryApply)) {
22132216
val expanded = alts.flatMap(applyMembers)
2214-
resolve(expanded).map(retract)
2217+
resolve1(expanded).map(retract)
22152218
}
2216-
else resolve(alts)
2219+
else resolve1(alts)
22172220
end resolveOverloaded
22182221

22192222
/** This private version of `resolveOverloaded` does the bulk of the work of
@@ -2286,7 +2289,9 @@ trait Applications extends Compatibility {
22862289
def narrowByTypes(alts: List[TermRef], argTypes: List[Type], resultType: Type): List[TermRef] =
22872290
alts.filterConserve(isApplicableMethodRef(_, argTypes, resultType, ArgMatch.CompatibleCAP))
22882291

2289-
def narrowByNextParamClause(alts: List[TermRef], args: List[Tree], resultType: FunOrPolyProto): List[TermRef] =
2292+
def narrowByNextParamClause
2293+
(resolve: (List[TermRef], Type, SrcPos) => Context ?=> List[TermRef])
2294+
(alts: List[TermRef], args: List[Tree], resultType: FunOrPolyProto): List[TermRef] =
22902295

22912296
/** The type of alternative `alt` after instantiating its first parameter
22922297
* clause with `argTypes`. In addition, if the resulting type is a PolyType
@@ -2311,10 +2316,10 @@ trait Applications extends Compatibility {
23112316
resultType match
23122317
case PolyProto(targs, resType) =>
23132318
// try to narrow further with snd argument list and following type params
2314-
resolveMapped(skipParamClause(targs.tpes))(alts, resType, srcPos)
2319+
resolveMapped(skipParamClause(targs.tpes), resolve)(alts, resType, srcPos)
23152320
case resType =>
23162321
// try to narrow further with snd argument list
2317-
resolveMapped(skipParamClause(Nil))(alts, resType, srcPos)
2322+
resolveMapped(skipParamClause(Nil), resolve)(alts, resType, srcPos)
23182323
end narrowByNextParamClause
23192324

23202325
/** Normalization steps before checking arguments:
@@ -2479,7 +2484,7 @@ trait Applications extends Compatibility {
24792484
deepPt match
24802485
case pt @ FunProto(_, resType: FunOrPolyProto) =>
24812486
warnOnPriorityChange(candidates, found):
2482-
narrowByNextParamClause(_, pt.typedArgs(), resType)
2487+
narrowByNextParamClause(resolveOverloaded1)(_, pt.typedArgs(), resType)
24832488
case _ =>
24842489
// prefer alternatives that need no eta expansion
24852490
val noCurried = alts.filterConserve(!resultIsMethod(_))
@@ -2528,12 +2533,12 @@ trait Applications extends Compatibility {
25282533
recur(paramss, 0)
25292534
case _ => (Nil, 0)
25302535

2531-
/** Resolve with `g` by mapping to a different problem where each alternative's type
2532-
* is mapped with `f`, alternatives with non-existing types or symbols are dropped,
2536+
/** ResolveOverloaded using `resolve` by mapping to a different problem where each alternative's
2537+
* type is mapped with `f`, alternatives with non-existing types or symbols are dropped,
25332538
* and the expected type is `pt`. Map the results back to the original alternatives.
25342539
*/
25352540
def resolveMapped
2536-
(f: TermRef => Type, g: (List[TermRef], Type, SrcPos) => Context ?=> List[TermRef] = resolveOverloaded)
2541+
(f: TermRef => Type, resolve: (List[TermRef], Type, SrcPos) => Context ?=> List[TermRef])
25372542
(alts: List[TermRef], pt: Type, srcPos: SrcPos)(using Context): List[TermRef] =
25382543
val reverseMapping = alts.flatMap { alt =>
25392544
val t = f(alt)
@@ -2557,7 +2562,7 @@ trait Applications extends Compatibility {
25572562
}
25582563
val mapped = reverseMapping.map(_._1)
25592564
overload.println(i"resolve mapped: ${mapped.map(_.widen)}%, % with $pt")
2560-
g(mapped, pt, srcPos)(using ctx.retractMode(Mode.SynthesizeExtMethodReceiver))
2565+
resolveOverloaded(resolve)(mapped, pt, srcPos)(using ctx.retractMode(Mode.SynthesizeExtMethodReceiver))
25612566
.map(reverseMapping.toMap)
25622567
end resolveMapped
25632568

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4108,7 +4108,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41084108
def altRef(alt: SingleDenotation) = TermRef(ref.prefix, ref.name, alt)
41094109
val alts = altDenots.map(altRef)
41104110

4111-
resolveOverloaded(alts, pt, tree.srcPos) match
4111+
resolveOverloaded()(alts, pt, tree.srcPos) match
41124112
case alt :: Nil =>
41134113
readaptSimplified(tree.withType(alt))
41144114
case Nil =>

0 commit comments

Comments
 (0)