Skip to content

Commit 9bc668c

Browse files
committed
Fix scala#8020: Create dotty-tasty-consumer library
1 parent 5dc00b1 commit 9bc668c

File tree

29 files changed

+93
-43
lines changed

29 files changed

+93
-43
lines changed

.drone.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ steps:
4040
depends_on: [ clone ]
4141
commands:
4242
- cp -R . /tmp/2/ && cd /tmp/2/
43-
- ./project/scripts/sbt ";dotty-bootstrapped/compile ;dotty-bootstrapped/test ;dotty-staging/test ;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test ;configureIDE"
43+
- ./project/scripts/sbt ";dotty-bootstrapped/compile ;dotty-bootstrapped/test ;dotty-staging/test ;dotty-tasty-consumer/test ;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test ;configureIDE"
4444
- ./project/scripts/bootstrapCmdTests
4545

4646
- name: community_build

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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`
1313
val `dotty-staging` = Build.`dotty-staging`
14+
val `dotty-tasty-consumer` = Build.`dotty-tasty-consumer`
1415
val `dotty-language-server` = Build.`dotty-language-server`
1516
val `dotty-bench` = Build.`dotty-bench`
1617
val `dotty-bench-bootstrapped` = Build.`dotty-bench-bootstrapped`

compiler/test/dotty/Properties.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ object Properties {
5353
/** dotty-staging jar */
5454
def dottyStaging: String = sys.props("dotty.tests.classes.dottyStaging")
5555

56+
/** dotty-tasty-consumer jar */
57+
def dottyTastyConsumer: String = sys.props("dotty.tests.classes.dottyTastyConsumer")
58+
5659
/** tasty-core jar */
5760
def tastyCore: String = sys.props("dotty.tests.classes.tastyCore")
5861

compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting {
127127
aggregateTests(
128128
compileFilesInDir("tests/run-with-compiler", withCompilerOptions),
129129
compileFilesInDir("tests/run-staging", withStagingOptions),
130-
compileDir("tests/run-with-compiler-custom-args/tasty-interpreter", withCompilerOptions)
130+
compileFilesInDir("tests/run-custom-args/tasty-consumer", withTastyConsumerOptions),
131+
compileDir("tests/run-custom-args/tasty-interpreter", withTastyConsumerOptions),
131132
).checkRuns()
132133
}
133134

compiler/test/dotty/tools/vulpix/TestConfiguration.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ object TestConfiguration {
3939
lazy val withStagingClasspath =
4040
withCompilerClasspath + File.pathSeparator + mkClasspath(List(Properties.dottyStaging))
4141

42+
lazy val withTastyConsumerClasspath =
43+
withCompilerClasspath + File.pathSeparator + mkClasspath(List(Properties.dottyTastyConsumer))
44+
4245
def mkClasspath(classpaths: List[String]): String =
4346
classpaths.map({ p =>
4447
val file = new java.io.File(p)
@@ -54,6 +57,8 @@ object TestConfiguration {
5457
defaultOptions.withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
5558
lazy val withStagingOptions =
5659
defaultOptions.withClasspath(withStagingClasspath).withRunClasspath(withStagingClasspath)
60+
lazy val withTastyConsumerOptions =
61+
defaultOptions.withClasspath(withTastyConsumerClasspath).withRunClasspath(withTastyConsumerClasspath)
5762
val allowDeepSubtypes = defaultOptions without "-Yno-deep-subtypes"
5863
val allowDoubleBindings = defaultOptions without "-Yno-double-bindings"
5964
val picklingOptions = defaultOptions and (

dist/bin/common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ DOTTY_COMP=$(find_lib "*dotty-compiler*")
108108
DOTTY_INTF=$(find_lib "*dotty-interfaces*")
109109
DOTTY_LIB=$(find_lib "*dotty-library*")
110110
DOTTY_STAGING=$(find_lib "*dotty-staging*")
111+
DOTTY_CONSUME_TASTY=$(find_lib "*dotty-consume-tasty*")
111112
TASTY_CORE=$(find_lib "*tasty-core*")
112113
SCALA_ASM=$(find_lib "*scala-asm*")
113114
SCALA_LIB=$(find_lib "*scala-library*")

dist/bin/dotr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ elif [ $execute_repl == true ] || [ ${#residual_args[@]} -ne 0 ]; then
102102
echo "warning: multiple classpaths are found, dotr only use the last one."
103103
fi
104104
if [ $with_compiler == true ]; then
105-
cp_arg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING"
105+
cp_arg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_CONSUME_TASTY"
106106
fi
107107
eval exec "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${jvm_options[@]}" "${residual_args[@]}"
108108
else

docs/docs/reference/metaprogramming/tasty-inspect.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ object Test {
3333
ConsumeTasty("", List("foo.Bar"), new Consumer)
3434
}
3535
}
36-
```
36+
```
37+
38+
Note that if we need to run the main (in an object called `Test`) after
39+
compilation we need make available the compiler to the runtime:
40+
41+
```shell
42+
dotc -with-compiler -d out Test.scala
43+
dotr -with-compiler -classpath out Test
44+
```
45+
46+
Or, from SBT:
47+
48+
```scala
49+
libraryDependencies += "ch.epfl.lamp" %% "dotty-tasty-consumer" % scalaVersion.value
50+
```

library/src/scala/tasty/file/ConsumeTasty.scala

Lines changed: 0 additions & 26 deletions
This file was deleted.

project/Build.scala

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,10 @@ object Build {
595595
val asm = findArtifactPath(externalDeps, "scala-asm")
596596
val dottyCompiler = jars("dotty-compiler")
597597
val dottyStaging = jars("dotty-staging")
598+
val dottyTastyConsumer = jars("dotty-tasty-consumer")
598599
val dottyInterfaces = jars("dotty-interfaces")
599600
val tastyCore = jars("tasty-core")
600-
run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, tastyCore).mkString(File.pathSeparator)))
601+
run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyConsumer, tastyCore).mkString(File.pathSeparator)))
601602
} else run(args)
602603
},
603604

@@ -671,9 +672,10 @@ object Build {
671672
}
672673
val dottyInterfaces = jars("dotty-interfaces")
673674
val dottyStaging = jars("dotty-staging")
675+
val dottyTastyConsumer = jars("dotty-tasty-consumer")
674676
val tastyCore = jars("tasty-core")
675677
val asm = findArtifactPath(externalDeps, "scala-asm")
676-
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, tastyCore)
678+
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyConsumer, tastyCore)
677679
}
678680

679681
val fullArgs = main :: insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator))
@@ -715,14 +717,18 @@ object Build {
715717
)
716718

717719
lazy val bootstrapedDottyCompilerSettings = commonDottyCompilerSettings ++ Seq(
718-
javaOptions += {
720+
javaOptions ++= {
719721
val jars = packageAll.value
720-
"-Ddotty.tests.classes.dottyStaging=" + jars("dotty-staging")
722+
Seq(
723+
"-Ddotty.tests.classes.dottyStaging=" + jars("dotty-staging"),
724+
"-Ddotty.tests.classes.dottyTastyConsumer=" + jars("dotty-tasty-consumer"),
725+
)
721726
},
722727
packageAll := {
723728
packageAll.in(`dotty-compiler`).value ++ Seq(
724729
"dotty-compiler" -> packageBin.in(Compile).value.getAbsolutePath,
725730
"dotty-staging" -> packageBin.in(LocalProject("dotty-staging"), Compile).value.getAbsolutePath,
731+
"dotty-tasty-consumer" -> packageBin.in(LocalProject("dotty-tasty-consumer"), Compile).value.getAbsolutePath,
726732
"tasty-core" -> packageBin.in(LocalProject("tasty-core-bootstrapped"), Compile).value.getAbsolutePath,
727733
)
728734
}
@@ -803,6 +809,20 @@ object Build {
803809
javaOptions := (javaOptions in `dotty-compiler-bootstrapped`).value
804810
)
805811

812+
lazy val `dotty-tasty-consumer` = project.in(file("tasty-consumer")).
813+
withCommonSettings(Bootstrapped).
814+
// FIXME: update comment
815+
// We want the compiler to be present in the compiler classpath when compiling this project but not
816+
// when compiling a project that depends on dotty-staging (see sbt-dotty/sbt-test/sbt-dotty/quoted-example-project),
817+
// but we always need it to be present on the JVM classpath at runtime.
818+
dependsOn(dottyCompiler(Bootstrapped) % "provided").
819+
dependsOn(dottyCompiler(Bootstrapped) % "compile->runtime").
820+
dependsOn(dottyCompiler(Bootstrapped) % "test->test").
821+
settings(commonBootstrappedSettings).
822+
settings(
823+
javaOptions := (javaOptions in `dotty-compiler-bootstrapped`).value
824+
)
825+
806826
lazy val `dotty-sbt-bridge` = project.in(file("sbt-bridge/src")).
807827
// We cannot depend on any bootstrapped project to compile the bridge, since the
808828
// bridge is needed to compile these projects.
@@ -1106,6 +1126,7 @@ object Build {
11061126
publishLocal in `dotty-library-bootstrapped`,
11071127
publishLocal in `tasty-core-bootstrapped`,
11081128
publishLocal in `dotty-staging`,
1129+
publishLocal in `dotty-tasty-consumer`,
11091130
publishLocal in `scala-library`,
11101131
publishLocal in `scala-reflect`,
11111132
publishLocal in `dotty-doc-bootstrapped`,
@@ -1315,7 +1336,7 @@ object Build {
13151336
// FIXME: we do not aggregate `bin` because its tests delete jars, thus breaking other tests
13161337
def asDottyRoot(implicit mode: Mode): Project = project.withCommonSettings.
13171338
aggregate(`dotty-interfaces`, dottyLibrary, dottyCompiler, tastyCore, dottyDoc, `dotty-sbt-bridge`).
1318-
bootstrappedAggregate(`scala-library`, `scala-compiler`, `scala-reflect`, scalap, `dotty-language-server`, `dotty-staging`).
1339+
bootstrappedAggregate(`scala-library`, `scala-compiler`, `scala-reflect`, scalap, `dotty-language-server`, `dotty-staging`, `dotty-tasty-consumer`).
13191340
dependsOn(tastyCore).
13201341
dependsOn(dottyCompiler).
13211342
dependsOn(dottyLibrary).

compiler/src/dotty/tools/dotc/consumetasty/ConsumeTasty.scala renamed to tasty-consumer/src/scala/tasty/file/ConsumeTasty.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.tools.dotc.consumetasty
1+
package scala.tasty.file
22

33
import java.net.URLClassLoader
44

@@ -14,6 +14,12 @@ import scala.tasty.file.TastyConsumer
1414

1515
object ConsumeTasty {
1616

17+
/** Load and process TASTy files using TASTy reflect
18+
*
19+
* @param classpath Classpath where the classes are located
20+
* @param classes classes to be consumed
21+
* @param tastyConsumer consumer that will process the tasty trees
22+
*/
1723
def apply(classpath: String, classes: List[String], tastyConsumer: TastyConsumer): Unit = {
1824
if (classes.isEmpty)
1925
throw new IllegalArgumentException("Parameter classes should no be empty")
@@ -33,6 +39,7 @@ object ConsumeTasty {
3339
* BEWARE: with exotic enough classloaders, this may not work at all or do
3440
* the wrong thing.
3541
*/
42+
// FIXME factor out
3643
private def classpathFromClassloader(cl: ClassLoader): String = {
3744
@tailrec
3845
def loop(cl: ClassLoader, suffixClasspath: String): String =

compiler/src/dotty/tools/dotc/consumetasty/TastyConsumerPhase.scala renamed to tasty-consumer/src/scala/tasty/file/TastyConsumerPhase.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package dotty.tools.dotc.consumetasty
1+
package scala.tasty.file
22

33
import dotty.tools.dotc.core.Contexts._
44
import dotty.tools.dotc.core.Phases.Phase
55
import dotty.tools.dotc.tastyreflect.ReflectionImpl
66

77
import scala.tasty.file.TastyConsumer
88

9-
class TastyConsumerPhase(consumer: TastyConsumer) extends Phase {
9+
private class TastyConsumerPhase(consumer: TastyConsumer) extends Phase {
1010

1111
override def phaseName: String = "tastyConsumer"
1212

compiler/src/dotty/tools/dotc/consumetasty/TastyFromClass.scala renamed to tasty-consumer/src/scala/tasty/file/TastyFromClass.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package dotty.tools.dotc.consumetasty
1+
package scala.tasty.file
22

33
import dotty.tools.dotc.Run
44
import dotty.tools.dotc.core.Mode
55
import dotty.tools.dotc.core.Contexts.Context
66
import dotty.tools.dotc.core.Phases.Phase
77
import dotty.tools.dotc.fromtasty._
88

9-
import scala.tasty.file.TastyConsumer
10-
11-
class TastyFromClass(consumer: TastyConsumer) extends TASTYCompiler {
9+
private class TastyFromClass(consumer: TastyConsumer) extends TASTYCompiler {
1210

1311
override protected def frontendPhases: List[List[Phase]] =
1412
List(new ReadTasty) :: // Load classes from tasty
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
scala> import scala.quoted._
2+
scala> import scala.quoted.staging._
3+
scala> implicit def toolbox: Toolbox = Toolbox.make(getClass.getClassLoader)
4+
def toolbox: quoted.staging.Toolbox
5+
scala> def v(given QuoteContext) = '{ (if true then Some(1) else None).map(v => v+1) }
6+
def v(given x$1: quoted.QuoteContext): quoted.Expr[Option[Int]]
7+
scala> scala.quoted.staging.withQuoteContext(v.show)
8+
val res0: String = (if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))
9+
scala> scala.quoted.staging.run(v)
10+
val res1: Option[Int] = Some(2)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package scala.quoted.file.repl
2+
3+
import dotty.BootstrappedOnlyTests
4+
import dotty.tools.repl.ReplTest
5+
import dotty.tools.vulpix.TestConfiguration
6+
import org.junit.Test
7+
import org.junit.experimental.categories.Category
8+
9+
/** Runs all tests contained in `staging/test-resources/repl-tasty-consumer` */
10+
class TastyConsumerScriptedReplTests extends ReplTest(withStaging = false) { // FIXME: withStaging?
11+
12+
@Category(Array(classOf[BootstrappedOnlyTests]))
13+
@Test def replTastyConsumerTests = scripts("/repl-tasty-consumer").foreach(testFile)
14+
15+
}

tastydoc/src/dotty/tastydoc/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ object Main {
9393
println("Running Dotty Tastydoc on: " + classes.mkString(" "))
9494
val mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation] = new scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]()
9595
val tc = new TastydocConsumer(mutablePackagesMap)
96-
ConsumeTasty(extraClasspath, classes, tc)
96+
TastyConsumer(extraClasspath, classes, tc)
9797

9898
representations.setSubClasses(mutablePackagesMap)
9999

0 commit comments

Comments
 (0)