Skip to content

Commit b83a7f0

Browse files
maseevallanrenucci
authored andcommitted
Add error message for missing () argument list
1 parent 13c5455 commit b83a7f0

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public enum ErrorMessageID {
106106
ClassAndCompanionNameClashID,
107107
TailrecNotApplicableID,
108108
FailureToEliminateExistentialID,
109-
OnlyFunctionsCanBeFollowedByUnderscoreID
109+
OnlyFunctionsCanBeFollowedByUnderscoreID,
110+
MissingArgumentsForMethodID
110111
;
111112

112113
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,4 +1847,35 @@ object messages {
18471847
hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function.
18481848
|To convert to a function value, you need to explicitly write ${"() => x"}"""
18491849
}
1850+
1851+
case class MissingEmptyArgumentListForMethod(method: Symbol, methodStr: String)(implicit ctx: Context)
1852+
extends Message(MissingArgumentsForMethodID) {
1853+
val kind: String = "Syntax"
1854+
val msg: String = hl"$methodStr must be called with an empty argument list"
1855+
val explanation: String = {
1856+
val codeExample =
1857+
"""
1858+
|def next(): T = ...
1859+
|next // is expanded to next()
1860+
"""
1861+
val errorMessage =
1862+
"""
1863+
|next
1864+
|^^^^
1865+
|method next must be called with an empty argument list
1866+
"""
1867+
hl"""
1868+
|Previously an empty argument list () was implicitly inserted when calling a nullary method without arguments. E.g.
1869+
|
1870+
|$codeExample
1871+
|
1872+
|In Dotty, this idiom is an error which leads to the following message:
1873+
|
1874+
|$errorMessage
1875+
|
1876+
|In Dotty, the application syntax has to follow exactly the parameter syntax.
1877+
|Excluded from this rule are methods that are defined in Java or that override methods defined in Java.
1878+
"""
1879+
}
1880+
}
18501881
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
19081908
def methodStr = err.refStr(methPart(tree).tpe)
19091909

19101910
def missingArgs(mt: MethodType) = {
1911-
ctx.error(em"missing arguments for $methodStr", tree.pos)
1911+
ctx.error(MissingEmptyArgumentListForMethod(methPart(tree).symbol, methodStr), tree.pos)
19121912
tree.withType(mt.resultType)
19131913
}
19141914

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,4 +1069,25 @@ class ErrorMessagesTests extends ErrorMessagesTest {
10691069
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
10701070
assertEquals("String(n)", pt.show)
10711071
}
1072+
1073+
@Test def missingEmptyArgumentListForMethod =
1074+
checkMessagesAfter("frontend") {
1075+
"""
1076+
|class Test {
1077+
| def greet(): String = "Hello"
1078+
| def main(args: Array[String]): Unit = {
1079+
| greet
1080+
| }
1081+
|}
1082+
""".stripMargin
1083+
}
1084+
.expect { (ictx, messages) =>
1085+
implicit val ctx: Context = ictx
1086+
1087+
assertMessageCount(1, messages)
1088+
1089+
val MissingEmptyArgumentListForMethod(method, _) :: Nil = messages
1090+
1091+
assertEquals("method greet", method.show)
1092+
}
10721093
}

0 commit comments

Comments
 (0)