Syntax issues with compiler option "-source 3.0-migration" under 3.1.1

Compile the following code (in file Main.scala)

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"
  def test5(x: Int, y: Int) = if ((x - y) == 0)  then "equal" else "different"

with (scala version 3.1.1)

scala3 -indent -new-syntax -source 3.0-migration src/main/scala/Main.scala

This results in

-- Error: src/main/scala/Main.scala:5:33 ---------------------------------------
5 |  def test4(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:5:44 ---------------------------------------
5 |  def test4(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:5:41 ---------------------------------------
5 |  def test4(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different"
  |                                         ^^
  |                        missing arguments for value of type Any => Boolean
3 errors found

Without the option “-source 3.0-migration” the code compiles just fine. Got the same behaviour in Scastie.

My question: Is this a compiler bug, or is something else the matter? For example is the use of this compiler option not allowed for scala 3.1.1? (3.0.2 gives the same error btw)

2 Likes

I have filed this issue under Compiler error with -new-syntax and -source:3.0-migration. Although it is minor, and a workaround is available, it should be dealt with eventually.