在从Scala 2.13迁移到3.2时迷失方向

svmlkihl  于 2022-11-09  发布在  Scala
关注(0)|答案(2)|浏览(165)

我一直在按照migration guide上的说明操作,但没有成功。我已经有了一个交叉编译没有问题的SBT项目,它的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"
            )
        })
    }
  )

所有Scala 2.13代码都可以很好地编译,但我添加了一个特定于Scala 3的文件进行测试: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
}

如果我从头开始创建一个Scala 3项目,则该测试运行得很好,但这里它似乎根本无法识别新的语法:

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

我对based on SBT cross-building docs的理解是,它应该在3.2.0中构建该文件并识别语法。它也没有重写任何语法(因为它应该给出“-indent”、“-rewrite”标志)。
如有任何建议,我们将不胜感激。

u3r8eeie

u3r8eeie1#

您在某个地方设置了错误的标志。请允许我演示一下:

$ 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

现在我禁用了-source:3.0-migration标志。

$ 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

此外,即使您在构建文件中只设置了一次Flag -source set repeatedly,编译器也会警告您Flag -source set repeatedly,这一定意味着您也在其他地方设置了它。
如果您的项目中确实没有其他构建代码,包括任何可以设置一些您不知道的标志的插件,那么您的$HOME/.sbt/1.0文件夹中可能有一些代码可以启用插件或设置标志。

omjgkv6w

omjgkv6w2#

正在删除

"-source:3.2-migration",

会有帮助的。

相关问题