Skip to content

Commit 3e18fee

Browse files
authored
Don't eta expand unary varargs methods (#16892)
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)
2 parents b289cd1 + ae05f99 commit 3e18fee

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
@@ -3919,7 +3919,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39193919
else defn.functionArity(ptNorm)
39203920
else
39213921
val nparams = wtp.paramInfos.length
3922-
if nparams > 0 || pt.eq(AnyFunctionProto) then nparams
3922+
if nparams > 1
3923+
|| nparams == 1 && !wtp.isVarArgsMethod
3924+
|| pt.eq(AnyFunctionProto)
3925+
then nparams
39233926
else -1 // no eta expansion in this case
39243927
adaptNoArgsUnappliedMethod(wtp, funExpected, arity)
39253928
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)