Unexpected Scala3's new-syntax compilation error

Upon compilation of the following

/* sbt = 1.6.2
 * scalaVersion := "3.1.1"
 * scalacOptions ++= Seq("-explain","-indent","-new-syntax") */
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" 

the first methods compile fine, but the latter gives:

[error] -- Error: .../scala3/src/main/scala/Main.scala:4:33 
[error] 4 |  def test4(x: Int, y: Int) = if (x - y) == 0    then "equal" else "different" 
[error]   |                                 ^^^^^^^
[error]   |This construct is not allowed under -new-syntax.
[error]   |This construct can be rewritten automatically under -new-syntax -rewrite -source 3.0-migration.

why is that?

I can’t reproduce the error you’re getting (neither with a build.sbt containing the given lines, nor with calling the compiler directly), also I don’t see, why it shouldn’t compile. Do you have more code in the file? Maybe something is triggering a compiler bug. Otherwise my first guess would be to remove sbt’s target folder and try again.

1 Like

Can’t reproduce either. (Not locally and not in scastie)

The relevant code is in def condExpr in Parser.scala where a call to toBeContinued seems to have returned false although the condition continues after the ).

1 Like

Thank you for verification. This is interesting. There is no other code around (or libs for that matter), clearing the target directory does not help. But indeed, only compilation via SBT causes this error, a direct call to the dotty compiler does not. Interesting. Something is different … :thinking:

@devlaam do you maybe have -source 3.0-migration enabled somewhere? (or "-source","3.0-migration" in some sbt settings?)

With that setting (precisely scalacOptions ++= Seq("-new-syntax","-source","3.0-migration")) I was able to reproduce the error message.

If yes, your options are basically saying “use version 3.1 and please help me to upgrade from old syntax to 3.0”, which does not really make sense.

If you used compiler version 3.0, that should imho work (but does not :grimacing: ).

1 Like

I can confirm that -source 3.0-migration on direct compilation indeed causes this error, but the options i used where as described in the comments of the code, nothing more. Maybe there is something under the hood …
This is my build.sbt file (comments removed):

val scala3Version = "3.1.1"

lazy val root = project
  .in(file("."))
  .settings(
    name := "scala3-simple",
    version := "0.1.0",
    scalaVersion := scala3Version,
    scalacOptions ++= Seq("-explain","-indent","-new-syntax")  )

Well, I still can’t reproduce the error with that build.sbt file:

$ tree
.
├── build.sbt
└── src
    └── main
        └── scala
            └── Main.scala

3 directories, 2 files
$ cat build.sbt 
val scala3Version = "3.1.1"

lazy val root = project
  .in(file("."))
  .settings(
    name := "scala3-simple",
    version := "0.1.0",
    scalaVersion := scala3Version,
    scalacOptions ++= Seq("-explain","-indent","-new-syntax")  )


$ cat src/main/scala/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" 

$ sbt "clean; compile"
[info] Updated file /tmp/tmp.meGJLOxJCn/project/build.properties: set sbt.version to 1.6.2
[info] welcome to sbt 1.6.2 (Ubuntu Java 11.0.14)
[info] loading settings for project global-plugins from plugins.sbt ...
[info] loading global plugins from /home/anselm/.sbt/1.0/plugins
[info] loading project definition from /tmp/tmp.meGJLOxJCn/project
[info] loading settings for project root from build.sbt ...
[info] set current project to scala3-simple (in build file:/tmp/tmp.meGJLOxJCn/)
[success] Total time: 0 s, completed Mar 10, 2022, 2:37:09 PM
[info] compiling 1 Scala source to /tmp/tmp.meGJLOxJCn/target/scala-3.1.1/classes ...
[success] Total time: 5 s, completed Mar 10, 2022, 2:37:15 PM

Do you have other files in the project?
And have you checked your global sbt settings for weird options or plugins?

1 Like

Found it! The offending plugin was in global plugins:

addSbtPlugin("ch.epfl.scala" % "sbt-scala3-migrate" % "0.5.0")

Thank you for reproducing and assistance.

1 Like