Skip to content

Commit

Permalink
Merge pull request #3407 from allanrenucci/err-msg-missing-empty-arg-…
Browse files Browse the repository at this point in the history
…list

 Add error message for missing () argument list
  • Loading branch information
allanrenucci committed Oct 30, 2017
2 parents 13c5455 + 0af2bd8 commit 6398448
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import util.Property
import collection.mutable
import ast.tpd._
import reporting.trace
import reporting.diagnostic.Message

trait TypeOps { this: Context => // TODO: Make standalone object.

Expand Down Expand Up @@ -315,7 +316,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
def dynamicsEnabled =
featureEnabled(defn.LanguageModuleClass, nme.dynamics)

def testScala2Mode(msg: => String, pos: Position, rewrite: => Unit = ()) = {
def testScala2Mode(msg: => Message, pos: Position, rewrite: => Unit = ()) = {
if (scala2Mode) {
migrationWarning(msg, pos)
rewrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public enum ErrorMessageID {
ClassAndCompanionNameClashID,
TailrecNotApplicableID,
FailureToEliminateExistentialID,
OnlyFunctionsCanBeFollowedByUnderscoreID
OnlyFunctionsCanBeFollowedByUnderscoreID,
MissingEmptyArgumentListID
;

public int errorNumber() {
Expand Down
18 changes: 18 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,22 @@ 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 MissingEmptyArgumentList(method: Symbol)(implicit ctx: Context)
extends Message(MissingEmptyArgumentListID) {
val kind = "Syntax"
val msg = hl"$method must be called with ${"()"} argument"
val explanation = {
val codeExample =
"""def next(): T = ...
|next // is expanded to next()"""

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. 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."""
}
}
}
4 changes: 2 additions & 2 deletions 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(MissingEmptyArgumentList(methPart(tree).symbol), tree.pos)
tree.withType(mt.resultType)
}

Expand Down Expand Up @@ -2076,7 +2076,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def isAutoApplied(sym: Symbol): Boolean = {
sym.isConstructor ||
sym.matchNullaryLoosely ||
ctx.testScala2Mode(em"${sym.showLocated} requires () argument", tree.pos,
ctx.testScala2Mode(MissingEmptyArgumentList(sym), tree.pos,
patch(tree.pos.endPos, "()"))
}

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 @@ -1069,4 +1069,23 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
assertEquals("String(n)", pt.show)
}

@Test def missingEmptyArgumentList =
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 MissingEmptyArgumentList(method) :: Nil = messages
assertEquals("method greet", method.show)
}
}

0 comments on commit 6398448

Please sign in to comment.