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

Report a clearer error message for unexpected final modifier #17579

Closed
som-snytt opened this issue May 24, 2023 · 7 comments · Fixed by #20557
Closed

Report a clearer error message for unexpected final modifier #17579

som-snytt opened this issue May 24, 2023 · 7 comments · Fixed by #20557
Assignees
Labels
area:repl area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement Spree Suitable for a future Spree
Milestone

Comments

@som-snytt
Copy link
Contributor

Welcome to Scala 3.3.1-RC1-bin-SNAPSHOT-git-5262680 (20.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> final val x = 42
-- [E088] Syntax Error: ------------------------------------------------------------------------------------------------
1 |final val x = 42
  |      ^^^
  |      Expected start of definition
  |
  | longer explanation available when compiling with `-explain`

Expectation

That seems like a natural thing to do in the REPL.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 24, 2023
@szymon-rd szymon-rd added area:repl and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 29, 2023
@mbovel
Copy link
Member

mbovel commented Jun 10, 2024

Scala 2:

$ scala-cli repl -S 2.13
Welcome to Scala 2.13.14 (OpenJDK 64-Bit Server VM, Java 17.0.8).
Type in expressions for evaluation. Or try :help.

scala> final val x = 42
val x: Int(42) = 42

scala> val x = 42
val x: Int = 42

Scala 3:

$ scala-cli repl -S 3.4
Welcome to Scala 3.4.2 (17.0.8, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                
scala> final val x = 42
-- [E088] Syntax Error: --------------------------------------------------------
1 |final val x = 42
  |      ^^^
  |      Expected start of definition
  |
  | longer explanation available when compiling with `-explain`
scala> final def x = 42
-- [E088] Syntax Error: --------------------------------------------------------
1 |final def x = 42
  |      ^^^
  |      Expected start of definition
  |
  | longer explanation available when compiling with `-explain`

Why should this work; shouldn't final only be available inside classes?

What is the use-case for this?

@mbovel
Copy link
Member

mbovel commented Jun 10, 2024

Note: you get the same error when trying to define a final val in a method for example:

  ~/scala-snippets-5 cat finalVal.scala
package finalVal

@main def Main =
  final val x = 42
  ()
  ~/scala-snippets-5 scala-cli run finalVal.scala
Compiling project (Scala 3.4.1, JVM (17))
[error] ./finalVal.scala:4:9
[error] Expected start of definition
[error]   final val x = 42
[error]         ^^^
Error compiling project (Scala 3.4.1, JVM (17))
Compilation failed

@Gedochao Gedochao added area:reporting Error reporting including formatting, implicit suggestions, etc Spree Suitable for a future Spree itype:enhancement and removed itype:bug labels Jun 10, 2024
@mbovel
Copy link
Member

mbovel commented Jun 10, 2024

Note from @dwijnand: -explain actually doesn't show anything useful here.

@mbovel
Copy link
Member

mbovel commented Jun 10, 2024

This issue has been re-purposed to making the error message clearer. Note that this will not be specific to the REPL.

@mbovel mbovel changed the title REPL parse failure Report a clearer error message for unexpected final modifier Jun 10, 2024
@mbovel
Copy link
Member

mbovel commented Jun 11, 2024

This issue was picked for the Scala Issue Spree of today, June 11th. @aherlihy, @mbovel, @bracevac, @EugeneFlesselle will be working on it.

@dwijnand
Copy link
Member

Note from @dwijnand: -explain actually doesn't show anything useful here.

Scratch that, I wasn't compiling it correctly (getting other compile errors on what I was working on).

@mbovel
Copy link
Member

mbovel commented Jun 11, 2024

Ah, indeed:

  ~/scala-snippets-5 scala-cli run -O -explain finalVal.scala
Compiling project (Scala 3.4.1, JVM (17))
[error] ./finalVal.scala:4:9
[error] Expected start of definition
[error] 
[error] Explanation
[error] ===========
[error] You have to provide either class, trait, object, or enum definitions after qualifiers
[error]   final val x = 42
[error]         ^^^
Error compiling project (Scala 3.4.1, JVM (17))
Compilation failed

However, this message also doesn't seem particularly helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:repl area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement Spree Suitable for a future Spree
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants