Skip to content

Commit 945ee91

Browse files
committed
Pretype functional arguments when doing overload resolution
1 parent ff6c79d commit 945ee91

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,10 @@ trait Applications extends Compatibility { self: Typer =>
10891089
val alts2 = narrowByShapes(alts1)
10901090
//ctx.log(i"narrowed by shape: ${alts1.map(_.symbol.showDcl)}%, %")
10911091
if (isDetermined(alts2)) alts2
1092-
else narrowByTrees(alts2, pt.typedArgs, resultType)
1092+
else {
1093+
pretypeArgs(alts2, pt)
1094+
narrowByTrees(alts2, pt.typedArgs, resultType)
1095+
}
10931096
}
10941097

10951098
case pt @ PolyProto(targs, pt1) =>
@@ -1122,6 +1125,48 @@ trait Applications extends Compatibility { self: Typer =>
11221125
}
11231126
}
11241127

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+
11251170
private def harmonizeWith[T <: AnyRef](ts: List[T])(tpe: T => Type, adapt: (T, Type) => T)(implicit ctx: Context): List[T] = {
11261171
def numericClasses(ts: List[T], acc: Set[Symbol]): Set[Symbol] = ts match {
11271172
case t :: ts1 =>

src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ object ProtoTypes {
179179
if ((args eq this.args) && (resultType eq this.resultType) && (typer eq this.typer)) this
180180
else new FunProto(args, resultType, typer)
181181

182-
def argsAreTyped: Boolean = myTypedArgs.nonEmpty || args.isEmpty
182+
def argsAreTyped: Boolean = myTypedArgs.size == args.length
183183

184184
/** The typed arguments. This takes any arguments already typed using
185185
* `typedArg` into account.

tests/pos/overloaded.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ object overloaded {
2121

2222
val xs = List("a", "b")
2323
xs.mkString
24+
25+
def map(f: Char => Char): String = ???
26+
def map[U](f: Char => U): Seq[U] = ???
27+
map(x => x.toUpper)
2428
}

0 commit comments

Comments
 (0)