Skip to content

Add an error message class for the method's missing arguments #3394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public enum ErrorMessageID {
ExpectedTypeBoundOrEqualsID,
ClassAndCompanionNameClashID,
TailrecNotApplicableID,
MissingEmptyArgumentListForMethodID,
FailureToEliminateExistentialID,
OnlyFunctionsCanBeFollowedByUnderscoreID
;
Expand Down
31 changes: 31 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
}
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
19 changes: 19 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
"""
Expand Down