Ausgabe
Ich habe die Anweisungen im Migrationsleitfaden ohne Erfolg befolgt. Ich habe bereits ein SBT-Projekt, das ohne Probleme kompiliert wird, mit folgendem 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"
)
})
}
)
Der gesamte scala 2.13-Code lässt sich perfekt kompilieren, aber ich habe zum Testen eine scala 3-spezifische Datei hinzugefügt 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
}
Dieser Test läuft perfekt, wenn ich ein Scala 3-Projekt von Grund auf neu erstelle, aber hier scheint es die neue Syntax überhaupt nicht zu erkennen:
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
Mein Verständnis basierend auf SBT Cross-Building-Dokumenten ist, dass es diese Datei in 3.2.0 erstellen und die Syntax erkennen sollte. Es schreibt auch keine Syntax um (wie es angesichts der Flags “-indent”, “-rewrite” sein sollte).
Alle Hinweise wären sehr dankbar.
Lösung
Irgendwo hast du ein falsches Flag gesetzt. Lassen Sie mich demonstrieren:
$ 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
Jetzt habe ich die -source:3.0-migration
Flagge deaktiviert.
$ 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
Auch die Tatsache, dass der Compiler Sie davor warnt Flag -source set repeatedly
, obwohl Sie es nur einmal in Ihrer Build-Datei gesetzt haben, muss bedeuten, dass Sie es auch woanders gesetzt haben.
Wenn Sie wirklich keinen anderen Build-Code in Ihrem Projekt haben, einschließlich Plugins, die einige Flags setzen können, von denen Sie nichts wissen, dann haben Sie vielleicht Code in Ihrem $HOME/.sbt/1.0
Ordner, der ein Plugin aktiviert oder ein Flag setzt.
Beantwortet von – Jasper-M
Antwort geprüft von – Mary Flores (FixError Volunteer)