Skip to content

Add error message IdentifierExpected #1633

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

Merged
Merged
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
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -230,7 +231,7 @@ object JavaParsers {
case AppliedTypeTree(_, _) | Select(_, _) =>
tree
case _ =>
syntaxError("identifier expected", tree.pos)
syntaxError(IdentifierExpected(tree.show), tree.pos)
errorTypeTree
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
24 changes: 24 additions & 0 deletions src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I have a couple of comments/questions on this explanation:

  • Does it make sense for all cases?
  • Could you show the user their code and the correct way to rewrite it? E.g: def foo: int = { ... } => def foo = { ... }
  • There are some weird things when it comes to the language like "Because of Scala's stronger type" - wat? Have a look through it and I'm sure you'll find places to correct :)
  • The line breaking is increasing, first line is something like 60 chars, the second 65 and third 90+, try to even it out.
  • The line with "Here we explicitly" is too long (84 chars). We want a line to be max 80 chars (the standard terminal width).

Copy link
Contributor Author

@iamthiago iamthiago Oct 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @felixmulder thanks for your comments. I'm going through your cases and explain to you what I thought right?

1 - Actually, to be honest, the only piece of code I could reproduce this error was this:

def foo: this = "bar"

and it goes to the Parsers class.

Is there any other case I could reproduce it, so I can make sure nothing was left behind?

2 - Get the user code, you mean I could use the Tree to show the user wrong code and give the correct one? In fact I could pass the Tree inside the class and use it. Don't know how to play with Trees but I will figure out.

3 - yeah, that one was difficult to write. I mean, Scala has a strong type system, which is good, everything has a type where it could be inferred by the compiler or not(in case the user provided it).

4 & 5 - No problem :)

}