Skip to content

Commit 95ec081

Browse files
Merge pull request #4673 from dotty-staging/remove-tasty-universe
Remove tasty.Universe and use tasty.Tasty directly
2 parents e3960ae + 59a74e9 commit 95ec081

File tree

30 files changed

+107
-126
lines changed

30 files changed

+107
-126
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,11 @@ class Definitions {
667667
def Unpickler_liftedExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
668668
def Unpickler_unpickleType = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")
669669

670-
lazy val TastyUniverseModule = ctx.requiredModule("scala.tasty.Universe")
671-
def TastyUniverseModuleClass(implicit ctx: Context) = TastyUniverseModule.symbol.asClass
670+
lazy val TastyTopLevelSpliceModule = ctx.requiredModule("scala.tasty.TopLevelSplice")
671+
def TastyTopLevelSpliceModuleClass(implicit ctx: Context) = TastyTopLevelSpliceModule.symbol.asClass
672672

673-
lazy val TastyUniverse_compilationUniverseR = TastyUniverseModule.requiredMethod("compilationUniverse")
674-
def TastyUniverse_compilationUniverse(implicit ctx: Context) = TastyUniverse_compilationUniverseR.symbol
673+
lazy val TastyTopLevelSplice_compilationTopLevelSpliceR = TastyTopLevelSpliceModule.requiredMethod("tastyContext")
674+
def TastyTopLevelSplice_compilationTopLevelSplice(implicit ctx: Context) = TastyTopLevelSplice_compilationTopLevelSpliceR.symbol
675675

676676
lazy val EqType = ctx.requiredClassRef("scala.Eq")
677677
def EqClass(implicit ctx: Context) = EqType.symbol.asClass

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DecompilationPrinter extends Phase {
4141
new TastyPrinter(unit.pickled.head._2).printContents()
4242
} else {
4343
out.println(s"/** Decompiled from $unit */")
44-
out.println(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx))
44+
out.println(new TastyImpl(ctx).showSourceCode.showTree(unit.tpdTree)(ctx))
4545
}
4646
}
4747
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class QuoteDriver extends Driver {
4242
def show(expr: Expr[_], settings: Settings[Show]): String = {
4343
def show(tree: Tree, ctx: Context): String = {
4444
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
45-
TastyImpl.showSourceCode.showTree(tree1)(ctx)
45+
new TastyImpl(ctx).showSourceCode.showTree(tree1)(ctx)
4646
}
4747
withTree(expr, show, settings)
4848
}

compiler/src/dotty/tools/dotc/tastyreflect/CompilationUniverse.scala

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

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import dotty.tools.dotc.reporting.Reporter
1111
import dotty.tools.dotc.reporting.diagnostic.MessageContainer
1212
import dotty.tools.dotc.util.SourcePosition
1313

14-
import scala.{quoted, tasty}
14+
import scala.quoted
1515
import scala.reflect.ClassTag
1616
import scala.tasty.util.{Show, ShowExtractors, ShowSourceCode}
1717

18-
object TastyImpl extends scala.tasty.Tasty {
18+
class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { self =>
1919

2020
// ===== Quotes ===================================================
2121

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
516516
outer.enteredSyms.foreach(registerCapturer)
517517

518518
if (ctx.owner.owner.is(Macro)) {
519-
registerCapturer(defn.TastyUniverse_compilationUniverse)
519+
registerCapturer(defn.TastyTopLevelSplice_compilationTopLevelSplice)
520520
// Force a macro to have the context in first position
521-
forceCapture(defn.TastyUniverse_compilationUniverse)
521+
forceCapture(defn.TastyTopLevelSplice_compilationTopLevelSplice)
522522
// Force all parameters of the macro to be created in the definition order
523523
outer.enteredSyms.reverse.foreach(forceCapture)
524524
}
@@ -649,7 +649,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
649649

650650
private def isStage0Value(sym: Symbol)(implicit ctx: Context): Boolean =
651651
(sym.is(Inline) && sym.owner.is(Macro) && !defn.isFunctionType(sym.info)) ||
652-
sym == defn.TastyUniverse_compilationUniverse // intrinsic value at stage 0
652+
sym == defn.TastyTopLevelSplice_compilationTopLevelSplice // intrinsic value at stage 0
653653

654654
private def liftList(list: List[Tree], tpe: Type)(implicit ctx: Context): Tree = {
655655
list.foldRight[Tree](ref(defn.NilModule)) { (x, acc) =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import dotty.tools.dotc.core.quoted._
1515
import dotty.tools.dotc.core.Types._
1616
import dotty.tools.dotc.core.Symbols._
1717
import dotty.tools.dotc.core.TypeErasure
18-
import dotty.tools.dotc.tastyreflect.CompilationUniverse
18+
import dotty.tools.dotc.tastyreflect.TastyImpl
1919

2020
import scala.util.control.NonFatal
2121
import dotty.tools.dotc.util.Positions.Position
@@ -38,7 +38,7 @@ object Splicer {
3838
val liftedArgs = getLiftedArgs(call, bindings)
3939
val interpreter = new Interpreter(pos, classLoader)
4040
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
41-
val tctx = new CompilationUniverse(ctx)
41+
val tctx = new TastyImpl(ctx)
4242
evaluateMacro(pos) {
4343
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
4444
val evaluated = interpreted.map(lambda => lambda(tctx :: liftedArgs).asInstanceOf[scala.quoted.Expr[Nothing]])

library/src/scala/tasty/Tasty.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ abstract class Tasty { tasty =>
3434
}
3535
implicit def ContextDeco(ctx: Context): ContextAPI
3636

37+
implicit def rootContext: Context
38+
3739
// ===== Id =======================================================
3840

3941
type Id
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scala.tasty
2+
3+
/** Context in a top level ~ at inline site, intrinsically as `import TopLevelSplice._` */
4+
object TopLevelSplice {
5+
/** Compiler tasty context available in a top level ~ at inline site */
6+
implicit def tastyContext: Tasty = throw new Exception("Not in inline macro.")
7+
}

library/src/scala/tasty/Universe.scala

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

library/src/scala/tasty/util/ConstantExtractor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import scala.tasty.Tasty
1818
class ConstantExtractor[T <: Tasty with Singleton](val tasty: T) {
1919
import tasty._
2020

21-
def unapply[T](expr: Expr[T])(implicit ctx: Context): Option[T] = {
21+
def unapply[T](expr: Expr[T]): Option[T] = {
2222
def const(tree: Term): Option[T] = tree match {
2323
case Term.Literal(c) => Some(c.value.asInstanceOf[T])
2424
case Term.Block(Nil, e) => const(e)

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.tasty.util
33
import scala.tasty.Tasty
44

55
class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
6-
import tasty._
6+
import tasty.{rootContext => _, _}
77

88
def showTree(tree: Tree)(implicit ctx: Context): String =
99
new Buffer().visitTree(tree).result()

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package util
44
import scala.annotation.switch
55

66
class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
7-
import tasty._
7+
import tasty.{rootContext => _, _}
88

99
def showTree(tree: Tree)(implicit ctx: Context): String =
1010
(new Buffer).printTree(tree).result()

library/src/scala/tasty/util/TreeAccumulator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.tasty.util
33
import scala.tasty.Tasty
44

55
abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
6-
import tasty._
6+
import tasty.{rootContext => _, _}
77

88
// Ties the knot of the traversal: call `foldOver(x, tree))` to dive in the `tree` node.
99
def foldTree(x: X, tree: Tree)(implicit ctx: Context): X

library/src/scala/tasty/util/TreeTraverser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.tasty.util
33
import scala.tasty.Tasty
44

55
abstract class TreeTraverser[T <: Tasty with Singleton](tasty0: T) extends TreeAccumulator[Unit, T](tasty0) {
6-
import tasty._
6+
import tasty.{rootContext => _, _}
77

88
def traverseTree(tree: Tree)(implicit ctx: Context): Unit = traverseTreeChildren(tree)
99
def traverseTypeTree(tree: TypeOrBoundsTree)(implicit ctx: Context): Unit = traverseTypeTreeChildren(tree)

tests/neg/tasty-macro-assert/quoted_1.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

3-
import scala.tasty.Universe
3+
import scala.tasty._
44

55
object Asserts {
66

@@ -12,11 +12,10 @@ object Asserts {
1212
object Ops
1313

1414
inline def macroAssert(cond: Boolean): Unit =
15-
~impl('(cond))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
15+
~impl('(cond))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1616

17-
def impl(cond: Expr[Boolean])(implicit u: Universe): Expr[Unit] = {
18-
import u._
19-
import u.tasty._
17+
def impl(cond: Expr[Boolean])(implicit tasty: Tasty): Expr[Unit] = {
18+
import tasty._
2019

2120
val tree = cond.toTasty
2221

tests/run/tasty-custom-show/quoted_1.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty.TopLevelSplice
55
import scala.tasty.Tasty
66
import scala.tasty.util.{TreeTraverser, Show}
77

88
object Macros {
99

1010
implicit inline def printOwners[T](x: => T): Unit =
11-
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
12-
13-
def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
14-
import u._
15-
import u.tasty._
11+
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1612

13+
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
14+
import tasty._
1715

1816
val buff = new StringBuilder
1917

20-
val output = new TreeTraverser(u.tasty) {
18+
val output = new TreeTraverser(tasty) {
2119
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
2220
// Use custom Show[_] here
2321
implicit val printer = new DummyShow(tasty)

tests/run/tasty-eval/quoted_1.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import scala.quoted._
22

3-
import scala.tasty.Universe
3+
import scala.tasty._
44
import scala.tasty.util.TreeTraverser
55

66
object Macros {
77

88
implicit inline def foo(i: Int): String =
9-
~impl('(i))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
9+
~impl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1010

11-
def impl(i: Expr[Int])(implicit u: Universe): Expr[String] = {
11+
def impl(i: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
1212
value(i).toString.toExpr
1313
}
1414

15-
inline implicit def value[X](e: Expr[X])(implicit u: Universe, ev: Valuable[X]): Option[X] = ev.value(e)
15+
inline implicit def value[X](e: Expr[X])(implicit tasty: Tasty, ev: Valuable[X]): Option[X] = ev.value(e)
1616

1717
trait Valuable[X] {
18-
def value(e: Expr[X])(implicit u: Universe): Option[X]
18+
def value(e: Expr[X])(implicit tasty: Tasty): Option[X]
1919
}
2020

2121
implicit def intIsEvalable: Valuable[Int] = new Valuable[Int] {
22-
override def value(e: Expr[Int])(implicit u: Universe): Option[Int] = {
23-
import u._
24-
import u.tasty._
22+
override def value(e: Expr[Int])(implicit tasty: Tasty): Option[Int] = {
23+
import tasty._
24+
2525
e.toTasty.tpe match {
2626
case Type.SymRef(ValDef(_, tpt, _), pre) =>
2727
tpt.tpe match {

tests/run/tasty-extractors-1/quoted_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty._
55

66
object Macros {
77

88
implicit inline def printTree[T](x: => T): Unit =
9-
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
9+
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1010

11-
def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
12-
import u._
11+
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1312
import tasty._
13+
1414
val tree = x.toTasty
1515
val treeStr = tree.show
1616
val treeTpeStr = tree.tpe.show

tests/run/tasty-extractors-2/quoted_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty._
55

66
object Macros {
77

88
implicit inline def printTree[T](x: => T): Unit =
9-
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
9+
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1010

11-
def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
12-
import u._
11+
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1312
import tasty._
13+
1414
val tree = x.toTasty
1515

1616
val treeStr = tree.show

tests/run/tasty-extractors-3/quoted_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty.TopLevelSplice
5+
import scala.tasty.Tasty
56
import scala.tasty.util.TreeTraverser
67

78
object Macros {
89

910
implicit inline def printTypes[T](x: => T): Unit =
10-
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
11+
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1112

12-
def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
13-
import u._
14-
import u.tasty._
13+
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
14+
import tasty._
1515

1616
val buff = new StringBuilder
17-
val traverser = new TreeTraverser(u.tasty) {
17+
val traverser = new TreeTraverser(tasty) {
1818
override def traverseTypeTree(tree: TypeOrBoundsTree)(implicit ctx: Context): Unit = {
1919
buff.append(tree.tpe.show)
2020
buff.append("\n\n")

tests/run/tasty-extractors-constants-1/quoted_1.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty._
55
import scala.tasty.util._
66

77
object Macros {
88

99
implicit inline def testMacro: Unit =
10-
~impl(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
10+
~impl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1111

12-
def impl(implicit u: Universe): Expr[Unit] = {
13-
import u._
14-
import u.tasty._
12+
def impl(implicit tasty: Tasty): Expr[Unit] = {
13+
import tasty._
1514

1615
val buff = new StringBuilder
1716
def stagedPrintln(x: Any): Unit = buff append java.util.Objects.toString(x) append "\n"

tests/run/tasty-extractors-constants-2/quoted_1.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import scala.quoted._
22
import dotty.tools.dotc.quoted.Toolbox._
33

4-
import scala.tasty.Universe
4+
import scala.tasty._
55
import scala.tasty.util._
66

77
object Macros {
88

99
inline def testMacro: Unit =
10-
~impl(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
10+
~impl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
1111

12-
def impl(implicit u: Universe): Expr[Unit] = {
12+
def impl(implicit tasty: Tasty): Expr[Unit] = {
1313
// 2 is a lifted constant
14-
val show1 = power(2.toExpr, 3.0.toExpr)(u).show
15-
val run1 = power(2.toExpr, 3.0.toExpr)(u).run
14+
val show1 = power(2.toExpr, 3.0.toExpr).show
15+
val run1 = power(2.toExpr, 3.0.toExpr).run
1616

1717
// n is a lifted constant
1818
val n = 2
19-
val show2 = power(n.toExpr, 4.0.toExpr)(u).show
20-
val run2 = power(n.toExpr, 4.0.toExpr)(u).run
19+
val show2 = power(n.toExpr, 4.0.toExpr).show
20+
val run2 = power(n.toExpr, 4.0.toExpr).run
2121

2222
// n is a constant in a quote
23-
val show3 = power('(2), 5.0.toExpr)(u).show
24-
val run3 = power('(2), 5.0.toExpr)(u).run
23+
val show3 = power('(2), 5.0.toExpr).show
24+
val run3 = power('(2), 5.0.toExpr).run
2525

2626
// n2 is clearly not a constant
2727
// FIXME
@@ -44,10 +44,10 @@ object Macros {
4444
}
4545
}
4646

47-
def power(n: Expr[Int], x: Expr[Double])(implicit u: Universe): Expr[Double] = {
48-
import u._
49-
import u.tasty._
50-
val Constant = new ConstantExtractor(u.tasty)
47+
def power(n: Expr[Int], x: Expr[Double])(implicit tasty: Tasty): Expr[Double] = {
48+
import tasty._
49+
50+
val Constant = new ConstantExtractor(tasty)
5151
n match {
5252
case Constant(n1) => powerCode(n1, x)
5353
case _ => '{ dynamicPower(~n, ~x) }

0 commit comments

Comments
 (0)