diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index 5f5cbc5625bc..ef97720368ce 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -57,6 +57,7 @@ public enum ErrorMessageID { CyclicReferenceInvolvingImplicitID, SuperQualMustBeParentID, AmbiguousImportID, + MethodDoesNotTakeParametersId, AmbiguousOverloadID, ReassignmentToValID, ; diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 3d9cde9235ae..f18c7139f408 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1251,6 +1251,30 @@ object messages { |""" } + case class MethodDoesNotTakeParameters(tree: tpd.Tree, methPartType: Types.Type)(err: typer.ErrorReporting.Errors)(implicit ctx: Context) + extends Message(MethodDoesNotTakeParametersId) { + private val more = tree match { + case Apply(_, _) => " more" + case _ => "" + } + + val msg = hl"${err.refStr(methPartType)} does not take$more parameters" + + val kind = "Reference" + + private val noParameters = if (methPartType.widenSingleton.isInstanceOf[ExprType]) + hl"""|As ${err.refStr(methPartType)} is defined without parenthesis, you may + |not use any at call-site, either. + |""" + else + "" + + val explanation = + s"""|You have specified more parameter lists as defined in the method definition(s). + |$noParameters""".stripMargin + + } + case class AmbiguousOverload(tree: tpd.Tree, alts: List[SingleDenotation], pt: Type)( err: typer.ErrorReporting.Errors)( implicit ctx: Context) @@ -1283,5 +1307,4 @@ object messages { | ${"var"} $name ${"="} ... |""".stripMargin } - } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 6c481e5e462e..69140fcd0204 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1834,11 +1834,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit else tree case _ => tryInsertApplyOrImplicit(tree, pt) { - val more = tree match { - case Apply(_, _) => " more" - case _ => "" - } - errorTree(tree, em"$methodStr does not take$more parameters") + errorTree(tree, MethodDoesNotTakeParameters(tree, methPart(tree).tpe)(err)) } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 20c5e2905f06..cc0c53aad017 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -360,6 +360,46 @@ class ErrorMessagesTests extends ErrorMessagesTest { assertEquals(namedImport, prevPrec) } + @Test def methodDoesNotTakePrameters = + checkMessagesAfter("frontend") { + """ + |object Scope{ + | def foo = () + | foo() + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + val defn = ictx.definitions + + assertMessageCount(1, messages) + val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages + + assertEquals("Scope.foo", tree.show) + assertEquals("=> Unit(Scope.foo)", methodPart.show) + } + + @Test def methodDoesNotTakeMorePrameters = + checkMessagesAfter("frontend") { + """ + |object Scope{ + | def foo(a: Int) = () + | foo(1)("2") + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + val defn = ictx.definitions + + assertMessageCount(1, messages) + val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages + + assertEquals("Scope.foo(1)", tree.show) + assertEquals("((a: Int)Unit)(Scope.foo)", methodPart.show) + } + @Test def ambiugousOverloadWithWildcard = checkMessagesAfter("frontend") { """object Context {