Skip to content

Commit 3f8ef66

Browse files
Extract narrowByNextParamClause logic into local def
1 parent eedd309 commit 3f8ef66

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,37 @@ trait Applications extends Compatibility {
22862286
def narrowByTypes(alts: List[TermRef], argTypes: List[Type], resultType: Type): List[TermRef] =
22872287
alts.filterConserve(isApplicableMethodRef(_, argTypes, resultType, ArgMatch.CompatibleCAP))
22882288

2289+
def narrowByNextParamClause(alts: List[TermRef], args: List[Tree], resultType: FunOrPolyProto): List[TermRef] =
2290+
2291+
/** The type of alternative `alt` after instantiating its first parameter
2292+
* clause with `argTypes`. In addition, if the resulting type is a PolyType
2293+
* and `typeArgs` matches its parameter list, instantiate the result with `typeArgs`.
2294+
*/
2295+
def skipParamClause(typeArgs: List[Type])(alt: TermRef): Type =
2296+
def skip(tp: Type): Type = tp match
2297+
case tp: PolyType =>
2298+
skip(tp.resultType) match
2299+
case NoType =>
2300+
NoType
2301+
case rt: PolyType if typeArgs.length == rt.paramInfos.length =>
2302+
tp.derivedLambdaType(resType = rt.instantiate(typeArgs))
2303+
case rt =>
2304+
tp.derivedLambdaType(resType = rt).asInstanceOf[PolyType].flatten
2305+
case tp: MethodType =>
2306+
tp.instantiate(args.tpes)
2307+
case _ =>
2308+
NoType
2309+
skip(alt.widen)
2310+
2311+
resultType match
2312+
case PolyProto(targs, resType) =>
2313+
// try to narrow further with snd argument list and following type params
2314+
resolveMapped(skipParamClause(targs.tpes))(alts, resType, srcPos)
2315+
case resType =>
2316+
// try to narrow further with snd argument list
2317+
resolveMapped(skipParamClause(Nil))(alts, resType, srcPos)
2318+
end narrowByNextParamClause
2319+
22892320
/** Normalization steps before checking arguments:
22902321
*
22912322
* { expr } --> expr
@@ -2431,27 +2462,6 @@ trait Applications extends Compatibility {
24312462
else compat
24322463
}
24332464

2434-
/** The type of alternative `alt` after instantiating its first parameter
2435-
* clause with `argTypes`. In addition, if the resulting type is a PolyType
2436-
* and `typeArgs` matches its parameter list, instantiate the result with `typeArgs`.
2437-
*/
2438-
def skipParamClause(argTypes: List[Type], typeArgs: List[Type])(alt: TermRef): Type =
2439-
def skip(tp: Type): Type = tp match {
2440-
case tp: PolyType =>
2441-
skip(tp.resultType) match
2442-
case NoType =>
2443-
NoType
2444-
case rt: PolyType if typeArgs.length == rt.paramInfos.length =>
2445-
tp.derivedLambdaType(resType = rt.instantiate(typeArgs))
2446-
case rt =>
2447-
tp.derivedLambdaType(resType = rt).asInstanceOf[PolyType].flatten
2448-
case tp: MethodType =>
2449-
tp.instantiate(argTypes)
2450-
case _ =>
2451-
NoType
2452-
}
2453-
skip(alt.widen)
2454-
24552465
def resultIsMethod(tp: Type): Boolean = tp.widen.stripPoly match
24562466
case tp: MethodType => stripInferrable(tp.resultType).isInstanceOf[MethodType]
24572467
case _ => false
@@ -2467,14 +2477,9 @@ trait Applications extends Compatibility {
24672477
else
24682478
val deepPt = pt.deepenProto
24692479
deepPt match
2470-
case pt @ FunProto(_, PolyProto(targs, resType)) =>
2471-
// try to narrow further with snd argument list and following type params
2472-
warnOnPriorityChange(candidates, found):
2473-
resolveMapped(skipParamClause(pt.typedArgs().tpes, targs.tpes))(_, resType, srcPos)
24742480
case pt @ FunProto(_, resType: FunOrPolyProto) =>
2475-
// try to narrow further with snd argument list
24762481
warnOnPriorityChange(candidates, found):
2477-
resolveMapped(skipParamClause(pt.typedArgs().tpes, Nil))(_, resType, srcPos)
2482+
narrowByNextParamClause(_, pt.typedArgs(), resType)
24782483
case _ =>
24792484
// prefer alternatives that need no eta expansion
24802485
val noCurried = alts.filterConserve(!resultIsMethod(_))

0 commit comments

Comments
 (0)