Skip to content

Commit 6a08e09

Browse files
Merge pull request #7080 from dotty-staging/create-staging-lib
Create dotty-staging library
2 parents 78d043a + 13f2fe4 commit 6a08e09

File tree

230 files changed

+206
-212
lines changed

Some content is hidden

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

230 files changed

+206
-212
lines changed

.drone.yml

+1-1
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 ;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test"
43+
- ./project/scripts/sbt ";dotty-bootstrapped/compile ;dotty-bootstrapped/test ;dotty-staging/test ;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test"
4444
- ./project/scripts/bootstrapCmdTests
4545

4646
- name: community_build

build.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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-staging` = Build.`dotty-staging`
1314
val `dotty-language-server` = Build.`dotty-language-server`
1415
val `dotty-bench` = Build.`dotty-bench`
1516
val `dotty-bench-bootstrapped` = Build.`dotty-bench-bootstrapped`
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package dotty.tools.dotc.consumetasty
22

3+
import java.net.URLClassLoader
4+
35
import dotty.tools.dotc
46
import dotty.tools.dotc.core.Contexts._
5-
import dotty.tools.dotc.quoted.QuoteDriver
67

78
import scala.tasty.file.TastyConsumer
89

910
object ConsumeTasty {
11+
1012
def apply(classpath: String, classes: List[String], tastyConsumer: TastyConsumer): Unit = {
1113
if (classes.isEmpty)
1214
throw new IllegalArgumentException("Parameter classes should no be empty")
@@ -16,9 +18,23 @@ object ConsumeTasty {
1618
new TastyFromClass(tastyConsumer)
1719
}
1820

19-
val currentClasspath = QuoteDriver.currentClasspath(getClass.getClassLoader)
21+
val currentClasspath = getCurrentClasspath(getClass.getClassLoader)
2022
import java.io.File.{ pathSeparator => sep }
2123
val args = "-from-tasty" :: "-Yretain-trees" :: "-classpath" :: s"$classpath$sep$currentClasspath" :: classes
2224
(new Consume).process(args.toArray)
2325
}
26+
27+
private def getCurrentClasspath(cl: ClassLoader): String = {
28+
val classpath0 = System.getProperty("java.class.path")
29+
cl match {
30+
case cl: URLClassLoader =>
31+
// Loads the classes loaded by this class loader
32+
// When executing `run` or `test` in sbt the classpath is not in the property java.class.path
33+
import java.nio.file.Paths
34+
val newClasspath = cl.getURLs.map(url => Paths.get(url.toURI).toString)
35+
newClasspath.mkString("", java.io.File.pathSeparator, if (classpath0 == "") "" else java.io.File.pathSeparator + classpath0)
36+
case _ => classpath0
37+
}
38+
}
39+
2440
}

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.core.Types._
1414
import dotty.tools.dotc.core.tasty.TreePickler.Hole
1515
import dotty.tools.dotc.core.tasty.{PositionPickler, TastyPickler, TastyPrinter, TastyString}
1616
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode
17-
import dotty.tools.dotc.quoted.ToolboxImpl
17+
import dotty.tools.dotc.quoted.QuoteContext
1818
import dotty.tools.dotc.tastyreflect.ReflectionImpl
1919

2020
import scala.internal.quoted._
@@ -38,14 +38,14 @@ object PickledQuotes {
3838
/** Transform the expression into its fully spliced Tree */
3939
def quotedExprToTree[T](expr: quoted.Expr[T])(implicit ctx: Context): Tree = {
4040
val expr1 = expr.asInstanceOf[TastyTreeExpr[Tree]]
41-
ToolboxImpl.checkScopeId(expr1.scopeId)
41+
QuoteContext.checkScopeId(expr1.scopeId)
4242
healOwner(expr1.tree)
4343
}
4444

4545
/** Transform the expression into its fully spliced TypeTree */
4646
def quotedTypeToTree(tpe: quoted.Type[_])(implicit ctx: Context): Tree = {
4747
val tpe1 = tpe.asInstanceOf[TreeType[Tree]]
48-
ToolboxImpl.checkScopeId(tpe1.scopeId)
48+
QuoteContext.checkScopeId(tpe1.scopeId)
4949
healOwner(tpe1.typeTree)
5050
}
5151

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import scala.collection.mutable.ListBuffer
3131
import scala.collection.mutable
3232
import config.Printers.pickling
3333
import core.quoted.PickledQuotes
34-
import dotty.tools.dotc.quoted.ToolboxImpl
34+
import dotty.tools.dotc.quoted.QuoteContext
3535

3636
import scala.quoted
3737
import scala.internal.quoted.{TastyTreeExpr, TreeType}
@@ -1275,8 +1275,8 @@ class TreeUnpickler(reader: TastyReader,
12751275
val args = until(end)(readTerm())
12761276
val splice = splices(idx)
12771277
def wrap(arg: Tree) =
1278-
if (arg.isTerm) given (qctx: scala.quoted.QuoteContext) => new TastyTreeExpr(arg, ToolboxImpl.scopeId)
1279-
else new TreeType(arg, ToolboxImpl.scopeId)
1278+
if (arg.isTerm) given (qctx: scala.quoted.QuoteContext) => new TastyTreeExpr(arg, QuoteContext.scopeId)
1279+
else new TreeType(arg, QuoteContext.scopeId)
12801280
val reifiedArgs = args.map(wrap)
12811281
val filled = if (isType) {
12821282
val quotedType = splice.asInstanceOf[Seq[Any] => quoted.Type[_]](reifiedArgs)

compiler/src/dotty/tools/dotc/quoted/ExprCompilationUnit.scala

-9
This file was deleted.

compiler/src/dotty/tools/dotc/quoted/QuoteContext.scala

+12
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ object QuoteContext {
88
def apply() given Context: scala.quoted.QuoteContext =
99
new scala.quoted.QuoteContext(ReflectionImpl(the[Context]))
1010

11+
type ScopeId = Int
12+
13+
private[dotty] def checkScopeId(id: ScopeId) given Context: Unit = {
14+
if (id != scopeId)
15+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
16+
}
17+
18+
// TODO Explore more fine grained scope ids.
19+
// This id can only differentiate scope extrusion from one compiler instance to another.
20+
private[dotty] def scopeId given Context: ScopeId =
21+
the[Context].outersIterator.toList.last.hashCode()
22+
1123
}

compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala

-46
This file was deleted.

compiler/src/dotty/tools/dotc/transform/Splicer.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import dotty.tools.repl.AbstractFileClassLoader
2525

2626
import scala.reflect.ClassTag
2727

28-
import dotty.tools.dotc.quoted.{QuoteContext, ToolboxImpl}
28+
import dotty.tools.dotc.quoted.QuoteContext
2929

3030
/** Utility class to splice quoted expressions */
3131
object Splicer {
@@ -251,10 +251,10 @@ object Splicer {
251251
}
252252

253253
private def interpretQuote(tree: Tree)(implicit env: Env): Object =
254-
new scala.internal.quoted.TastyTreeExpr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), ToolboxImpl.scopeId)
254+
new scala.internal.quoted.TastyTreeExpr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId)
255255

256256
private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
257-
new scala.internal.quoted.TreeType(tree, ToolboxImpl.scopeId)
257+
new scala.internal.quoted.TreeType(tree, QuoteContext.scopeId)
258258

259259
private def interpretLiteral(value: Any)(implicit env: Env): Object =
260260
value.asInstanceOf[Object]

compiler/test/dotty/Properties.scala

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ object Properties {
5050
/** dotty-compiler jar */
5151
def dottyCompiler: String = sys.props("dotty.tests.classes.dottyCompiler")
5252

53+
/** dotty-staging jar */
54+
def dottyStaging: String = sys.props("dotty.tests.classes.dottyStaging")
55+
5356
/** compiler-interface jar */
5457
def compilerInterface: String = sys.props("dotty.tests.classes.compilerInterface")
5558

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting {
3939
implicit val testGroup: TestGroup = TestGroup("compilePosWithCompiler")
4040
aggregateTests(
4141
compileFilesInDir("tests/pos-with-compiler", withCompilerOptions),
42+
compileFilesInDir("tests/pos-staging", withStagingOptions),
4243
compileDir("compiler/src/dotty/tools/dotc/ast", withCompilerOptions),
4344
compileDir("compiler/src/dotty/tools/dotc/config", withCompilerOptions),
4445
compileDir("compiler/src/dotty/tools/dotc/core", withCompilerOptions),
@@ -103,7 +104,10 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting {
103104

104105
@Test def negWithCompiler: Unit = {
105106
implicit val testGroup: TestGroup = TestGroup("compileNegWithCompiler")
106-
compileFilesInDir("tests/neg-with-compiler", withCompilerOptions).checkExpectedErrors()
107+
aggregateTests(
108+
compileFilesInDir("tests/neg-with-compiler", withCompilerOptions),
109+
compileFilesInDir("tests/neg-staging", withStagingOptions),
110+
).checkExpectedErrors()
107111
}
108112

109113
// Run tests -----------------------------------------------------------------
@@ -120,6 +124,7 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting {
120124
implicit val testGroup: TestGroup = TestGroup("runWithCompiler")
121125
aggregateTests(
122126
compileFilesInDir("tests/run-with-compiler", withCompilerOptions),
127+
compileFilesInDir("tests/run-staging", withStagingOptions),
123128
compileDir("tests/run-with-compiler-custom-args/tasty-interpreter", withCompilerOptions)
124129
).checkRuns()
125130
}

compiler/test/dotty/tools/repl/ReplTest.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import org.junit.{After, Before}
1414
import org.junit.Assert._
1515

1616

17-
class ReplTest(withCompiler: Boolean = false, out: ByteArrayOutputStream = new ByteArrayOutputStream) extends ReplDriver(
17+
class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new ByteArrayOutputStream) extends ReplDriver(
1818
Array(
1919
"-classpath",
20-
if (withCompiler)
21-
TestConfiguration.withCompilerClasspath
20+
if (withStaging)
21+
TestConfiguration.withStagingClasspath
2222
else
2323
TestConfiguration.basicClasspath,
2424
"-color:never"),

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

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ object TestConfiguration {
3535
Properties.dottyCompiler
3636
))
3737

38+
lazy val withStagingClasspath =
39+
withCompilerClasspath + File.pathSeparator + mkClasspath(List(Properties.dottyStaging))
40+
3841
def mkClasspath(classpaths: List[String]): String =
3942
classpaths.map({ p =>
4043
val file = new java.io.File(p)
@@ -48,6 +51,8 @@ object TestConfiguration {
4851
val defaultOptions = TestFlags(basicClasspath, commonOptions)
4952
val withCompilerOptions =
5053
defaultOptions.withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
54+
lazy val withStagingOptions =
55+
defaultOptions.withClasspath(withStagingClasspath).withRunClasspath(withStagingClasspath)
5156
val allowDeepSubtypes = defaultOptions without "-Yno-deep-subtypes"
5257
val allowDoubleBindings = defaultOptions without "-Yno-double-bindings"
5358
val picklingOptions = defaultOptions and (

dist/bin/common

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ find_lib () {
107107
DOTTY_COMP=$(find_lib "*dotty-compiler*")
108108
DOTTY_INTF=$(find_lib "*dotty-interfaces*")
109109
DOTTY_LIB=$(find_lib "*dotty-library*")
110+
DOTTY_STAGING=$(find_lib "*dotty-staging*")
110111
SCALA_ASM=$(find_lib "*scala-asm*")
111112
SCALA_LIB=$(find_lib "*scala-library*")
112113
SBT_INTF=$(find_lib "*compiler-interface*")

dist/bin/dotr

+1-1
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$DOTTY_INTF$PSEP$SCALA_ASM"
105+
cp_arg+="$PSEP$DOTTY_COMP$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$DOTTY_STAGING"
106106
fi
107107
eval exec "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${jvm_options[@]}" "${residual_args[@]}"
108108
else

library/src-non-bootstrapped/scala/quoted/package.scala

-51
This file was deleted.

library/src/scala/quoted/Expr.scala

-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@ package scala
33
package quoted {
44

55
import scala.quoted.show.SyntaxHighlight
6-
import scala.quoted.staging.Toolbox
76

87
sealed trait Expr[+T] {
98

10-
/** Evaluate the contents of this expression and return the result.
11-
*
12-
* May throw a FreeVariableError on expressions that came from a macro.
13-
*/
14-
@deprecated("Use scala.quoted.run", "")
15-
final def run(implicit toolbox: Toolbox): T = toolbox.run(_ => this)
16-
179
/** Show a source code like representation of this expression without syntax highlight */
1810
def show(implicit qctx: QuoteContext): String = qctx.show(this, SyntaxHighlight.plain)
1911

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package scala.quoted
2+
3+
/** Excetion thrown when an Expr or Type is used ouside of the scope where it is valid */
4+
class ScopeException(msg: String) extends Exception(msg)

library/src/scala/quoted/staging/RunScopeException.scala

-3
This file was deleted.

library/src/scala/quoted/staging/ToolboxNotFoundException.scala

-3
This file was deleted.

0 commit comments

Comments
 (0)