Skip to content

Better expected type for arguments of overloaded methods #10327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2026,38 +2026,37 @@ trait Applications extends Compatibility {
private def pretypeArgs(alts: List[TermRef], pt: FunProto)(using Context): Unit = {
def recur(altFormals: List[List[Type]], args: List[untpd.Tree]): Unit = args match {
case arg :: args1 if !altFormals.exists(_.isEmpty) =>
untpd.functionWithUnknownParamType(arg) match {
case Some(fn) =>
def isUniform[T](xs: List[T])(p: (T, T) => Boolean) = xs.forall(p(_, xs.head))
val formalsForArg: List[Type] = altFormals.map(_.head)
def argTypesOfFormal(formal: Type): List[Type] =
formal match {
case defn.FunctionOf(args, result, isImplicit, isErased) => args
case defn.PartialFunctionOf(arg, result) => arg :: Nil
case _ => Nil
}
val formalParamTypessForArg: List[List[Type]] =
formalsForArg.map(argTypesOfFormal)
if (formalParamTypessForArg.forall(_.nonEmpty) &&
isUniform(formalParamTypessForArg)((x, y) => x.length == y.length)) {
val commonParamTypes = formalParamTypessForArg.transpose.map(ps =>
// Given definitions above, for i = 1,...,m,
// ps(i) = List(p_i_1, ..., p_i_n) -- i.e. a column
// If all p_i_k's are the same, assume the type as formal parameter
// type of the i'th parameter of the closure.
if (isUniform(ps)(_ frozen_=:= _)) ps.head
else WildcardType)
def isPartial = // we should generate a partial function for the arg
fn.isInstanceOf[untpd.Match] &&
formalsForArg.exists(_.isRef(defn.PartialFunctionClass))
val commonFormal =
if (isPartial) defn.PartialFunctionOf(commonParamTypes.head, WildcardType)
else defn.FunctionOf(commonParamTypes, WildcardType)
overload.println(i"pretype arg $arg with expected type $commonFormal")
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
withMode(Mode.ImplicitsEnabled)(pt.typedArg(arg, commonFormal))
}
case None =>
def isUniform[T](xs: List[T])(p: (T, T) => Boolean) = xs.forall(p(_, xs.head))
val formalsForArg: List[Type] = altFormals.map(_.head)
def argTypesOfFormal(formal: Type): List[Type] =
formal match {
case defn.FunctionOf(args, result, isImplicit, isErased) => args
case defn.PartialFunctionOf(arg, result) => arg :: Nil
case _ => Nil
}
val formalParamTypessForArg: List[List[Type]] =
formalsForArg.map(argTypesOfFormal)
if (formalParamTypessForArg.forall(_.nonEmpty) &&
isUniform(formalParamTypessForArg)((x, y) => x.length == y.length)) {
val commonParamTypes = formalParamTypessForArg.transpose.map(ps =>
// Given definitions above, for i = 1,...,m,
// ps(i) = List(p_i_1, ..., p_i_n) -- i.e. a column
// If all p_i_k's are the same, assume the type as formal parameter
// type of the i'th parameter of the closure.
if (isUniform(ps)(_ frozen_=:= _)) ps.head
else WildcardType)
/** Should we generate a partial function for the arg ? */
def isPartial = untpd.functionWithUnknownParamType(arg) match
case Some(_: untpd.Match) =>
formalsForArg.exists(_.isRef(defn.PartialFunctionClass))
case _ =>
false
val commonFormal =
if (isPartial) defn.PartialFunctionOf(commonParamTypes.head, WildcardType)
else defn.FunctionOf(commonParamTypes, WildcardType)
overload.println(i"pretype arg $arg with expected type $commonFormal")
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
withMode(Mode.ImplicitsEnabled)(pt.typedArg(arg, commonFormal))
}
recur(altFormals.map(_.tail), args1)
case _ =>
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i10325.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object Test {
def nullToNone[K, V](tuple: (K, V)): (K, Option[V]) = {
val (k, v) = tuple
(k, Option(v))
}

def test: Unit = {
val scalaMap: Map[String, String] = Map()

val a = scalaMap.map(nullToNone)
val a1: Map[String, Option[String]] = a

val b = scalaMap.map(nullToNone(_))
val b1: Map[String, Option[String]] = b

val c = scalaMap.map(x => nullToNone(x))
val c1: Map[String, Option[String]] = c
}
}