Skip to content

Commit

Permalink
Improve missing argument list error
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Mar 17, 2023
1 parent 2dec682 commit fdbc125
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case InlineGivenShouldNotBeFunctionID // errorNumber 174
case ValueDiscardingID // errorNumber 175
case UnusedNonUnitValueID // errorNumber 176
case MissingArgumentListID // errorNumber: 177

def errorNumber = ordinal - 1

Expand Down
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,22 @@ class MissingEmptyArgumentList(method: String)(using Context)
}
}

class MissingArgumentList(method: String, sym: Symbol)(using Context)
extends SyntaxMsg(MissingArgumentListID) {
def msg(using Context) =
def paramsStr(tp: Type): String = tp match
case tp: MethodType if !tp.isImplicitMethod =>
tp.paramNames.zip(tp.paramInfos)
.map((name, tpe) => name.show + ": " + tpe.show)
.mkString("(", ", ", ")" + paramsStr(tp.resType))
case tp: MethodOrPoly => paramsStr(tp.resType)
case _ => ""
i"missing argument list for $method\n${sym} must be called with arguments ${hl(paramsStr(sym.info))}"
def explain(using Context) = {
i"""Unapplied methods are only converted to functions when a function type is expected."""
}
}

class DuplicateNamedTypeParameter(name: Name)(using Context)
extends SyntaxMsg(DuplicateNamedTypeParameterID) {
def msg(using Context) = i"Type parameter $name was defined multiple times."
Expand Down
20 changes: 15 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ object ErrorReporting {
errorType(WrongNumberOfTypeArgs(fntpe, expectedArgs, actual), pos)

def missingArgs(tree: Tree, mt: Type)(using Context): Unit =
def isCallableWithoutArgumentsLists(mt: Type): Boolean = mt match
case pt: PolyType => isCallableWithoutArgumentsLists(pt.resType)
case mt: MethodType if mt.isImplicitMethod => isCallableWithoutArgumentsLists(mt.resType)
case mt: MethodType => false
case _ => true
def isCallableWithSingleEmptyArgumentList(mt: Type): Boolean =
mt match
case mt: MethodType if mt.paramNames.isEmpty => isCallableWithoutArgumentsLists(mt.resType)
case mt: MethodType if mt.isImplicitMethod => isCallableWithSingleEmptyArgumentList(mt.resType)
case pt: PolyType => isCallableWithSingleEmptyArgumentList(pt.resType)
case _ => false
val meth = err.exprStr(methPart(tree))
mt match
case mt: MethodType if mt.paramNames.isEmpty =>
report.error(MissingEmptyArgumentList(meth), tree.srcPos)
case _ =>
report.error(em"missing arguments for $meth", tree.srcPos)
if isCallableWithSingleEmptyArgumentList(tree.symbol.info) then
report.error(MissingEmptyArgumentList(meth), tree.srcPos)
else
report.error(MissingArgumentList(meth, tree.symbol), tree.srcPos)

def matchReductionAddendum(tps: Type*)(using Context): String =
val collectMatchTrace = new TypeAccumulator[String]:
Expand Down
76 changes: 76 additions & 0 deletions tests/neg/i17123.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- [E100] Syntax Error: tests/neg/i17123.scala:7:2 ---------------------------------------------------------------------
7 | m1 // error
| ^^
| method m1 in object ConfusingErrorMessage must be called with () argument
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:9:2 ---------------------------------------------------------------------
9 | m2 // error
| ^^
| missing argument list for method m2 in object ConfusingErrorMessage
| method m2 must be called with arguments ()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:10:4 --------------------------------------------------------------------
10 | m2() // error
| ^^^^
| missing argument list for method m2 in object ConfusingErrorMessage
| method m2 must be called with arguments ()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:11:2 --------------------------------------------------------------------
11 | m3 // error
| ^^
| missing argument list for method m3 in object ConfusingErrorMessage
| method m3 must be called with arguments ()()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:12:4 --------------------------------------------------------------------
12 | m3() // error
| ^^^^
| missing argument list for method m3 in object ConfusingErrorMessage
| method m3 must be called with arguments ()()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:13:6 --------------------------------------------------------------------
13 | m3()() // error
| ^^^^^^
| missing argument list for method m3 in object ConfusingErrorMessage
| method m3 must be called with arguments ()()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:15:2 --------------------------------------------------------------------
15 | f3 // error
| ^^
| missing argument list for method f3 in object ConfusingErrorMessage
| method f3 must be called with arguments ()(i: Int)()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:16:2 --------------------------------------------------------------------
16 | f3() // error
| ^^^^
| missing argument list for method f3 in object ConfusingErrorMessage
| method f3 must be called with arguments ()(i: Int)()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:17:6 --------------------------------------------------------------------
17 | f3()(2) // error
| ^^^^^^^
| missing argument list for method f3 in object ConfusingErrorMessage
| method f3 must be called with arguments ()(i: Int)()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:19:2 --------------------------------------------------------------------
19 | i3 // error
| ^^
| missing argument list for method i3 in object ConfusingErrorMessage
| method i3 must be called with arguments ()()
|
| longer explanation available when compiling with `-explain`
-- [E177] Syntax Error: tests/neg/i17123.scala:20:2 --------------------------------------------------------------------
20 | i3() // error
| ^^^^
| missing argument list for method i3 in object ConfusingErrorMessage
| method i3 must be called with arguments ()()
|
| longer explanation available when compiling with `-explain`
22 changes: 22 additions & 0 deletions tests/neg/i17123.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
object ConfusingErrorMessage {
def m1() = ()
def m2()() = ()
def m3()()() = ()
def f3()(i: Int)() = ()
def i3()(using d: DummyImplicit)() = ()
m1 // error
m1()
m2 // error
m2() // error
m3 // error
m3() // error
m3()() // error
m3()()()
f3 // error
f3() // error
f3()(2) // error
f3()(2)()
i3 // error
i3() // error
i3()()
}

0 comments on commit fdbc125

Please sign in to comment.