@@ -1656,27 +1656,39 @@ trait Applications extends Compatibility {
1656
1656
alts.filter(sizeFits(_))
1657
1657
1658
1658
def narrowByShapes (alts : List [TermRef ]): List [TermRef ] =
1659
-
1659
+
1660
1660
/** Normalization steps before shape-checking arguments:
1661
1661
*
1662
1662
* { expr } --> expr
1663
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.
1664
+ * if n > 1, no alternative has a corresponding formal parameter that
1665
+ * is an n-ary function, and at least one alternative has a corresponding
1666
+ * formal parameter that is a unary function.
1666
1667
*/
1667
- def normArg (arg : untpd.Tree ): untpd.Tree = arg match
1668
- case Block (Nil , expr) => normArg(expr)
1669
- case x @ untpd.Function (args, body) =>
1668
+ def normArg (arg : untpd.Tree , idx : Int ): untpd.Tree = arg match
1669
+ case Block (Nil , expr) => normArg(expr, idx)
1670
+ case untpd.Function (args, body) =>
1671
+
1672
+ // If ref refers to a method whose parameter at index `idx` is a function type,
1673
+ // the arity of that function, otherise 0.
1674
+ def paramCount (ref : TermRef ) =
1675
+ val formals = ref.widen.firstParamTypes
1676
+ if formals.length > idx then
1677
+ formals(idx) match
1678
+ case defn.FunctionOf (args, _, _, _) => args.length
1679
+ case _ => 0
1680
+ else 0
1681
+
1670
1682
val numArgs = args.length
1671
- def paramCount (ref : TermRef ) = ref.widen.firstParamTypes.length
1672
1683
if numArgs > 1
1673
1684
&& ! alts.exists(paramCount(_) == numArgs)
1674
1685
&& alts.exists(paramCount(_) == 1 )
1675
1686
then untpd.Function (untpd.Tuple (args) :: Nil , body)
1676
1687
else arg
1677
1688
case _ => arg
1678
-
1679
- val normArgs = args.mapConserve(normArg)
1689
+ end normArg
1690
+
1691
+ val normArgs = args.mapWithIndexConserve(normArg)
1680
1692
if (normArgs exists untpd.isFunctionWithUnknownParamType)
1681
1693
if (hasNamedArg(args)) narrowByTrees(alts, args map treeShape, resultType)
1682
1694
else narrowByTypes(alts, normArgs map typeShape, resultType)
0 commit comments