diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index 618e1857902c..f60c618b91e9 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -105,6 +105,7 @@ public enum ErrorMessageID { ExpectedTypeBoundOrEqualsID, ClassAndCompanionNameClashID, TailrecNotApplicableID, + MissingEmptyArgumentListForMethodID, FailureToEliminateExistentialID, OnlyFunctionsCanBeFollowedByUnderscoreID ; diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index aec64be3468a..3d1fd540a4f2 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1847,4 +1847,35 @@ object messages { hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function. |To convert to a function value, you need to explicitly write ${"() => x"}""" } + + case class MissingEmptyArgumentListForMethod(method: Symbol, methodStr: String)(implicit ctx: Context) + extends Message(MissingEmptyArgumentListForMethodID) { + val kind: String = "Syntax" + val msg: String = hl"$methodStr must be called with an empty argument list" + val explanation: String = { + val codeExample = + """ + |def next(): T = ... + |next // is expanded to next() + """ + val errorMessage = + """ + |next + |^^^^ + |method next must be called with an empty argument list + """ + hl""" + |Previously an empty argument list () was implicitly inserted when calling a nullary method without arguments. E.g. + | + |$codeExample + | + |In Dotty, this idiom is an error which leads to the following message: + | + |$errorMessage + | + |In Dotty, the application syntax has to follow exactly the parameter syntax. + |Excluded from this rule are methods that are defined in Java or that override methods defined in Java. + """ + } + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 7c6250689d23..dabf87d214f4 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1908,7 +1908,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def methodStr = err.refStr(methPart(tree).tpe) def missingArgs(mt: MethodType) = { - ctx.error(em"missing arguments for $methodStr", tree.pos) + ctx.error(MissingEmptyArgumentListForMethod(methPart(tree).symbol, methodStr), tree.pos) tree.withType(mt.resultType) } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index e261c7e7c339..fa39fb26034a 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1052,6 +1052,25 @@ class ErrorMessagesTests extends ErrorMessagesTest { } + @Test def missingEmptyArgumentListForMethod = + checkMessagesAfter("frontend") { + """ + |class Test { + | def greet(): String = "Hello" + | def main(args: Array[String]): Unit = { + | greet + | } + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + + assertMessageCount(1, messages) + val MissingEmptyArgumentListForMethod(method, _) :: Nil = messages + assertEquals("method greet", method.show) + } + @Test def onlyFunctionsCanBeFollowedByUnderscore = checkMessagesAfter("frontend") { """