From 1c0e48fe8cd0b44bfcfbd3705c4f7fcbe448a5be Mon Sep 17 00:00:00 2001 From: Thiago Pereira Date: Tue, 25 Oct 2016 00:08:37 -0200 Subject: Add error message IdentifierExpected This commit adds the semantic object fir the ```identifier expected``` error. It is part of the https://github.com/lampepfl/dotty/issues/1589 --- src/dotty/tools/dotc/parsing/JavaParsers.scala | 3 ++- src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 24 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/parsing/JavaParsers.scala b/src/dotty/tools/dotc/parsing/JavaParsers.scala index 0be4226ed..b6a423dc7 100644 --- a/src/dotty/tools/dotc/parsing/JavaParsers.scala +++ b/src/dotty/tools/dotc/parsing/JavaParsers.scala @@ -21,6 +21,7 @@ import Symbols._ import ast.Trees._ import Decorators._ import StdNames._ +import dotty.tools.dotc.reporting.diagnostic.messages.IdentifierExpected import dotty.tools.dotc.util.SourceFile import util.Positions._ import annotation.switch @@ -230,7 +231,7 @@ object JavaParsers { case AppliedTypeTree(_, _) | Select(_, _) => tree case _ => - syntaxError("identifier expected", tree.pos) + syntaxError(IdentifierExpected(tree.show), tree.pos) errorTypeTree } } diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index 754db3d3b..4926ebb9f 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -316,7 +316,7 @@ object Parsers { case id @ Select(qual, name) => cpy.Select(id)(qual, name.toTypeName) case _ => - syntaxError("identifier expected", tree.pos) + syntaxError(IdentifierExpected(tree.show), tree.pos) tree } diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 67d53fd22..bec49e728 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -691,4 +691,28 @@ object messages { |${s"trait A[${mods.flags} type $identifier]"} |""".stripMargin } + + case class IdentifierExpected(identifier: String)(implicit ctx: Context) extends Message(25) { + val kind = "Syntax" + + val msg = "identifier expected" + + val wrongIdentifier = s"def foo: $identifier = {...}" + + val validIdentifier = s"def foo = {...}" + + val explanation = { + hl"""|A valid identifier expected, but `$identifier` found. + |Let the compiler infer the type for you. + |For example, instead of: + | + |$wrongIdentifier + | + |Write your code like: + | + |$validIdentifier + | + |""".stripMargin + } + } } -- cgit v1.2.3