@@ -1929,21 +1929,39 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19291929 NoType
19301930 }
19311931
1932- def tryToInstantiateInUnion (tp : Type ): Unit = tp match
1933- case tp : OrType =>
1934- tryToInstantiateInUnion(tp.tp1)
1935- tryToInstantiateInUnion(tp.tp2)
1932+ /** Try to instantiate one type variable bounded by function types that appear
1933+ * deeply inside `tp`, including union or intersection types.
1934+ */
1935+ def tryToInstantiateDeeply (tp : Type ): Boolean = tp match
1936+ case tp : AndOrType =>
1937+ tryToInstantiateDeeply(tp.tp1)
1938+ || tryToInstantiateDeeply(tp.tp2)
19361939 case tp : FlexibleType =>
1937- tryToInstantiateInUnion(tp.hi)
1938- case tp : TypeVar =>
1940+ tryToInstantiateDeeply(tp.hi)
1941+ case tp : TypeVar if isConstrainedByFunctionType(tp) =>
1942+ // Only instantiate if the type variable is constrained by function types
19391943 isFullyDefined(tp, ForceDegree .flipBottom)
1940- case _ =>
1944+ case _ => false
1945+
1946+ def isConstrainedByFunctionType (tvar : TypeVar ): Boolean =
1947+ val origin = tvar.origin
1948+ val bounds = ctx.typerState.constraint.bounds(origin)
1949+ def containsFunctionType (tp : Type ): Boolean = tp.dealias match
1950+ case tp if defn.isFunctionType(tp) => true
1951+ case SAMType (_, _) => true
1952+ case tp : AndOrType =>
1953+ containsFunctionType(tp.tp1) || containsFunctionType(tp.tp2)
1954+ case tp : FlexibleType =>
1955+ containsFunctionType(tp.hi)
1956+ case _ => false
1957+ containsFunctionType(bounds.lo) || containsFunctionType(bounds.hi)
19411958
19421959 if untpd.isFunctionWithUnknownParamType(tree) && ! calleeType.exists then
1943- // Try to instantiate `pt` when possible, including type variables in union types
1944- // to help finding function types. If it does not work the error will be reported
1945- // later in `inferredParam`, when we try to infer the parameter type.
1946- tryToInstantiateInUnion(pt)
1960+ // Try to instantiate `pt` when possible, by searching a nested type variable
1961+ // bounded by function types to help infer parameter types.
1962+ // If it does not work the error will be reported later in `inferredParam`,
1963+ // when we try to infer the parameter type.
1964+ tryToInstantiateDeeply(pt)
19471965
19481966 val (protoFormals, resultTpt) = decomposeProtoFunction(pt, params.length, tree.srcPos)
19491967
0 commit comments