Skip to content

Commit 901cd0f

Browse files
committed
WIP fix after rebase
1 parent 2d85dba commit 901cd0f

File tree

13 files changed

+132
-13
lines changed

13 files changed

+132
-13
lines changed
Submodule scalatest updated 670 files
Submodule stdLib213 updated 547 files

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
165165
case Some(l) =>
166166
l == level ||
167167
level == -1 && (
168-
sym == defn.TastyReflection_macroContext ||
168+
sym == defn.StagingContext_macroContext ||
169169
// here we assume that Splicer.canBeSpliced was true before going to level -1,
170170
// this implies that all non-inline arguments are quoted and that the following two cases are checked
171171
// on inline parameters or type parameters.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.quoted
2+
3+
import scala.tasty.Reflection
4+
5+
import scala.annotation.{implicitAmbiguous, implicitNotFound}
6+
7+
// TODO add @implicitAmbiguous("...")
8+
// TODO add some fake default and give better error message in `Staging` compiler phase
9+
@implicitNotFound("Could not find an implicit StagingContext.\nIf this is a method that returns an `Expr[T]` you can use `Staged[T]` instead.\n\nQuotedContex is provided inside of top level splices in `inline` macros or within a call to `Toolbox.run`.\n")
10+
trait StagingContext extends scala.runtime.quoted.Unpickler {
11+
def show[T](expr: Expr[T]): String
12+
def show[T](tpe: Type[T]): String
13+
14+
/** AST reflection API. Provides low level reflection capabilities on the definition of the TASTy trees.
15+
*
16+
* Waring: Using this API can break the static type safety of quotes and splices.
17+
* Error will be thrown at reflection time.
18+
*/
19+
val reflection: Reflection
20+
}
21+
22+
object StagingContext {
23+
/** Compiler `StagingContext` available in a top level `~` of an inline macro */
24+
implicit def macroContext: StagingContext = throw new Exception("Not in inline macro.")
25+
}

library/src/scala/quoted/Toolbox.scala renamed to library/src-2.x/scala/quoted/Toolbox.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ trait Toolbox {
44
def run[T](code: Staged[T]): T = runImpl(code)
55
protected def runImpl[T](code: StagingContext => Expr[T]): T // For Scala2 compat in ToolboxImpl
66

7-
def show[T](code: Staged[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; code(ctx).show.toExpr })
8-
def show[T](code: StagedType[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; code(ctx).show.toExpr })
7+
// def show[T](code: Staged[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; code(ctx).show.toExpr })
8+
// def show[T](code: StagedType[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; code(ctx).show.toExpr })
99
}
1010

1111
object Toolbox {

library/src-non-bootstrapped/scala/quoted/package.scala renamed to library/src-2.x/scala/quoted/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package scala
22

33
package object quoted {
44

5-
type Staged[T] = /*implicit*/ StagingContext => Expr[T]
5+
type Staged[T] = /*given*/ StagingContext => Expr[T]
66

7-
type StagedType[T] = /*implicit*/ StagingContext => Type[T]
7+
type StagedType[T] = /*given*/ StagingContext => Type[T]
88

99
implicit class LiftExprOps[T](val x: T) extends AnyVal {
1010
def toExpr(implicit ev: Liftable[T], st: StagingContext): Expr[T] = ev.toExpr(x)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package scala.quoted
2+
3+
trait Toolbox {
4+
def run[T](code: Staged[T]): T = runImpl(sc => code given sc)
5+
protected def runImpl[T](code: StagingContext => Expr[T]): T // For Scala2 compat in ToolboxImpl
6+
7+
// def show[T](code: Staged[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; (code given ctx).show.toExpr })
8+
// def show[T](code: StagedType[T]): String = runImpl(ctx => { implicit val c: StagingContext = ctx; (code given ctx).show.toExpr })
9+
}
10+
11+
object Toolbox {
12+
13+
/** Create a new instance of the toolbox using the the classloader of the application.
14+
*
15+
* Usuage:
16+
* ```
17+
* implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
18+
* ```
19+
*
20+
* @param appClassloader classloader of the application that generated the quotes
21+
* @param settings toolbox settings
22+
* @return A new instance of the toolbox
23+
*/
24+
def make(appClassloader: ClassLoader)(implicit settings: Settings): Toolbox = {
25+
try {
26+
val toolboxImplCls = appClassloader.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
27+
val makeMeth = toolboxImplCls.getMethod("make", classOf[Settings], classOf[ClassLoader])
28+
makeMeth.invoke(null, settings, appClassloader).asInstanceOf[Toolbox]
29+
}
30+
catch {
31+
case ex: ClassNotFoundException =>
32+
throw new ToolboxNotFoundException(
33+
s"""Could not load the Toolbox class `${ex.getMessage}` from the JVM classpath. Make sure that the compiler is on the JVM classpath.""",
34+
ex
35+
)
36+
}
37+
}
38+
39+
/** Setting of the Toolbox instance. */
40+
case class Settings private (outDir: Option[String], showRawTree: Boolean, compilerArgs: List[String], color: Boolean)
41+
42+
object Settings {
43+
44+
implicit def default: Settings = make()
45+
46+
/** Make toolbox settings
47+
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
48+
* @param color Print output with colors
49+
* @param showRawTree Do not remove quote tree artifacts
50+
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
51+
*/
52+
def make( // TODO avoid using default parameters (for binary compat)
53+
color: Boolean = false,
54+
showRawTree: Boolean = false,
55+
outDir: Option[String] = None,
56+
compilerArgs: List[String] = Nil
57+
): Settings =
58+
new Settings(outDir, showRawTree, compilerArgs, color)
59+
}
60+
61+
class ToolboxNotFoundException(msg: String, cause: ClassNotFoundException) extends Exception(msg, cause)
62+
}

library/src-3.x/scala/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Type {
1313

1414
implicit class TypeOps[T](tpe: Type[T]) {
1515
/** Show a source code like representation of this type */
16-
def show(implicit toolbox: Toolbox): String = toolbox.show(tpe.asInstanceOf[Type[Any]])
16+
def show given StagingContext: String = the[StagingContext].show(tpe)
1717
}
1818

1919
implicit def UnitTag: Type[Unit] = new TaggedType[Unit]

library/src-3.x/scala/quoted/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package scala
22

33
package object quoted {
44

5-
type Staged[T] = implicit StagingContext => Expr[T]
5+
type Staged[T] = given StagingContext => Expr[T]
66

7-
type StagedType[T] = implicit StagingContext => Type[T]
7+
type StagedType[T] = given StagingContext => Type[T]
88

99
object autolift {
10-
implicit def autoToExpr[T: Liftable](x: T): Expr[T] = x.toExpr
10+
implicit def autoToExpr[T: Liftable](x: T) given StagingContext: Expr[T] = x.toExpr
1111
}
1212

1313
implicit class LiftExprOps[T](val x: T) extends AnyVal {

library/src-3.x/scala/testing/typeChecks.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import scala.tasty.Reflection
55

66
inline def typeChecks(inline code: String): Boolean = ${ typeChecksImpl(code) }
77

8-
private def typeChecksImpl(code: String)(implicit reflect: Reflection): Expr[Boolean] = {
8+
private def typeChecksImpl(code: String) given StagingContext: Expr[Boolean] = {
9+
val reflect = the[StagingContext].reflection
910
import reflect._
1011
typing.typeChecks(code).toExpr
1112
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.quoted
2+
3+
import scala.tasty.Reflection
4+
5+
import scala.annotation.{implicitAmbiguous, implicitNotFound}
6+
7+
// TODO add @implicitAmbiguous("...")
8+
// TODO add some fake default and give better error message in `Staging` compiler phase
9+
@implicitNotFound("Could not find an implicit StagingContext.\nIf this is a method that returns an `Expr[T]` you can use `Staged[T]` instead.\n\nQuotedContex is provided inside of top level splices in `inline` macros or within a call to `Toolbox.run`.\n")
10+
trait StagingContext extends scala.runtime.quoted.Unpickler {
11+
def show[T](expr: Expr[T]): String
12+
def show[T](tpe: Type[T]): String
13+
14+
/** AST reflection API. Provides low level reflection capabilities on the definition of the TASTy trees.
15+
*
16+
* Waring: Using this API can break the static type safety of quotes and splices.
17+
* Error will be thrown at reflection time.
18+
*/
19+
val reflection: Reflection
20+
}
21+
22+
object StagingContext {
23+
/** Compiler `StagingContext` available in a top level `~` of an inline macro */
24+
def macroContext: StagingContext = throw new Exception("Not in inline macro.")
25+
}

library/src/scala/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Expr {
99

1010
implicit class ExprOps[T](expr: Expr[T]) {
1111
/** Show a source code like representation of this expression */
12-
def show(implicit toolbox: Toolbox): String = toolbox.show(expr)
12+
def show(implicit stCtx: StagingContext): String = stCtx.show(expr)
1313
}
1414

1515
implicit class AsFunction0[R](private val f: Expr[() => R]) extends AnyVal {

library/src/scala/tasty/Reflection.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ class Reflection(val kernel: Kernel)
4242
val reflect: self.type = self
4343
}
4444
}
45+
46+
object Reflection {
47+
/** Compiler tasty context available in a top level ~ of an inline macro */
48+
@deprecated("use StagingContext.macroContext")
49+
def macroContext: Reflection = throw new Exception("Not in inline macro.") // TODO remove when reference compiler is updated
50+
}

0 commit comments

Comments
 (0)