Skip to content

Commit a6d5eb5

Browse files
committed
SI-8667 Improve too-many-args message
Use removeNames to help diagnose the application. Supplement the error message with how many extra args and any other residual assignments that the user might have thought was a properly named arg. The error message is gradual: succinct for short arg lists, more verbose for longer applications. Very long arg lists are probably generated, so that message is the least colloquial.
1 parent 9e30bee commit a6d5eb5

15 files changed

+172
-27
lines changed

src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,33 @@ trait ContextErrors {
538538
def NamedAndDefaultArgumentsNotSupportedForMacros(tree: Tree, fun: Tree) =
539539
NormalTypeError(tree, "macro applications do not support named and/or default arguments")
540540

541-
def TooManyArgsNamesDefaultsError(tree: Tree, fun: Tree) =
542-
NormalTypeError(tree, "too many arguments for "+treeSymTypeMsg(fun))
541+
def TooManyArgsNamesDefaultsError(tree: Tree, fun: Tree, expected: Int, supplied: Int, unknowns: List[Name]) = {
542+
val msg = {
543+
val badappl = {
544+
val excess = supplied - expected
545+
val target = treeSymTypeMsg(fun)
546+
547+
if (expected == 0) s"no arguments allowed for nullary $target"
548+
else if (excess < 3 && expected <= 5) s"too many arguments ($supplied) for $target"
549+
else if (expected > 10) s"$supplied arguments but expected $expected for $target"
550+
else {
551+
val oneOf =
552+
if (excess == 1) "one more argument"
553+
else if (excess > 0) s"$excess more arguments"
554+
else "too many arguments"
555+
s"$oneOf than can be applied to $target"
556+
}
557+
}
558+
val suppl =
559+
unknowns.size match {
560+
case 0 => ""
561+
case 1 => s"\nNote that '${unknowns.head}' is not a parameter name of the invoked method."
562+
case _ => unknowns.mkString("\nNote that '", "', '", "' are not parameter names of the invoked method.")
563+
}
564+
s"${badappl}${suppl}"
565+
}
566+
NormalTypeError(tree, msg)
567+
}
543568

544569
// can it still happen? see test case neg/overloaded-unapply.scala
545570
def OverloadedUnapplyError(tree: Tree) =
@@ -551,7 +576,7 @@ trait ContextErrors {
551576
def MultipleVarargError(tree: Tree) =
552577
NormalTypeError(tree, "when using named arguments, the vararg parameter has to be specified exactly once")
553578

554-
def ModuleUsingCompanionClassDefaultArgsErrror(tree: Tree) =
579+
def ModuleUsingCompanionClassDefaultArgsError(tree: Tree) =
555580
NormalTypeError(tree, "module extending its companion class cannot use default constructor arguments")
556581

557582
def NotEnoughArgsError(tree: Tree, fun: Tree, missing: List[Symbol]) = {

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,7 +3330,13 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
33303330
// #2064
33313331
duplErrorTree(WrongNumberOfArgsError(tree, fun))
33323332
} else if (lencmp > 0) {
3333-
tryTupleApply orElse duplErrorTree(TooManyArgsNamesDefaultsError(tree, fun))
3333+
tryTupleApply orElse duplErrorTree {
3334+
val (namelessArgs, _) = removeNames(Typer.this)(args, params)
3335+
val wrongs = (namelessArgs zip args) collect {
3336+
case (_: Assign, AssignOrNamedArg(Ident(name), _)) => name
3337+
}
3338+
TooManyArgsNamesDefaultsError(tree, fun, expected = formals.size, supplied = args.size, wrongs)
3339+
}
33343340
} else if (lencmp == 0) {
33353341
// we don't need defaults. names were used, so this application is transformed
33363342
// into a block (@see transformNamedApplication in NamesDefaults)
@@ -3394,7 +3400,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
33943400
val lencmp2 = compareLengths(allArgs, formals)
33953401

33963402
if (!sameLength(allArgs, args) && callToCompanionConstr(context, funSym)) {
3397-
duplErrorTree(ModuleUsingCompanionClassDefaultArgsErrror(tree))
3403+
duplErrorTree(ModuleUsingCompanionClassDefaultArgsError(tree))
33983404
} else if (lencmp2 > 0) {
33993405
removeNames(Typer.this)(allArgs, params) // #3818
34003406
duplErrTree

test/files/neg/eta-expand-star.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
eta-expand-star.scala:6: error: too many arguments for method apply: (v1: Seq[T])Unit in trait Function1
1+
eta-expand-star.scala:6: error: too many arguments (2) for method apply: (v1: Seq[T])Unit in trait Function1
22
g(1, 2)
33
^
44
one error found

test/files/neg/macro-invalidusage-badargs.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Macros_Test_2.scala:8: error: not enough arguments for macro method foo: (x: Int
1313
Unspecified value parameter x.
1414
foo()
1515
^
16-
Macros_Test_2.scala:9: error: too many arguments for macro method foo: (x: Int)Int
16+
Macros_Test_2.scala:9: error: too many arguments (2) for macro method foo: (x: Int)Int
1717
foo(4, 2)
1818
^
1919
5 errors found

test/files/neg/multi-array.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
multi-array.scala:7: error: too many arguments for constructor Array: (_length: Int)Array[T]
1+
multi-array.scala:7: error: too many arguments (2) for constructor Array: (_length: Int)Array[T]
22
val a: Array[Int] = new Array(10, 10)
33
^
44
one error found

test/files/neg/protected-constructors.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
protected-constructors.scala:17: error: too many arguments for constructor Foo1: ()dingus.Foo1
1+
protected-constructors.scala:17: error: no arguments allowed for nullary constructor Foo1: ()dingus.Foo1
22
val foo1 = new Foo1("abc")
33
^
44
protected-constructors.scala:18: error: constructor Foo2 in class Foo2 cannot be accessed in object P

test/files/neg/t1112.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
t1112.scala:12: error: too many arguments for method call: (p: Int)(f: => Test.this.Type1)Unit
1+
t1112.scala:12: error: too many arguments (2) for method call: (p: Int)(f: => Test.this.Type1)Unit
22
call(0,() => System.out.println("here we are"))
33
^
44
one error found

test/files/neg/t1523.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
t1523.scala:4: error: too many arguments for method bug: (x: Any)Any
1+
t1523.scala:4: error: 25 more arguments than can be applied to method bug: (x: Any)Any
22
def go() = bug("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a")
33
^
44
one error found

test/files/neg/t6920.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
t6920.scala:9: error: too many arguments for method applyDynamicNamed: (values: Seq[(String, Any)])String
1+
t6920.scala:9: error: too many arguments (2) for method applyDynamicNamed: (values: Seq[(String, Any)])String
22
error after rewriting to CompilerError.this.test.applyDynamicNamed("crushTheCompiler")(scala.Tuple2("a", 1), scala.Tuple2("b", 2))
33
possible cause: maybe a wrong Dynamic method signature?
44
test.crushTheCompiler(a = 1, b = 2)

test/files/neg/t7157.check

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
Test_2.scala:5: error: too many arguments for macro method m1_0_0: ()Unit
1+
Test_2.scala:5: error: no arguments allowed for nullary macro method m1_0_0: ()Unit
22
m1_0_0(1)
33
^
4-
Test_2.scala:6: error: too many arguments for macro method m1_0_0: ()Unit
4+
Test_2.scala:6: error: no arguments allowed for nullary macro method m1_0_0: ()Unit
55
m1_0_0(1, 2)
66
^
7-
Test_2.scala:7: error: too many arguments for macro method m1_0_0: ()Unit
7+
Test_2.scala:7: error: no arguments allowed for nullary macro method m1_0_0: ()Unit
88
m1_0_0(1, 2, 3)
99
^
1010
Test_2.scala:9: error: not enough arguments for macro method m1_1_1: (x: Int)Unit.
1111
Unspecified value parameter x.
1212
m1_1_1()
1313
^
14-
Test_2.scala:11: error: too many arguments for macro method m1_1_1: (x: Int)Unit
14+
Test_2.scala:11: error: too many arguments (2) for macro method m1_1_1: (x: Int)Unit
1515
m1_1_1(1, 2)
1616
^
17-
Test_2.scala:12: error: too many arguments for macro method m1_1_1: (x: Int)Unit
17+
Test_2.scala:12: error: too many arguments (3) for macro method m1_1_1: (x: Int)Unit
1818
m1_1_1(1, 2, 3)
1919
^
2020
Test_2.scala:14: error: not enough arguments for macro method m1_2_2: (x: Int, y: Int)Unit.
@@ -25,7 +25,7 @@ Test_2.scala:15: error: not enough arguments for macro method m1_2_2: (x: Int, y
2525
Unspecified value parameter y.
2626
m1_2_2(1)
2727
^
28-
Test_2.scala:17: error: too many arguments for macro method m1_2_2: (x: Int, y: Int)Unit
28+
Test_2.scala:17: error: too many arguments (3) for macro method m1_2_2: (x: Int, y: Int)Unit
2929
m1_2_2(1, 2, 3)
3030
^
3131
Test_2.scala:24: error: not enough arguments for macro method m1_1_inf: (x: Int, y: Int*)Unit.
@@ -40,23 +40,23 @@ Test_2.scala:30: error: not enough arguments for macro method m1_2_inf: (x: Int,
4040
Unspecified value parameters y, z.
4141
m1_2_inf(1)
4242
^
43-
Test_2.scala:35: error: too many arguments for macro method m2_0_0: ()Unit
43+
Test_2.scala:35: error: no arguments allowed for nullary macro method m2_0_0: ()Unit
4444
m2_0_0()(1)
4545
^
46-
Test_2.scala:36: error: too many arguments for macro method m2_0_0: ()Unit
46+
Test_2.scala:36: error: no arguments allowed for nullary macro method m2_0_0: ()Unit
4747
m2_0_0()(1, 2)
4848
^
49-
Test_2.scala:37: error: too many arguments for macro method m2_0_0: ()Unit
49+
Test_2.scala:37: error: no arguments allowed for nullary macro method m2_0_0: ()Unit
5050
m2_0_0()(1, 2, 3)
5151
^
5252
Test_2.scala:39: error: not enough arguments for macro method m2_1_1: (x: Int)Unit.
5353
Unspecified value parameter x.
5454
m2_1_1()()
5555
^
56-
Test_2.scala:41: error: too many arguments for macro method m2_1_1: (x: Int)Unit
56+
Test_2.scala:41: error: too many arguments (2) for macro method m2_1_1: (x: Int)Unit
5757
m2_1_1()(1, 2)
5858
^
59-
Test_2.scala:42: error: too many arguments for macro method m2_1_1: (x: Int)Unit
59+
Test_2.scala:42: error: too many arguments (3) for macro method m2_1_1: (x: Int)Unit
6060
m2_1_1()(1, 2, 3)
6161
^
6262
Test_2.scala:44: error: not enough arguments for macro method m2_2_2: (x: Int, y: Int)Unit.
@@ -67,7 +67,7 @@ Test_2.scala:45: error: not enough arguments for macro method m2_2_2: (x: Int, y
6767
Unspecified value parameter y.
6868
m2_2_2()(1)
6969
^
70-
Test_2.scala:47: error: too many arguments for macro method m2_2_2: (x: Int, y: Int)Unit
70+
Test_2.scala:47: error: too many arguments (3) for macro method m2_2_2: (x: Int, y: Int)Unit
7171
m2_2_2()(1, 2, 3)
7272
^
7373
Test_2.scala:54: error: not enough arguments for macro method m2_1_inf: (x: Int, y: Int*)Unit.

test/files/neg/t8006.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
t8006.scala:3: error: too many arguments for method applyDynamicNamed: (value: (String, Any))String
1+
t8006.scala:3: error: too many arguments (2) for method applyDynamicNamed: (value: (String, Any))String
22
error after rewriting to X.this.d.applyDynamicNamed("meth")(scala.Tuple2("value1", 10), scala.Tuple2("value2", 100))
33
possible cause: maybe a wrong Dynamic method signature?
44
d.meth(value1 = 10, value2 = 100) // two arguments here, but only one is allowed

test/files/neg/t8035-no-adapted-args.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ t8035-no-adapted-args.scala:4: warning: No automatic adaptation here: use explic
44
after adaptation: Test.f((1, 2, 3): (Int, Int, Int))
55
f(1, 2, 3)
66
^
7-
t8035-no-adapted-args.scala:4: error: too many arguments for method f: (x: (Int, Int, Int))Int
7+
t8035-no-adapted-args.scala:4: error: too many arguments (3) for method f: (x: (Int, Int, Int))Int
88
f(1, 2, 3)
99
^
1010
t8035-no-adapted-args.scala:5: warning: No automatic adaptation here: use explicit parentheses.

test/files/neg/t8667.check

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
t8667.scala:6: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
2+
Note that 'c' is not a parameter name of the invoked method.
3+
def c2 = new C(a = 42, b = 17, c = 5)
4+
^
5+
t8667.scala:7: error: unknown parameter name: c
6+
def c3 = new C(b = 42, a = 17, c = 5)
7+
^
8+
t8667.scala:7: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
9+
def c3 = new C(b = 42, a = 17, c = 5)
10+
^
11+
t8667.scala:8: error: positional after named argument.
12+
def c4 = new C(b = 42, a = 17, 5)
13+
^
14+
t8667.scala:8: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
15+
def c4 = new C(b = 42, a = 17, 5)
16+
^
17+
t8667.scala:9: error: not found: value c
18+
def c5 = new C(a = 42, c = 17)
19+
^
20+
t8667.scala:10: error: parameter 'b' is already specified at parameter position 2
21+
Note that 'c' is not a parameter name of the invoked method.
22+
def c6 = new C(a = 42, c = 17, b = 5)
23+
^
24+
t8667.scala:10: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
25+
Note that 'c' is not a parameter name of the invoked method.
26+
def c6 = new C(a = 42, c = 17, b = 5)
27+
^
28+
t8667.scala:11: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
29+
Note that 'c' is not a parameter name of the invoked method.
30+
def c7 = new C(42, 17, c = 5)
31+
^
32+
t8667.scala:12: error: parameter 'b' is already specified at parameter position 2
33+
def c8 = new C(42, 17, b = 5)
34+
^
35+
t8667.scala:12: error: too many arguments (3) for constructor C: (a: Int, b: Int)C
36+
def c8 = new C(42, 17, b = 5)
37+
^
38+
t8667.scala:13: error: parameter 'b' is already specified at parameter position 2
39+
Note that 'c' is not a parameter name of the invoked method.
40+
def c9 = new C(a = 42, c = 17, d = 3, b = 5)
41+
^
42+
t8667.scala:13: error: too many arguments (4) for constructor C: (a: Int, b: Int)C
43+
Note that 'c', 'd' are not parameter names of the invoked method.
44+
def c9 = new C(a = 42, c = 17, d = 3, b = 5)
45+
^
46+
t8667.scala:14: error: too many arguments (4) for constructor C: (a: Int, b: Int)C
47+
Note that 'd', 'c' are not parameter names of the invoked method.
48+
def c0 = new C(42, 17, d = 3, c = 5)
49+
^
50+
t8667.scala:24: error: no arguments allowed for nullary method f0: ()Int
51+
f0(1)
52+
^
53+
t8667.scala:25: error: too many arguments (2) for method f1: (i: Int)Int
54+
f1(1, 2)
55+
^
56+
t8667.scala:26: error: too many arguments (3) for method f1: (i: Int)Int
57+
f1(1, 2, 3)
58+
^
59+
t8667.scala:27: error: 3 more arguments than can be applied to method f1: (i: Int)Int
60+
f1(1, 2, 3, 4)
61+
^
62+
t8667.scala:28: error: 3 more arguments than can be applied to method f1: (i: Int)Int
63+
Note that 'j' is not a parameter name of the invoked method.
64+
f1(1, j = 2, 3, 4)
65+
^
66+
t8667.scala:29: error: 3 more arguments than can be applied to method f1: (i: Int)Int
67+
Note that 'j', 'k' are not parameter names of the invoked method.
68+
f1(1, j = 2, k = 3, 4)
69+
^
70+
t8667.scala:30: error: one more argument than can be applied to method f6: (i: Int, j: Int, k: Int, l: Int, m: Int, n: Int)Int
71+
f6(1, 2, 3, 4, 5, 6, 7)
72+
^
73+
t8667.scala:31: error: 2 more arguments than can be applied to method f6: (i: Int, j: Int, k: Int, l: Int, m: Int, n: Int)Int
74+
f6(1, 2, 3, 4, 5, 6, 7, 8)
75+
^
76+
t8667.scala:32: error: 15 arguments but expected 12 for method f12: (i: Int, j: Int, k: Int, l: Int, m: Int, n: Int, o: Int, p: Int, q: Int, r: Int, s: Int, t: Int)Int
77+
f12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
78+
^
79+
23 errors found

test/files/neg/t8667.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class C(a: Int, b: Int)
3+
4+
trait T {
5+
def c1 = new C(a = 42, b = 17)
6+
def c2 = new C(a = 42, b = 17, c = 5)
7+
def c3 = new C(b = 42, a = 17, c = 5)
8+
def c4 = new C(b = 42, a = 17, 5)
9+
def c5 = new C(a = 42, c = 17)
10+
def c6 = new C(a = 42, c = 17, b = 5)
11+
def c7 = new C(42, 17, c = 5)
12+
def c8 = new C(42, 17, b = 5)
13+
def c9 = new C(a = 42, c = 17, d = 3, b = 5)
14+
def c0 = new C(42, 17, d = 3, c = 5)
15+
}
16+
17+
trait X {
18+
def f0() = 42
19+
def f1(i: Int) = 42
20+
def f6(i: Int, j: Int, k: Int, l: Int, m: Int, n: Int) = 42
21+
def f12(i: Int, j: Int, k: Int, l: Int, m: Int, n: Int, o: Int, p: Int, q: Int, r: Int, s: Int, t: Int) = 42
22+
23+
def g() = {
24+
f0(1)
25+
f1(1, 2)
26+
f1(1, 2, 3)
27+
f1(1, 2, 3, 4)
28+
f1(1, j = 2, 3, 4)
29+
f1(1, j = 2, k = 3, 4)
30+
f6(1, 2, 3, 4, 5, 6, 7)
31+
f6(1, 2, 3, 4, 5, 6, 7, 8)
32+
f12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
33+
()
34+
}
35+
}

test/files/neg/t876.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
t876.scala:25: error: too many arguments for method apply: (key: AssertionError.A)manager.B in class HashMap
1+
t876.scala:25: error: too many arguments (2) for method apply: (key: AssertionError.A)manager.B in class HashMap
22
assert(manager.map(A2) == List(manager.map(A2, A1)))
33
^
44
one error found

0 commit comments

Comments
 (0)