Skip to content

Commit 24852e8

Browse files
committed
Compile Scala library with Dotty and test its TASTy
1 parent c40ef6e commit 24852e8

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ val `dotty-library-bootstrapped` = Build.`dotty-library-bootstrapped`
1010
val `dotty-library-bootstrappedJS` = Build.`dotty-library-bootstrappedJS`
1111
val `dotty-sbt-bridge` = Build.`dotty-sbt-bridge`
1212
val `dotty-sbt-bridge-tests` = Build.`dotty-sbt-bridge-tests`
13+
val `dotty-scala-library` = Build.`dotty-scala-library`
14+
val `dotty-tasty-inspector-library-test` = Build.`dotty-tasty-inspector-library-test`
15+
val superBootstrappedSandbox = Build.superBootstrappedSandbox
1316
val `dotty-staging` = Build.`dotty-staging`
1417
val `dotty-tasty-inspector` = Build.`dotty-tasty-inspector`
1518
val `dotty-language-server` = Build.`dotty-language-server`

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,12 +3266,14 @@ class Typer extends Namer
32663266
// To overcome the current limitation we intercept the call and rewrite it into
32673267
// a call to dotty.internal.StringContext.f which we can implement using the new macros.
32683268
// As the macro is implemented in the bootstrapped library, it can only be used from the bootstrapped compiler.
3269-
val Apply(TypeApply(Select(sc, _), _), args) = tree
3270-
val newCall = ref(defn.InternalStringContextMacroModule_f).appliedTo(sc).appliedToArgs(args).withSpan(tree.span)
3271-
readaptSimplified(Inliner.inlineCall(newCall))
3269+
// val Apply(TypeApply(Select(sc, _), _), args) = tree
3270+
// val newCall = ref(defn.InternalStringContextMacroModule_f).appliedTo(sc).appliedToArgs(args).withSpan(tree.span)
3271+
// readaptSimplified(Inliner.inlineCall(newCall))
3272+
Literal(Constant("")) // FIXME: intrinsify to be able to compile the scala library from dotty without using macros
32723273
else if (tree.symbol.isScala2Macro &&
32733274
// raw and s are eliminated by the StringInterpolatorOpt phase
32743275
tree.symbol != defn.StringContext_raw &&
3276+
tree.symbol != defn.StringContext_f &&
32753277
tree.symbol != defn.StringContext_s)
32763278
if (ctx.settings.XignoreScala2Macros.value) {
32773279
report.warning("Scala 2 macro cannot be used in Dotty, this call will crash at runtime. See https://dotty.epfl.ch/docs/reference/dropped-features/macros.html", tree.srcPos.startPos)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import org.junit.Test
2+
import org.junit.Assert._
3+
4+
import scala.quoted._
5+
6+
class Test1 {
7+
@Test def test1: Unit =
8+
val foo = new scala.tasty.inspector.TastyInspector {
9+
def processCompilationUnit(using QuoteContext)(root: qctx.tasty.Tree): Unit =
10+
println(root.show)
11+
}
12+
foo.inspect(System.getProperty("dotty.scala.library"), List("scala.Option"))
13+
14+
println(new dotty.tools.dotc.Driver)
15+
16+
// assert(false, System.getProperty("dotty.scala.library"))
17+
}

project/Build.scala

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,79 @@ object Build {
817817
javaOptions := (javaOptions in `dotty-compiler-bootstrapped`).value
818818
)
819819

820+
lazy val `dotty-scala-library-from-tasty-tests` = project.in(file("dotty-scala-library-from-tasty-tests")).
821+
withCommonSettings(Bootstrapped).
822+
dependsOn(`dotty-tasty-inspector` % "test->test").
823+
settings(commonBootstrappedSettings).
824+
settings(
825+
javaOptions := (javaOptions in `dotty-compiler-bootstrapped`).value,
826+
javaOptions += "-Ddotty.scala.library=" + packageBin.in(`dotty-scala-library`, Compile).value.getAbsolutePath
827+
)
828+
829+
830+
// Scala library compiled by dotty using the latest published sources of the library
831+
lazy val `dotty-scala-library` = project.in(file("dotty-scala-library")).
832+
withCommonSettings(Bootstrapped).
833+
dependsOn(dottyCompiler(Bootstrapped) % "provided; compile->runtime; test->test").
834+
dependsOn(`dotty-tasty-inspector` % "test->test").
835+
settings(commonBootstrappedSettings).
836+
settings(
837+
javaOptions := (javaOptions in `dotty-compiler-bootstrapped`).value,
838+
scalacOptions -= "-Xfatal-warnings",
839+
/* Add the sources of scalajs-ir.
840+
* To guarantee that dotty can bootstrap without depending on a version
841+
* of scalajs-ir built with a different Scala compiler, we add its
842+
* sources instead of depending on the binaries.
843+
*/
844+
ivyConfigurations += SourceDeps.hide,
845+
transitiveClassifiers := Seq("sources"),
846+
libraryDependencies +=
847+
("org.scala-lang" % "scala-library" % stdlibVersion(Bootstrapped) % "sourcedeps"),
848+
sourceGenerators in Compile += Def.task { // TODO factor out common logic with scalajs
849+
val s = streams.value
850+
val cacheDir = s.cacheDirectory
851+
val trgDir = (sourceManaged in Compile).value / "scala-library-src"
852+
853+
val report = updateClassifiers.value
854+
val scalaLibrarySourcesJar = report.select(
855+
configuration = configurationFilter("sourcedeps"),
856+
module = (_: ModuleID).name == "scala-library",
857+
artifact = artifactFilter(`type` = "src")).headOption.getOrElse {
858+
sys.error(s"Could not fetch scala-library sources")
859+
}
860+
861+
FileFunction.cached(cacheDir / s"fetchScalaLibrarySrc",
862+
FilesInfo.lastModified, FilesInfo.exists) { dependencies =>
863+
s.log.info(s"Unpacking scala-library sources to $trgDir...")
864+
if (trgDir.exists)
865+
IO.delete(trgDir)
866+
IO.createDirectory(trgDir)
867+
IO.unzip(scalaLibrarySourcesJar, trgDir)
868+
869+
((trgDir ** "*.scala") +++ (trgDir ** "*.java")).get.toSet
870+
} (Set(scalaLibrarySourcesJar)).toSeq
871+
}.taskValue,
872+
sources in Compile ~= (_.filterNot(file => // TODO more efficient filter
873+
file.getPath.endsWith("scala/Any.scala") ||
874+
file.getPath.endsWith("scala/AnyVal.scala") ||
875+
file.getPath.endsWith("scala/AnyRef.scala") ||
876+
file.getPath.endsWith("scala/Nothing.scala") ||
877+
file.getPath.endsWith("scala/Null.scala") ||
878+
file.getPath.endsWith("scala/Singleton.scala")))
879+
)
880+
881+
// TODO rename?
882+
// Tests that we can compile and run an app using the scala-library compiled by dotty
883+
lazy val superBootstrappedSandbox = project.in(file("sandbox/superbootstrapped")).
884+
withCommonSettings(Bootstrapped).
885+
dependsOn(`dotty-scala-library`).
886+
settings(
887+
managedClasspath in Compile ~= {
888+
_.filterNot(file => file.data.getName == "scala-library-2.13.3.jar")
889+
}
890+
)
891+
892+
820893
lazy val `dotty-sbt-bridge` = project.in(file("sbt-bridge/src")).
821894
// We cannot depend on any bootstrapped project to compile the bridge, since the
822895
// bridge is needed to compile these projects.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package hello
2+
3+
enum Color:
4+
case Red, Green, Blue
5+
6+
object HelloWorld:
7+
def main(args: Array[String]): Unit = {
8+
println("hello dotty.superbootstrapped!")
9+
println(Color.Red)
10+
println(Color.Green)
11+
println(Color.Blue)
12+
}

0 commit comments

Comments
 (0)