Skip to content

Commit

Permalink
Merge pull request #14397 from dotty-staging/fix-14367
Browse files Browse the repository at this point in the history
Infer parameters of eta applications with vararg parameters
  • Loading branch information
odersky authored Feb 9, 2022
2 parents e12105d + 9a644e4 commit 037c5bf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case mtpe: MethodType =>
val pos = paramIndex(param.name)
if pos < mtpe.paramInfos.length then
val ptype = mtpe.paramInfos(pos)
if ptype.isRepeatedParam then NoType else ptype
mtpe.paramInfos(pos)
// This works only if vararg annotations match up.
// See neg/i14367.scala for an example where the inferred type is mispredicted.
// Nevertheless, the alternative would be to give up completely, so this is
// defensible.
else NoType
case _ => NoType
if target.exists then formal <:< target
Expand Down Expand Up @@ -1317,10 +1320,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
*/
var fnBody = tree.body

def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match {
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match
case Ident(name) => name == param.name
case Typed(arg1, _) if untpd.isWildcardStarArg(arg) => refersTo(arg1, param)
case _ => false
}

/** If parameter `param` appears exactly once as an argument in `args`,
* the singleton list consisting of its position in `args`, otherwise `Nil`.
Expand Down
10 changes: 0 additions & 10 deletions compiler/test-resources/repl/errorThenValid

This file was deleted.

7 changes: 7 additions & 0 deletions tests/neg/i14367.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E007] Type Mismatch Error: tests/neg/i14367.scala:2:16 -------------------------------------------------------------
2 |val h2 = i => p(i) // error: Found (i : Seq[Int]), Required: Int
| ^
| Found: (i : Seq[Int])
| Required: Int

longer explanation available when compiling with `-explain`
5 changes: 5 additions & 0 deletions tests/neg/i14367.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def p(i: Int*) = i.sum
val h2 = i => p(i) // error: Found (i : Seq[Int]), Required: Int
// It would be more logical to fail with a "missing parameter type", however.


7 changes: 7 additions & 0 deletions tests/pos/i14367.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def m(i: Int*) = i.sum
val f1 = m
val f2 = i => m(i*)

def n(i: Seq[Int]) = i.sum
val g1 = n
val g2 = i => n(i)

0 comments on commit 037c5bf

Please sign in to comment.