Skip to content
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

poor examples in "pattern expected" explanation with val def, (also do as name causes the problem) #18750

Closed
bishabosha opened this issue Oct 24, 2023 · 0 comments · Fixed by #18752
Assignees
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@bishabosha
Copy link
Member

bishabosha commented Oct 24, 2023

Compiler version

3.3.0

Minimized example

val do = 23

Output Error/Warning message

-- [E032] Syntax Error: --------------------------------------------------------
1 |val do = 23
  |    ^^
  |    pattern expected
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Simple patterns can be divided into several groups:
  | - Variable Patterns: case x => ....
  |   It matches any value, and binds the variable name to that value.
  |   A special case is the wild-card pattern _ which is treated as if it was a fresh
  |   variable on each occurrence.
  |
  | - Typed Patterns: case x: Int => ... or case _: Int => ....
  |   This pattern matches any value matched by the specified type; it binds the variable
  |   name to that value.
  |
  | - Literal Patterns: case 123 => ... or case 'A' => ....
  |   This type of pattern matches any value that is equal to the specified literal.
  |
  | - Stable Identifier Patterns:
  |
  |   def f(x: Int, y: Int) = x match {
  |         |  case `y` => ...
  |         |}
  |       
  |
  |   the match succeeds only if the x argument and the y argument of f are equal.
  |
  | - Constructor Patterns:
  |
  |   case class Person(name: String, age: Int)
  |         |
  |         |def test(p: Person) = p match {
  |         |  case Person(name, age) => ...
  |         |}
  |       
  |
  |   The pattern binds all object's fields to the variable names (name and age, in this
  |   case).
  |
  | - Tuple Patterns:
  |
  |   def swap(tuple: (String, Int)): (Int, String) = tuple match {
  |         |  case (text, number) => (number, text)
  |         |}
  |       
  |
  |   Calling:
  |
  |   swap(("Luftballons", 99)
  |
  |   would give (99, "Luftballons") as a result.
  |
  | - Pattern Sequences:
  |
  |   def getSecondValue(list: List[Int]): Int = list match {
  |         |  case List(_, second, x:_*) => second
  |         |  case _ => 0
  |         |}
  |
  |   Calling:
  |
  |   getSecondValue(List(1, 10, 2))
  |
  |   would give 10 as a result.
  |   This pattern is possible because a companion object for the List class has a method
  |   with the following signature:
  |
  |   def unapplySeq[A](x: List[A]): Some[List[A]]
   -----------------------------------------------------------------------------

Why this Error/Warning was not helpful

three things:

  1. I am putting a pattern in a val def, and none of the examples show how to do that, instead only in a match case.
  2. perhaps the user doesn't know that do is a keyword, so this might cause confusion when they expected to define an ordinary value.
  3. the code examples in the explanations are not formatted correctly, so vertical bars are duplicated.

Suggested improvements

  1. maybe conditionally show examples based on the position of the pattern
  2. identify that do is a keyword, so can't be used as a name
  3. fix the formatting of examples.
@bishabosha bishabosha added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc stat:needs triage Every issue needs to have an "area" and "itype" label better-errors Issues concerned with improving confusing/unhelpful diagnostic messages and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 24, 2023
@nicolasstucki nicolasstucki self-assigned this Oct 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 24, 2023
WojciechMazur added a commit that referenced this issue Jun 22, 2024
WojciechMazur added a commit that referenced this issue Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants