Skip to content

Commit

Permalink
Don't eta expand unary varargs methods
Browse files Browse the repository at this point in the history
A unary varargs method sits between nullary methods that sometimes get a () argument inferred
(i.e. for methods coming from Java) and other methods that can be eta expanded. The safest
strategy for them is to do neither, and expect either an explicit expected function type,
or an explicit argument. That's also what Scala 2 does.

Fixes #16820

Reclassifies #14567 to be a neg test (with the error message suggested in the original issue
for #14567)
  • Loading branch information
odersky committed Feb 12, 2023
1 parent 3d251d6 commit ae05f99
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3922,7 +3922,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
else defn.functionArity(ptNorm)
else
val nparams = wtp.paramInfos.length
if nparams > 0 || pt.eq(AnyFunctionProto) then nparams
if nparams > 1
|| nparams == 1 && !wtp.isVarArgsMethod
|| pt.eq(AnyFunctionProto)
then nparams
else -1 // no eta expansion in this case
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
case _ =>
Expand Down
22 changes: 22 additions & 0 deletions tests/neg/i16820.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Error: tests/neg/i16820.scala:5:11 ----------------------------------------------------------------------------------
5 | val x1 = f // error
| ^
| missing arguments for method f in object Test
-- [E100] Syntax Error: tests/neg/i16820.scala:6:11 --------------------------------------------------------------------
6 | val x2 = g // error
| ^
| method g in object Test must be called with () argument
|
| longer explanation available when compiling with `-explain`
-- Error: tests/neg/i16820.scala:8:14 ----------------------------------------------------------------------------------
8 | val x3 = "".formatted // error
| ^^^^^^^^^^^^
| missing arguments for method formatted in class String
-- Error: tests/neg/i16820.scala:9:40 ----------------------------------------------------------------------------------
9 | val x4 = java.nio.file.Paths.get(".").toRealPath // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| missing arguments for method toRealPath in trait Path
-- Error: tests/neg/i16820.scala:13:14 ---------------------------------------------------------------------------------
13 |def test = Foo(3) // error
| ^^^^^^
| missing arguments for method apply in object Foo
13 changes: 13 additions & 0 deletions tests/neg/i16820.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object Test:
def f(xs: Int*) = xs.sum
def g() = 1

val x1 = f // error
val x2 = g // error

val x3 = "".formatted // error
val x4 = java.nio.file.Paths.get(".").toRealPath // error

// #14567
case class Foo(x: Int)(xs: String*)
def test = Foo(3) // error
2 changes: 1 addition & 1 deletion tests/pos/i14367.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def m(i: Int*) = i.sum
val f1 = m
val f1: Seq[Int] => Int = m
val f2 = i => m(i*)

def n(i: Seq[Int]) = i.sum
Expand Down
2 changes: 0 additions & 2 deletions tests/pos/i14567.scala

This file was deleted.

0 comments on commit ae05f99

Please sign in to comment.