Skip to content

Commit 9d5bece

Browse files
RafalSierkiewiczstephenamar-dbHe-Pin
authored
Scala 3 migration (#270)
I tried to migrate code to scala 3. This required updates of some libs and a little code adjustments. I would appreciate your input 🙏 --------- Co-authored-by: Stephen Amar <[email protected]> Co-authored-by: He-Pin(kerr) <[email protected]>
1 parent 65a259b commit 9d5bece

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+427
-393
lines changed

.github/workflows/pr-build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
env:
1818
# Set LANG=C to simulate least-common-denominator target deployment environments:
1919
LANG: C
20+
SCALANATIVE_THREAD_STACK_SIZE: 10m
2021
name: Sjsonnet build for ${{ matrix.lang }} on JDK ${{ matrix.java }}
2122
steps:
2223
- uses: actions/checkout@v4

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ out/
1111
.bloop
1212
/project/.bloop
1313
/project/project/
14-
/project/metals.sbt
14+
/project/metals.sbt

bench/src/main/scala/sjsonnet/ParserBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ParserBenchmark {
2828
def main(bh: Blackhole): Unit = {
2929
bh.consume(allFiles.foreach { case (p, s) =>
3030
val res = fastparse.parse(s, new Parser(p, true, HashMap.empty, HashMap.empty).document(_))
31-
bh.consume(res.asInstanceOf[Success[_]])
31+
bh.consume(res.asInstanceOf[Success[?]])
3232
})
3333
}
3434
}

bench/src/main/scala/sjsonnet/ProfilingEvaluator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class ProfilingEvaluator(resolver: CachedResolver,
9696
case a: Array[Expr.Bind] => a.iterator.flatMap(getChildren).toSeq
9797
case a: Array[Expr.Member.Field] => a.iterator.flatMap(getChildren).toSeq
9898
case a: Array[Expr.Member.AssertStmt] => a.iterator.flatMap(getChildren).toSeq
99-
case s: Seq[_] => s.collect { case e: Expr => e }
100-
case s: Some[_] => s.collect { case e: Expr => e }
99+
case s: Seq[?] => s.collect { case e: Expr => e }
100+
case s: Some[?] => s.collect { case e: Expr => e }
101101
case _ => Nil
102102
}.filter(_ != null).toSeq
103103
case _ => Nil

build.sbt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
val sjsonnetVersion = "0.4.15.1"
22

3-
scalaVersion in Global := "2.13.16"
3+
val scala213 = "2.13.16"
4+
val scala3 = "3.3.5"
45

56
cancelable in Global := true
67

8+
val scala3Options = Seq()
9+
val scala2Options = Seq("-opt:l:inline", "-opt-inline-from:sjsonnet.*,sjsonnet.**", "-Xsource:3")
10+
11+
lazy val commonSettings = Seq(
12+
scalaVersion := scala3,
13+
crossScalaVersions := Seq(scala213, scala3),
14+
scalacOptions ++= {CrossVersion.partialVersion(scalaVersion.value) match {
15+
case Some((3, _)) => scala3Options
16+
case _ => scala2Options
17+
}}
18+
)
19+
720
lazy val main = (project in file("sjsonnet"))
21+
.settings(commonSettings: _*)
822
.settings(
9-
Compile / scalacOptions ++= Seq("-opt:l:inline", "-opt-inline-from:sjsonnet.*,sjsonnet.**"),
1023
Test / fork := true,
1124
Test / javaOptions += "-Xss100m",
1225
Test / baseDirectory := (ThisBuild / baseDirectory).value,
@@ -56,6 +69,7 @@ lazy val main = (project in file("sjsonnet"))
5669
lazy val bench = (project in file("bench"))
5770
.dependsOn(main % "compile->test")
5871
.enablePlugins(JmhPlugin)
72+
.settings(commonSettings: _*)
5973
.settings(
6074
run / fork := true,
61-
)
75+
)

build.sc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.util.Base64
55

66
val sjsonnetVersion = "0.4.15.1"
77

8-
val scalaVersions = Seq("2.12.20", "2.13.16")
8+
val scalaVersions = Seq("2.12.20", "2.13.16", "3.3.5")
99

1010
trait SjsonnetCrossModule extends CrossScalaModule with PublishModule {
1111
def crossValue: String
@@ -153,7 +153,7 @@ object sjsonnet extends Module {
153153
ivy"org.yaml:snakeyaml::2.0",
154154
ivy"com.google.re2j:re2j:1.8",
155155
)
156-
def scalacOptions = Seq("-opt:l:inline", "-opt-inline-from:sjsonnet.*,sjsonnet.**")
156+
def scalacOptions = Seq("-opt:l:inline", "-opt-inline-from:sjsonnet.*,sjsonnet.**", "-Xsource:3")
157157

158158
object test extends ScalaTests with CrossTests {
159159
def forkArgs = Seq("-Xss100m")

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.10.0
1+
sbt.version=1.10.11

project/plugins.sbt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
//addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.7")
2-
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3")
1+
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.3")

sjsonnet/server/src/sjsonnet/SjsonnetServerMain.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ class Server[T](lockBase: String,
151151

152152
@volatile var done = false
153153
@volatile var idle = false
154-
val t = new Thread(() =>
155-
try {
154+
val t = new Thread(new Runnable{
155+
override def run(): Unit = {
156+
try {
156157
val (result, newStateCache) = sm.main0(
157158
args,
158159
sm.stateCache,
@@ -173,6 +174,8 @@ class Server[T](lockBase: String,
173174
} finally{
174175
done = true
175176
idle = true
177+
}
178+
}
176179
},
177180
"SjsonnetServerActionRunner"
178181
)

sjsonnet/src-jvm-native/sjsonnet/CachedResolvedFile.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CachedResolvedFile(val resolvedImportPath: OsPath, memoryLimitBytes: Long,
2626
// Assert that the file is less than limit
2727
assert(jFile.length() <= memoryLimitBytes, s"Resolved import path $resolvedImportPath is too large: ${jFile.length()} bytes > ${memoryLimitBytes} bytes")
2828

29-
private[this] val resolvedImportContent: ResolvedFile = {
29+
private val resolvedImportContent: ResolvedFile = {
3030
// TODO: Support caching binary data
3131
if (jFile.length() > cacheThresholdBytes) {
3232
// If the file is too large, then we will just read it from disk
@@ -38,11 +38,11 @@ class CachedResolvedFile(val resolvedImportPath: OsPath, memoryLimitBytes: Long,
3838
}
3939
}
4040

41-
private[this] def readString(jFile: File): String = {
41+
private def readString(jFile: File): String = {
4242
new String(Files.readAllBytes(jFile.toPath), StandardCharsets.UTF_8);
4343
}
4444

45-
private[this] def readRawBytes(jFile: File): Array[Byte] = Files.readAllBytes(jFile.toPath)
45+
private def readRawBytes(jFile: File): Array[Byte] = Files.readAllBytes(jFile.toPath)
4646

4747
/**
4848
* A method that will return a reader for the resolved import. If the import is too large, then this will return
@@ -67,12 +67,12 @@ class CachedResolvedFile(val resolvedImportPath: OsPath, memoryLimitBytes: Long,
6767
}
6868

6969

70-
override lazy val contentHash: String = {
70+
override def contentHash(): String = {
7171
if (resolvedImportContent == null) {
7272
// If the file is too large, then we will just read it from disk
7373
Platform.hashFile(jFile)
7474
} else {
75-
resolvedImportContent.contentHash
75+
resolvedImportContent.contentHash()
7676
}
7777
}
7878

0 commit comments

Comments
 (0)