Skip to content

Commit ae05f99

Browse files
committed
Don't eta expand unary varargs methods
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)
1 parent 3d251d6 commit ae05f99

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3922,7 +3922,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39223922
else defn.functionArity(ptNorm)
39233923
else
39243924
val nparams = wtp.paramInfos.length
3925-
if nparams > 0 || pt.eq(AnyFunctionProto) then nparams
3925+
if nparams > 1
3926+
|| nparams == 1 && !wtp.isVarArgsMethod
3927+
|| pt.eq(AnyFunctionProto)
3928+
then nparams
39263929
else -1 // no eta expansion in this case
39273930
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
39283931
case _ =>

tests/neg/i16820.check

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Error: tests/neg/i16820.scala:5:11 ----------------------------------------------------------------------------------
2+
5 | val x1 = f // error
3+
| ^
4+
| missing arguments for method f in object Test
5+
-- [E100] Syntax Error: tests/neg/i16820.scala:6:11 --------------------------------------------------------------------
6+
6 | val x2 = g // error
7+
| ^
8+
| method g in object Test must be called with () argument
9+
|
10+
| longer explanation available when compiling with `-explain`
11+
-- Error: tests/neg/i16820.scala:8:14 ----------------------------------------------------------------------------------
12+
8 | val x3 = "".formatted // error
13+
| ^^^^^^^^^^^^
14+
| missing arguments for method formatted in class String
15+
-- Error: tests/neg/i16820.scala:9:40 ----------------------------------------------------------------------------------
16+
9 | val x4 = java.nio.file.Paths.get(".").toRealPath // error
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
| missing arguments for method toRealPath in trait Path
19+
-- Error: tests/neg/i16820.scala:13:14 ---------------------------------------------------------------------------------
20+
13 |def test = Foo(3) // error
21+
| ^^^^^^
22+
| missing arguments for method apply in object Foo

tests/neg/i16820.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test:
2+
def f(xs: Int*) = xs.sum
3+
def g() = 1
4+
5+
val x1 = f // error
6+
val x2 = g // error
7+
8+
val x3 = "".formatted // error
9+
val x4 = java.nio.file.Paths.get(".").toRealPath // error
10+
11+
// #14567
12+
case class Foo(x: Int)(xs: String*)
13+
def test = Foo(3) // error

tests/pos/i14367.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def m(i: Int*) = i.sum
2-
val f1 = m
2+
val f1: Seq[Int] => Int = m
33
val f2 = i => m(i*)
44

55
def n(i: Seq[Int]) = i.sum

tests/pos/i14567.scala

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

0 commit comments

Comments
 (0)