@@ -1089,7 +1089,10 @@ trait Applications extends Compatibility { self: Typer =>
1089
1089
val alts2 = narrowByShapes(alts1)
1090
1090
// ctx.log(i"narrowed by shape: ${alts1.map(_.symbol.showDcl)}%, %")
1091
1091
if (isDetermined(alts2)) alts2
1092
- else narrowByTrees(alts2, pt.typedArgs, resultType)
1092
+ else {
1093
+ pretypeArgs(alts2, pt)
1094
+ narrowByTrees(alts2, pt.typedArgs, resultType)
1095
+ }
1093
1096
}
1094
1097
1095
1098
case pt @ PolyProto (targs, pt1) =>
@@ -1122,6 +1125,48 @@ trait Applications extends Compatibility { self: Typer =>
1122
1125
}
1123
1126
}
1124
1127
1128
+ /** Try to typecheck any arguments in `pt` that are function values missing a
1129
+ * parameter type. The expected type for these arguments is the lub of the
1130
+ * corresponding formal parameter types of all alternatives. Type variables
1131
+ * in formal parameter types are replaced by wildcards. The result of the
1132
+ * typecheck is stored in `pt`, to be retrieved when its `typedArgs` are selected.
1133
+ * The benefit of doing this is to allow idioms like this:
1134
+ *
1135
+ * def map(f: Char => Char): String = ???
1136
+ * def map[U](f: Char => U): Seq[U] = ???
1137
+ * map(x => x.toUpper)
1138
+ *
1139
+ * Without `pretypeArgs` we'd get a "missing parameter type" error for `x`.
1140
+ * With `pretypeArgs`, we use the union of the two formal parameter types
1141
+ * `Char => Char` and `Char => ?` as the expected type of the closure `x => x.toUpper`.
1142
+ * That union is `Char => Char`, so we have an expected parameter type `Char`
1143
+ * for `x`, and the code typechecks.
1144
+ */
1145
+ private def pretypeArgs (alts : List [TermRef ], pt : FunProto )(implicit ctx : Context ): Unit = {
1146
+ def recur (altFormals : List [List [Type ]], args : List [untpd.Tree ]): Unit = args match {
1147
+ case arg :: args1 if ! altFormals.exists(_.isEmpty) =>
1148
+ def isUnknownParamType (t : untpd.Tree ) = t match {
1149
+ case ValDef (_, tpt, _) => tpt.isEmpty
1150
+ case _ => false
1151
+ }
1152
+ arg match {
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)
1157
+ case _ =>
1158
+ }
1159
+ recur(altFormals.map(_.tail), args1)
1160
+ case _ =>
1161
+ }
1162
+ def paramTypes (alt : Type ): List [Type ] = alt match {
1163
+ case mt : MethodType => mt.paramTypes
1164
+ case mt : PolyType => paramTypes(mt.resultType).map(wildApprox(_))
1165
+ case _ => Nil
1166
+ }
1167
+ recur(alts.map(alt => paramTypes(alt.widen)), pt.args)
1168
+ }
1169
+
1125
1170
private def harmonizeWith [T <: AnyRef ](ts : List [T ])(tpe : T => Type , adapt : (T , Type ) => T )(implicit ctx : Context ): List [T ] = {
1126
1171
def numericClasses (ts : List [T ], acc : Set [Symbol ]): Set [Symbol ] = ts match {
1127
1172
case t :: ts1 =>
0 commit comments