Skip to content

Commit 7f32f50

Browse files
committed
Fix #7757: Do auto-parameter-untupling also for overloaded methods
The case where a lambda is passed to a single method is handled in `typedFunction`. But overloading resolution failed before getting there.
1 parent 9aa7991 commit 7f32f50

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,11 +1638,6 @@ trait Applications extends Compatibility {
16381638
val candidates = pt match {
16391639
case pt @ FunProto(args, resultType) =>
16401640
val numArgs = args.length
1641-
val normArgs = args.mapConserve {
1642-
case Block(Nil, expr) => expr
1643-
case x => x
1644-
}
1645-
16461641
def sizeFits(alt: TermRef): Boolean = alt.widen.stripPoly match {
16471642
case tp: MethodType =>
16481643
val ptypes = tp.paramInfos
@@ -1661,6 +1656,27 @@ trait Applications extends Compatibility {
16611656
alts.filter(sizeFits(_))
16621657

16631658
def narrowByShapes(alts: List[TermRef]): List[TermRef] =
1659+
1660+
/** Normalization steps before shape-checking arguments:
1661+
*
1662+
* { expr } --> expr
1663+
* (x1, ..., xn) => expr --> ((x1, ..., xn)) => expr
1664+
* if n > 1, no alternative takes `n` parameters,
1665+
* and at least one alternative takes 1 parameter.
1666+
*/
1667+
def normArg(arg: untpd.Tree): untpd.Tree = arg match
1668+
case Block(Nil, expr) => normArg(expr)
1669+
case x @ untpd.Function(args, body) =>
1670+
val numArgs = args.length
1671+
def paramCount(ref: TermRef) = ref.widen.firstParamTypes.length
1672+
if numArgs > 1
1673+
&& !alts.exists(paramCount(_) == numArgs)
1674+
&& alts.exists(paramCount(_) == 1)
1675+
then untpd.Function(untpd.Tuple(args) :: Nil, body)
1676+
else arg
1677+
case _ => arg
1678+
1679+
val normArgs = args.mapConserve(normArg)
16641680
if (normArgs exists untpd.isFunctionWithUnknownParamType)
16651681
if (hasNamedArg(args)) narrowByTrees(alts, args map treeShape, resultType)
16661682
else narrowByTypes(alts, normArgs map typeShape, resultType)

tests/pos/i7757.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val m: Map[Int, String] = ???
2+
val _ = m.map((a, b) => a + b.length)

0 commit comments

Comments
 (0)