@@ -1151,17 +1151,38 @@ trait Applications extends Compatibility { self: Typer =>
1151
1151
}
1152
1152
arg match {
1153
1153
case arg : untpd.Function if arg.args.exists(isUnknownParamType) =>
1154
- val commonFormal = altFormals.map(_.head).reduceLeft(_ | _)
1155
- overload.println(i " pretype arg $arg with expected type $commonFormal" )
1156
- pt.typedArg(arg, commonFormal)
1154
+ def isUniform [T ](xs : List [T ])(p : (T , T ) => Boolean ) = xs.forall(p(_, xs.head))
1155
+ val formalsForArg : List [Type ] = altFormals.map(_.head)
1156
+ // For alternatives alt_1, ..., alt_n, test whether formal types for current argument are of the form
1157
+ // (p_1_1, ..., p_m_1) => r_1
1158
+ // ...
1159
+ // (p_1_n, ..., p_m_n) => r_n
1160
+ val decomposedFormalsForArg : List [Option [(List [Type ], Type )]] =
1161
+ formalsForArg.map(defn.FunctionOf .unapply)
1162
+ if (decomposedFormalsForArg.forall(_.isDefined)) {
1163
+ val formalParamTypessForArg : List [List [Type ]] =
1164
+ decomposedFormalsForArg.map(_.get._1)
1165
+ if (isUniform(formalParamTypessForArg)((x, y) => x.length == y.length)) {
1166
+ val commonParamTypes = formalParamTypessForArg.transpose.map(ps =>
1167
+ // Given definitions above, for i = 1,...,m,
1168
+ // ps(i) = List(p_i_1, ..., p_i_n) -- i.e. a column
1169
+ // If all p_i_k's are the same, assume the type as formal parameter
1170
+ // type of the i'th parameter of the closure.
1171
+ if (isUniform(ps)(ctx.typeComparer.isSameTypeWhenFrozen(_, _))) ps.head
1172
+ else WildcardType )
1173
+ val commonFormal = defn.FunctionOf (commonParamTypes, WildcardType )
1174
+ overload.println(i " pretype arg $arg with expected type $commonFormal" )
1175
+ pt.typedArg(arg, commonFormal)
1176
+ }
1177
+ }
1157
1178
case _ =>
1158
1179
}
1159
1180
recur(altFormals.map(_.tail), args1)
1160
1181
case _ =>
1161
1182
}
1162
1183
def paramTypes (alt : Type ): List [Type ] = alt match {
1163
1184
case mt : MethodType => mt.paramTypes
1164
- case mt : PolyType => paramTypes(mt.resultType).map(wildApprox(_))
1185
+ case mt : PolyType => paramTypes(mt.resultType)
1165
1186
case _ => Nil
1166
1187
}
1167
1188
recur(alts.map(alt => paramTypes(alt.widen)), pt.args)
0 commit comments