从Scala 2.13迁移到3.2时丢失



我一直在按照迁移指南中的说明进行操作,但没有成功。我已经有了一个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"
)
})
}
)

所有的scala2.13代码都能完美编译,但我添加了一个scala3特定的文件来测试: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
}

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

compile
Using 3.0 flags --------------------
[info] compiling 102 Scala sources to D:Projects4x-scalatargetscala-3.2.0classes ...
[warn] Flag -source set repeatedly
[error] -- [E018] Syntax Error: D:Projects4x-scalasrcmainscala-3Scala3Test.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:Projects4x-scalasrcmainscala-3Scala3Test.scala:3:15 ----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: D:Projects4x-scalasrcmainscala-3Scala3Test.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:Projects4x-scalasrcmainscala-3Scala3Test.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:Projects4x-scalasrcmainscala-3Scala3Test.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:Projects4x-scalasrcmainscala-3Scala3Test.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

基于SBT交叉构建文档,我的理解是,它应该在3.2.0中构建该文件并识别语法。它也没有重写任何语法(因为它应该给出"-intent"、"-rewrite"标志(。

任何建议都将不胜感激。

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

$ 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的信息,即使您在构建文件中只设置过一次,这一定意味着您也在其他地方设置了它。

如果你的项目中真的没有其他构建代码,包括任何可以设置一些你不知道的标志的插件,那么你的$HOME/.sbt/1.0文件夹中可能有一些代码可以启用插件或设置标志。

删除

"-source:3.2-migration",

会有所帮助。

相关内容

  • 没有找到相关文章

最新更新