Skip to content

Commit 037c5bf

Browse files
authored
Merge pull request #14397 from dotty-staging/fix-14367
Infer parameters of eta applications with vararg parameters
2 parents e12105d + 9a644e4 commit 037c5bf

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12271227
case mtpe: MethodType =>
12281228
val pos = paramIndex(param.name)
12291229
if pos < mtpe.paramInfos.length then
1230-
val ptype = mtpe.paramInfos(pos)
1231-
if ptype.isRepeatedParam then NoType else ptype
1230+
mtpe.paramInfos(pos)
1231+
// This works only if vararg annotations match up.
1232+
// See neg/i14367.scala for an example where the inferred type is mispredicted.
1233+
// Nevertheless, the alternative would be to give up completely, so this is
1234+
// defensible.
12321235
else NoType
12331236
case _ => NoType
12341237
if target.exists then formal <:< target
@@ -1317,10 +1320,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
13171320
*/
13181321
var fnBody = tree.body
13191322

1320-
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match {
1323+
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match
13211324
case Ident(name) => name == param.name
1325+
case Typed(arg1, _) if untpd.isWildcardStarArg(arg) => refersTo(arg1, param)
13221326
case _ => false
1323-
}
13241327

13251328
/** If parameter `param` appears exactly once as an argument in `args`,
13261329
* the singleton list consisting of its position in `args`, otherwise `Nil`.

compiler/test-resources/repl/errorThenValid

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/neg/i14367.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i14367.scala:2:16 -------------------------------------------------------------
2+
2 |val h2 = i => p(i) // error: Found (i : Seq[Int]), Required: Int
3+
| ^
4+
| Found: (i : Seq[Int])
5+
| Required: Int
6+
7+
longer explanation available when compiling with `-explain`

tests/neg/i14367.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def p(i: Int*) = i.sum
2+
val h2 = i => p(i) // error: Found (i : Seq[Int]), Required: Int
3+
// It would be more logical to fail with a "missing parameter type", however.
4+
5+

tests/pos/i14367.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def m(i: Int*) = i.sum
2+
val f1 = m
3+
val f2 = i => m(i*)
4+
5+
def n(i: Seq[Int]) = i.sum
6+
val g1 = n
7+
val g2 = i => n(i)

0 commit comments

Comments
 (0)