Skip to content

Commit 0700573

Browse files
Merge pull request #10249 from dotty-staging/move-internal-expr-type-in-compiler
Move internal `Expr` and `Type` implementations into the compiler
2 parents add696a + 2c5072e commit 0700573

File tree

10 files changed

+36
-27
lines changed

10 files changed

+36
-27
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ object PickledQuotes {
3939

4040
/** Transform the expression into its fully spliced Tree */
4141
def quotedExprToTree[T](expr: quoted.Expr[T])(using Context): Tree = {
42-
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
42+
val expr1 = expr.asInstanceOf[scala.quoted.internal.Expr]
4343
expr1.checkScopeId(QuoteContextImpl.scopeId)
4444
changeOwnerOfTree(expr1.tree, ctx.owner)
4545
}
4646

4747
/** Transform the expression into its fully spliced TypeTree */
4848
def quotedTypeToTree(tpe: quoted.Type[?])(using Context): Tree = {
49-
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
49+
val tpe1 = tpe.asInstanceOf[scala.quoted.internal.Type]
5050
tpe1.checkScopeId(QuoteContextImpl.scopeId)
5151
changeOwnerOfTree(tpe1.typeTree, ctx.owner)
5252
}
@@ -75,8 +75,8 @@ object PickledQuotes {
7575
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
7676
case Hole(isTerm, idx, args) =>
7777
val reifiedArgs = args.map { arg =>
78-
if (arg.isTerm) (using qctx: QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContextImpl.scopeId)
79-
else new scala.internal.quoted.Type(arg, QuoteContextImpl.scopeId)
78+
if (arg.isTerm) (using qctx: QuoteContext) => new scala.quoted.internal.Expr(arg, QuoteContextImpl.scopeId)
79+
else new scala.quoted.internal.Type(arg, QuoteContextImpl.scopeId)
8080
}
8181
if isTerm then
8282
val quotedExpr = pickledQuote.exprSplice(idx)(reifiedArgs)(dotty.tools.dotc.quoted.QuoteContextImpl())

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
7272
case _ => false
7373
def asExpr: scala.quoted.Expr[Any] =
7474
if self.isExpr then
75-
new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode)
75+
new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode)
7676
else self match
7777
case TermTypeTest(self) => throw new Exception("Expected an expression. This is a partially applied Term. Try eta-expanding the term first.")
7878
case _ => throw new Exception("Expected a Term but was: " + self)
@@ -316,11 +316,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
316316
object TermMethodsImpl extends TermMethods:
317317
extension (self: Term):
318318
def seal: scala.quoted.Expr[Any] =
319-
if self.isExpr then new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode)
319+
if self.isExpr then new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode)
320320
else throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
321321

322322
def sealOpt: Option[scala.quoted.Expr[Any]] =
323-
if self.isExpr then Some(new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode))
323+
if self.isExpr then Some(new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode))
324324
else None
325325

326326
def tpe: TypeRepr = self.tpe
@@ -1003,7 +1003,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
10031003

10041004
object TypeTree extends TypeTreeModule:
10051005
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree =
1006-
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree
1006+
tp.asInstanceOf[scala.quoted.internal.Type].typeTree
10071007
end TypeTree
10081008

10091009
object TypeTreeMethodsImpl extends TypeTreeMethods:
@@ -1572,7 +1572,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
15721572

15731573
object TypeRepr extends TypeReprModule:
15741574
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeRepr =
1575-
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
1575+
tp.asInstanceOf[scala.quoted.internal.Type].typeTree.tpe
15761576
def typeConstructorOf(clazz: Class[?]): TypeRepr =
15771577
if (clazz.isPrimitive)
15781578
if (clazz == classOf[Boolean]) dotc.core.Symbols.defn.BooleanType
@@ -1609,7 +1609,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
16091609
def seal: scala.quoted.Type[_] = self.asType
16101610

16111611
def asType: scala.quoted.Type[?] =
1612-
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
1612+
new scala.quoted.internal.Type(Inferred(self), QuoteContextImpl.this.hashCode)
16131613

16141614
def =:=(that: TypeRepr): Boolean = self =:= that
16151615
def <:<(that: TypeRepr): Boolean = self <:< that
@@ -2624,11 +2624,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
26242624

26252625
def unpickleExpr[T](pickledQuote: PickledQuote): scala.quoted.Expr[T] =
26262626
val tree = PickledQuotes.unpickleTerm(pickledQuote)(using reflect.rootContext)
2627-
new scala.internal.quoted.Expr(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
2627+
new scala.quoted.internal.Expr(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
26282628

26292629
def unpickleType[T <: AnyKind](pickledQuote: PickledQuote): scala.quoted.Type[T] =
26302630
val tree = PickledQuotes.unpickleTypeTree(pickledQuote)(using reflect.rootContext)
2631-
new scala.internal.quoted.Type(tree, hash).asInstanceOf[scala.quoted.Type[T]]
2631+
new scala.quoted.internal.Type(tree, hash).asInstanceOf[scala.quoted.Type[T]]
26322632

26332633
object ExprMatch extends ExprMatchModule:
26342634
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutinee: scala.quoted.Expr[Any])(using pattern: scala.quoted.Expr[Any]): Option[Tup] =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ object Splicer {
323323
}
324324

325325
private def interpretQuote(tree: Tree)(implicit env: Env): Object =
326-
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)
326+
new scala.quoted.internal.Expr(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)
327327

328328
private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
329-
new scala.internal.quoted.Type(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)
329+
new scala.quoted.internal.Type(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)
330330

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

library/src-bootstrapped/scala/internal/quoted/Expr.scala renamed to compiler/src/scala/quoted/internal/Expr.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package scala.internal.quoted
1+
package scala.quoted.internal
22

33
import scala.quoted._
44

5+
import dotty.tools.dotc.ast.tpd
6+
57
/** An Expr backed by a tree. Only the current compiler trees are allowed.
68
*
79
* These expressions are used for arguments of macros. They contain and actual tree
810
* from the program that is being expanded by the macro.
911
*
1012
* May contain references to code defined outside this Expr instance.
1113
*/
12-
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
14+
final class Expr(val tree: tpd.Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
1315
override def equals(that: Any): Boolean = that match {
14-
case that: Expr[_] =>
16+
case that: Expr =>
1517
// Expr are wrappers around trees, therefore they are equals if their trees are equal.
1618
// All scopeId should be equal unless two different runs of the compiler created the trees.
1719
tree == that.tree && scopeId == that.scopeId
@@ -24,7 +26,7 @@ final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Ex
2426

2527
def checkScopeId(expectedScopeId: Int): Unit =
2628
if expectedScopeId != scopeId then
27-
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
29+
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2830

2931
override def hashCode: Int = tree.hashCode
3032
override def toString: String = "'{ ... }"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package scala.quoted.internal
2+
3+
class ScopeException(msg: String) extends Exception(msg)

library/src-bootstrapped/scala/internal/quoted/Type.scala renamed to compiler/src/scala/quoted/internal/Type.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package scala.internal.quoted
1+
package scala.quoted.internal
22

33
import scala.quoted._
44

5+
import dotty.tools.dotc.ast.tpd
6+
57
/** Quoted type (or kind) `T` backed by a tree */
6-
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
8+
final class Type(val typeTree: tpd.Tree, val scopeId: Int) extends scala.quoted.Type[?] {
79
override def equals(that: Any): Boolean = that match {
8-
case that: Type[_] => typeTree ==
10+
case that: Type => typeTree ==
911
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
1012
// All scopeId should be equal unless two different runs of the compiler created the trees.
1113
that.typeTree && scopeId == that.scopeId
@@ -19,7 +21,7 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
1921

2022
def checkScopeId(expectedScopeId: Int): Unit =
2123
if expectedScopeId != scopeId then
22-
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
24+
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2325

2426
override def hashCode: Int = typeTree.hashCode
2527
override def toString: String = "'[ ... ]"

staging/src/scala/quoted/staging/Toolbox.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package staging
33

44
import scala.annotation.implicitNotFound
55

6+
import scala.quoted.internal.ScopeException
7+
68
@implicitNotFound("Could not find implicit scala.quoted.staging.Toolbox.\n\nDefault toolbox can be instantiated with:\n `given scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(getClass.getClassLoader)`\n\n")
79
trait Toolbox:
810
def run[T](expr: QuoteContext => Expr[T]): T
@@ -31,7 +33,7 @@ object Toolbox:
3133
def run[T](exprBuilder: QuoteContext => Expr[T]): T = synchronized {
3234
try
3335
if (running) // detected nested run
34-
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
36+
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
3537
running = true
3638
driver.run(exprBuilder, settings)
3739
finally

tests/run-staging/i4730.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Test {
55
given Toolbox = Toolbox.make(getClass.getClassLoader)
66
def ret(using QuoteContext): Expr[Int => Int] = '{ (x: Int) =>
77
${
8-
val z = run('{x + 1}) // throws Exception("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
8+
val z = run('{x + 1}) // throws scala.quoted.internal.ScopeException =>
99
Expr(z)
1010
}
1111
}
@@ -21,7 +21,7 @@ package scala {
2121
run(Test.ret).apply(10)
2222
throw new Exception
2323
} catch {
24-
case ex: Exception if ex.getMessage == "Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`" =>
24+
case ex: Exception if ex.getClass.getName == "scala.quoted.internal.ScopeException" =>
2525
// ok
2626
}
2727
}

tests/run-staging/i6754.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package scala {
2222
throw new Exception
2323
} catch {
2424
case ex: java.lang.reflect.InvocationTargetException =>
25-
assert(ex.getTargetException.getMessage == "Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
25+
assert(ex.getTargetException.getClass.getName == "scala.quoted.internal.ScopeException")
2626
}
2727
}
2828
}

tests/run-staging/i6992/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package scala {
2525
case '{$x: Foo} => Expr(run(x).x)
2626
}
2727
} catch {
28-
case ex: Exception if ex.getMessage == "Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`" =>
28+
case ex: Exception if ex.getClass.getName == "scala.quoted.internal.ScopeException" =>
2929
'{"OK"}
3030
}
3131
}

0 commit comments

Comments
 (0)