scala/scala3

Compiler error with -new-syntax and -source:3.0-migration

Open

#14,694 opened on Mar 16, 2022

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Scala (6,247 stars) (1,159 forks)batch import
area:parserarea:rewriting toolbackloghelp wanteditype:bugprio:low

Description

Observation

Compile the following code

object NewSyntax:
  def test1(x: Int, y: Int) = if x == y          then "equal" else "different"
  def test2(x: Int, y: Int) = if (x == y)        then "equal" else "different"
  def test3(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different"
  def test4(x: Int, y: Int) = if ((x - y) == 0)  then "equal" else "different"

under Scala 3.1.1 with the options -indent -new-syntax -source 3.0-migration, this results in

-- Error: src/main/scala/Main.scala:4:33 ---------------------------------------
4 |  def test3(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different"
  |                                 ^^^^^^^
  |This construct is not allowed under -new-syntax.
  |This construct can be rewritten automatically under -new-syntax -rewrite -source 3.0-migration.
-- Error: src/main/scala/Main.scala:4:44 ---------------------------------------
4 |  def test3(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different"
  |                                            ^
  |                       end of statement expected but integer literal found
-- Error: src/main/scala/Main.scala:4:41 ---------------------------------------
4 |  def test3(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different"
  |                                         ^^
  |                        missing arguments for value of type Any => Boolean
3 errors found

Expectation

Code should be accepted as correct.

Mitigation

Use extra parenthesis like in test4 or drop the compiler option -source 3.0-migration. The latter may not always be possible for every situation. The former may be a lot of work for large projects, and partly nullifies the beauty of the new syntax.

Contributor guide