Search code examples
scalasbtscala-3scala-version-cross-build

Lost on migrating from Scala 2.13 to 3.2


I have been following the instructions on the migration guide with no success. I already have an SBT project that cross-compiles without problems, with the following build.sbt:

lazy val ttv = (project in file("."))
  .settings(
    name := "Tame the Void",
    version := "0.1",
    scalaVersion := "3.2.0",
    crossScalaVersions := Seq("2.13.6", "3.2.0"),
    libraryDependencies ++= mainDependencies ++ testDependencies,
    scalacOptions := {
      Seq(
        "-encoding",
        "UTF-8",
        "-feature",
        "-language:implicitConversions",
        // disabled during the migration
        // "-Xfatal-warnings"
      ) ++
        (CrossVersion.partialVersion(scalaVersion.value) match {
          case Some((3, _)) =>
            println("Using 3.0 flags --------------------")
            Seq(
              "-unchecked",
              "-source:3.2-migration",
              "-indent",
              "-rewrite"
            )
          case _ =>
            println("Using 2.13 flags --------------------")
            Seq(
              "-deprecation",
              "-Wunused:imports,privates,locals",
              "-Wvalue-discard"
            )
        })
    }
  )

All scala 2.13 code compiles perfectly, but I added a scala 3 specific file to test: src/main/scala-3/Scala3test.scala:

object Scala3Test extends App {

  given x: Int = 1
  def f(using x: Int) = println(x)
  f

  given Ordering[Int] with
    override def compare(x: Int, y: Int): Int = if x < y then 1 else -1

  def g(using x: Ordering[Int]) = println(x.compare(3, 4))
  g
}

This test runs perfectly if I create a Scala 3 project from scratch, but here it seems not to recognize the new syntax at all:

compile
Using 3.0 flags --------------------
[info] compiling 102 Scala sources to D:\Projects\4x-scala\target\scala-3.2.0\classes ...
[warn] Flag -source set repeatedly
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:9 
[error] 3 |  given x: Int = 1
[error]   |         ^
[error]   |         expression expected but : found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:15 ----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:16 
[error] 7 |  given Ordering[Int] with
[error]   |                ^
[error]   |                expression expected but '[' found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:2 
[error] 3 |  given x: Int = 1
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:5:3 -----------
[error] 5 |  f
[error]   |   ^
[error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:2 
[error] 7 |  given Ordering[Int] with
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[warn] one warning found
[error] 6 errors found

My understanding based on SBT cross-building docs is that it should be building that file in 3.2.0 and recognize the syntax. It is also not rewriting any syntax (as it should given the "-indent", "-rewrite" flags).

Any pointers would be greatly appreciated.


Solution

  • You have a wrong flag set somewhere. Allow me to demonstrate:

    $ ls
    Scala3Test.scala build.sbt
    $ cat build.sbt
    name := "scala-3-test"
    scalaVersion := "3.2.0"
    scalacOptions += "-source:3.0-migration"
    $ sbt compile
    [info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
    [error] -- [E018] Syntax Error: ***/Scala3Test.scala:3:9
    [error] 3 |  given x: Int = 1
    [error]   |         ^
    [error]   |         expression expected but : found
    [error]   |
    [error]   | longer explanation available when compiling with `-explain`
    [error] -- Error: ***/Scala3Test.scala:3:15 -----------
    [error] 3 |  given x: Int = 1
    [error]   |               ^
    [error]   |               end of statement expected but '=' found
    [error] -- [E018] Syntax Error: ***/Scala3Test.scala:7:16
    [error] 7 |  given Ordering[Int] with
    [error]   |                ^
    [error]   |                expression expected but '[' found
    [error]   |
    [error]   | longer explanation available when compiling with `-explain`
    [error] -- [E006] Not Found Error: ***/Scala3Test.scala:3:2
    [error] 3 |  given x: Int = 1
    [error]   |  ^^^^^
    [error]   |  Not found: given
    [error]   |
    [error]   | longer explanation available when compiling with `-explain`
    [error] -- Error: ***/Scala3Test.scala:5:3 ------------
    [error] 5 |  f
    [error]   |   ^
    [error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
    [error] -- [E006] Not Found Error: ***/Scala3Test.scala:7:2
    [error] 7 |  given Ordering[Int] with
    [error]   |  ^^^^^
    [error]   |  Not found: given
    [error]   |
    [error]   | longer explanation available when compiling with `-explain`
    [error] 6 errors found
    [error] (Compile / compileIncremental) Compilation failed
    [error] Total time: 3 s, completed 28 Sep 2022, 15:23:17
    

    Now I disabled the -source:3.0-migration flag.

    $ cat build.sbt
    name := "scala-3-test"
    scalaVersion := "3.2.0"
    //scalacOptions += "-source:3.0-migration"
    $ sbt compile
    [info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
    [success] Total time: 3 s, completed 28 Sep 2022, 15:23:56
    

    Also the fact that the compiler warns you about Flag -source set repeatedly, even though you have set it only once in your build file, must mean that you have set it somewhere else too.

    If you really have no other build code in your project, including any plugins that can set some flags you don't know about, then perhaps you have some code in your $HOME/.sbt/1.0 folder that enables a plugin or sets a flag.