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

Improve error when missing a comma in Apply args #18734

Closed
hugo-vrijswijk opened this issue Oct 20, 2023 · 1 comment · Fixed by #18785
Closed

Improve error when missing a comma in Apply args #18734

hugo-vrijswijk opened this issue Oct 20, 2023 · 1 comment · Fixed by #18785
Assignees
Labels
area:parser area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@hugo-vrijswijk
Copy link
Contributor

Compiler version

3.3.1

Minimized example

case class Foo(a: Int, b: Int)

object Bar:
  Foo(1 2) 

  // Or
  Foo(
    a = 1
    b = 2
  )

Output Error/Warning message

-- [E040] Syntax Error: /Users/hvanrijswijk1/ws/explore/Foo.scala:4:8 ----------
4 |  Foo(1 2) 
  |        ^
  |        ')' expected, but integer literal found

When using named arguments the error is even more confusing:

-- [E018] Syntax Error: /Users/hvanrijswijk1/ws/explore/Foo.scala:8:6 ----------
8 |    b = 2
  |      ^
  |      expression expected but = found
  |
  | longer explanation available when compiling with `-explain`
-- [E008] Not Found Error: /Users/hvanrijswijk1/ws/explore/Foo.scala:8:4 -------
7 |    a = 1
8 |    b = 2
  |        ^
  |        value b is not a member of Int.
  |        Note that `b` is treated as an infix operator in Scala 3.
  |        If you do not want that, insert a `;` or empty line in front
  |        or drop any spaces behind the operator.

Why this Error/Warning was not helpful

Following the compiler suggestion and adding a ) would just lead to another compile error, as it is expecting more arguments. It's much more likely that a , is missing.

For named arguments the first error is similar to the first when not using named arguments, but the second one is more confusing, as the suggestion of a ; does not work, nor does adding an empty line (it is already there!)

Suggested improvement

Something similar to what TypeScript does:

foo(1 2)

Error:

',' expected.

This should also work for named arguments:

Foo(
  a = 1
  b = 2
)
@hugo-vrijswijk hugo-vrijswijk added area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 20, 2023
@nicolasstucki nicolasstucki added area:parser and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 20, 2023
@nicolasstucki
Copy link
Contributor

Compiling with -Xprint:parser

-- [E018] Syntax Error: t/Test.scala:9:6 ---------------------------------------
9 |    b = 2
  |      ^
  |      expression expected but = found
  |
  | longer explanation available when compiling with `-explain`
[[syntax trees at end of                    parser]] // t/Test.scala
package <empty> {
  case class Foo(val a: Int, val b: Int) {}
  module object Bar {
    Foo(1)
    Foo(a = 1 b null)
  }
}

@odersky odersky self-assigned this Oct 28, 2023
odersky added a commit that referenced this issue Oct 30, 2023
Three measures:

1. Classify an id as infix operator only if following can start an
operand
 2. Detect and report spread operators in illegal positions
3. Mention `,` in addition to `)` or `]` in error messages when a `,`
could have been missing

Fixes #18734
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:parser 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.

3 participants