@@ -1933,19 +1933,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19331933 NoType
19341934 }
19351935
1936- /** Try to instantiate one type variable bounded by function types that appear
1936+ /** Find one instantiatable type variable bounded by function types that appear
19371937 * deeply inside `tp`, including union or intersection types.
19381938 */
1939- def tryToInstantiateDeeply (tp : Type ): Boolean = tp.dealias match
1939+ def instantiatableTypeVar (tp : Type ): Type = tp.dealias match
19401940 case tp : AndOrType =>
1941- tryToInstantiateDeeply(tp.tp1)
1942- || tryToInstantiateDeeply(tp.tp2)
1941+ val t1 = instantiatableTypeVar(tp.tp1)
1942+ if t1.exists then t1
1943+ else instantiatableTypeVar(tp.tp2)
19431944 case tp : FlexibleType =>
1944- tryToInstantiateDeeply (tp.hi)
1945+ instantiatableTypeVar (tp.hi)
19451946 case tp : TypeVar if isConstrainedByFunctionType(tp) =>
19461947 // Only instantiate if the type variable is constrained by function types
1947- isFullyDefined(tp, ForceDegree .flipBottom)
1948- case _ => false
1948+ tp
1949+ case _ => NoType
19491950
19501951 def isConstrainedByFunctionType (tvar : TypeVar ): Boolean =
19511952 val origin = tvar.origin
@@ -1961,16 +1962,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19611962 case _ => false
19621963 containsFunctionType(bounds.lo) || containsFunctionType(bounds.hi)
19631964
1964- if untpd.isFunctionWithUnknownParamType(tree) && ! calleeType.exists then
1965+ if untpd.isFunctionWithUnknownParamType(tree) then
19651966 // Try to instantiate `pt` when possible.
19661967 // * If `pt` is a type variable, we try to instantiate it directly.
19671968 // * If `pt` is a more complex type, we try to instantiate it deeply by searching
19681969 // a nested type variable bounded by a function type to help infer parameter types.
19691970 // If it does not work the error will be reported later in `inferredParam`,
19701971 // when we try to infer the parameter type.
1971- pt match
1972- case pt : TypeVar => isFullyDefined(pt, ForceDegree .flipBottom)
1973- case _ => tryToInstantiateDeeply(pt)
1972+ // Note: we only check the `calleeType` if there is a TypeVar to instantiate to
1973+ // prioritize inferring from the callee.
1974+ val tp = if pt.isInstanceOf [TypeVar ] then pt else instantiatableTypeVar(pt)
1975+ if tp.exists && ! calleeType.exists then
1976+ isFullyDefined(tp, ForceDegree .flipBottom)
19741977
19751978 val (protoFormals, resultTpt) = decomposeProtoFunction(pt, params.length, tree.srcPos)
19761979
0 commit comments