Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 96b012a

Browse files
committed
Improved message for missing argument list
Clarifies the language and rules for eta-expansion. A missing argument in a list, as opposed to a missing argument list, results in a different message. The comical expansion in parens does not attempt to show what was already applied, but succeeds in showing at a glance the shape of the method in question. ``` scala> def m(i: Int, j: Int)(x: Int) = ??? m: (i: Int, j: Int)(x: Int)Nothing scala> m <console>:12: error: missing argument list for method m Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing `m _` or `m(_,_)(_)` instead of `m`. m ^ ``` The original submission was due to sschaef and the wording due to adriaanm, with a minor tweak.
1 parent 5b80736 commit 96b012a

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -550,23 +550,18 @@ trait ContextErrors {
550550
def ModuleUsingCompanionClassDefaultArgsErrror(tree: Tree) =
551551
NormalTypeError(tree, "module extending its companion class cannot use default constructor arguments")
552552

553-
def NotEnoughArgsError(tree: Tree, fun0: Tree, missing0: List[Symbol]) = {
554-
def notEnoughArgumentsMsg(fun: Tree, missing: List[Symbol]) = {
555-
val suffix = {
556-
if (missing.isEmpty) ""
557-
else {
558-
val keep = missing take 3 map (_.name)
559-
".\nUnspecified value parameter%s %s".format(
560-
if (missing.tail.isEmpty) "" else "s",
561-
if ((missing drop 3).nonEmpty) (keep :+ "...").mkString(", ")
562-
else keep.mkString("", ", ", ".")
563-
)
564-
}
553+
def NotEnoughArgsError(tree: Tree, fun: Tree, missing: List[Symbol]) = {
554+
val notEnoughArgumentsMsg = {
555+
val suffix = if (missing.isEmpty) "" else {
556+
val keep = missing take 3 map (_.name)
557+
val ess = if (missing.tail.isEmpty) "" else "s"
558+
f".%nUnspecified value parameter$ess ${
559+
keep.mkString("", ", ", if ((missing drop 3).nonEmpty) "..." else ".")
560+
}"
565561
}
566-
567-
"not enough arguments for " + treeSymTypeMsg(fun) + suffix
562+
s"not enough arguments for ${ treeSymTypeMsg(fun) }$suffix"
568563
}
569-
NormalTypeError(tree, notEnoughArgumentsMsg(fun0, missing0))
564+
NormalTypeError(tree, notEnoughArgumentsMsg)
570565
}
571566

572567
//doTypedApply - patternMode
@@ -632,12 +627,16 @@ trait ContextErrors {
632627

633628
//adapt
634629
def MissingArgsForMethodTpeError(tree: Tree, meth: Symbol) = {
630+
val f = meth.name
631+
val paf = s"$f(${ meth.asMethod.paramLists map (_ map (_ => "_") mkString ",") mkString ")(" })"
632+
val advice = s"""
633+
|Unapplied methods are only converted to functions when a function type is expected.
634+
|You can make this conversion explicit by writing `$f _` or `$paf` instead of `$f`.""".stripMargin
635635
val message =
636636
if (meth.isMacro) MacroTooFewArgumentListsMessage
637-
else "missing arguments for " + meth.fullLocationString + (
638-
if (meth.isConstructor) ""
639-
else ";\nfollow this method with `_' if you want to treat it as a partially applied function"
640-
)
637+
else s"""missing argument list for ${meth.fullLocationString}${
638+
if (!meth.isConstructor) advice else ""
639+
}"""
641640
issueNormalTypeError(tree, message)
642641
setError(tree)
643642
}

test/files/neg/macro-invalidshape.check

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ macro [<static object>].<method name>[[<type args>]] or
88
macro [<macro bundle>].<method name>[[<type args>]]
99
def foo2(x: Any) = macro Impls.foo(null)(null)
1010
^
11-
Macros_Test_2.scala:4: error: missing arguments for method foo in object Impls;
12-
follow this method with `_' if you want to treat it as a partially applied function
11+
Macros_Test_2.scala:4: error: missing argument list for method foo in object Impls
12+
Unapplied methods are only converted to functions when a function type is expected.
13+
You can make this conversion explicit by writing `foo _` or `foo(_)(_)` instead of `foo`.
1314
def foo3(x: Any) = macro {2; Impls.foo}
1415
^
1516
Macros_Test_2.scala:7: error: macro implementation reference has wrong shape. required:

test/files/neg/missing-arg-list.check

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
missing-arg-list.scala:9: error: missing argument list for method id in trait T
2+
Unapplied methods are only converted to functions when a function type is expected.
3+
You can make this conversion explicit by writing `id _` or `id(_)` instead of `id`.
4+
val w = id
5+
^
6+
missing-arg-list.scala:10: error: missing argument list for method f in trait T
7+
Unapplied methods are only converted to functions when a function type is expected.
8+
You can make this conversion explicit by writing `f _` or `f(_)(_)` instead of `f`.
9+
val x = f
10+
^
11+
missing-arg-list.scala:11: error: missing argument list for method g in trait T
12+
Unapplied methods are only converted to functions when a function type is expected.
13+
You can make this conversion explicit by writing `g _` or `g(_,_,_)` instead of `g`.
14+
val y = g
15+
^
16+
missing-arg-list.scala:12: error: missing argument list for method h in trait T
17+
Unapplied methods are only converted to functions when a function type is expected.
18+
You can make this conversion explicit by writing `h _` or `h(_,_,_)(_)` instead of `h`.
19+
val z = h
20+
^
21+
four errors found

test/files/neg/missing-arg-list.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
trait T {
3+
4+
def id(i: Int) = i
5+
def f(i: Int)(j: Int) = i+j
6+
def g(i: Int, j: Int, k: Int) = i+j+k
7+
def h(i: Int, j: Int, k: Int)(implicit s: String) = s*(i+j+k)
8+
9+
val w = id
10+
val x = f
11+
val y = g
12+
val z = h
13+
}

0 commit comments

Comments
 (0)