Skip to content
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

Infer parameters of eta applications with vararg parameters #14397

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 mispredicated.
odersky marked this conversation as resolved.
Show resolved Hide resolved
// 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
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)