Skip to content

Remove tasty.Universe and use tasty.Tasty directly #4673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,11 @@ class Definitions {
def Unpickler_liftedExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
def Unpickler_unpickleType = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")

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

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

lazy val EqType = ctx.requiredClassRef("scala.Eq")
def EqClass(implicit ctx: Context) = EqType.symbol.asClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DecompilationPrinter extends Phase {
new TastyPrinter(unit.pickled.head._2).printContents()
} else {
out.println(s"/** Decompiled from $unit */")
out.println(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx))
out.println(new TastyImpl(ctx).showSourceCode.showTree(unit.tpdTree)(ctx))
}
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class QuoteDriver extends Driver {
def show(expr: Expr[_], settings: Settings[Show]): String = {
def show(tree: Tree, ctx: Context): String = {
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
TastyImpl.showSourceCode.showTree(tree1)(ctx)
new TastyImpl(ctx).showSourceCode.showTree(tree1)(ctx)
}
withTree(expr, show, settings)
}
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import dotty.tools.dotc.reporting.Reporter
import dotty.tools.dotc.reporting.diagnostic.MessageContainer
import dotty.tools.dotc.util.SourcePosition

import scala.{quoted, tasty}
import scala.quoted
import scala.reflect.ClassTag
import scala.tasty.util.{Show, ShowExtractors, ShowSourceCode}

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

// ===== Quotes ===================================================

Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
outer.enteredSyms.foreach(registerCapturer)

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

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

private def liftList(list: List[Tree], tpe: Type)(implicit ctx: Context): Tree = {
list.foldRight[Tree](ref(defn.NilModule)) { (x, acc) =>
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.TypeErasure
import dotty.tools.dotc.tastyreflect.CompilationUniverse
import dotty.tools.dotc.tastyreflect.TastyImpl

import scala.util.control.NonFatal
import dotty.tools.dotc.util.Positions.Position
Expand All @@ -38,7 +38,7 @@ object Splicer {
val liftedArgs = getLiftedArgs(call, bindings)
val interpreter = new Interpreter(pos, classLoader)
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
val tctx = new CompilationUniverse(ctx)
val tctx = new TastyImpl(ctx)
evaluateMacro(pos) {
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val evaluated = interpreted.map(lambda => lambda(tctx :: liftedArgs).asInstanceOf[scala.quoted.Expr[Nothing]])
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class Tasty { tasty =>
}
implicit def ContextDeco(ctx: Context): ContextAPI

implicit def rootContext: Context

// ===== Id =======================================================

type Id
Expand Down
7 changes: 7 additions & 0 deletions library/src/scala/tasty/TopLevelSplice.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scala.tasty

/** Context in a top level ~ at inline site, intrinsically as `import TopLevelSplice._` */
object TopLevelSplice {
/** Compiler tasty context available in a top level ~ at inline site */
implicit def tastyContext: Tasty = throw new Exception("Not in inline macro.")
}
11 changes: 0 additions & 11 deletions library/src/scala/tasty/Universe.scala

This file was deleted.

2 changes: 1 addition & 1 deletion library/src/scala/tasty/util/ConstantExtractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import scala.tasty.Tasty
class ConstantExtractor[T <: Tasty with Singleton](val tasty: T) {
import tasty._

def unapply[T](expr: Expr[T])(implicit ctx: Context): Option[T] = {
def unapply[T](expr: Expr[T]): Option[T] = {
def const(tree: Term): Option[T] = tree match {
case Term.Literal(c) => Some(c.value.asInstanceOf[T])
case Term.Block(Nil, e) => const(e)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/util/ShowExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.tasty.util
import scala.tasty.Tasty

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

def showTree(tree: Tree)(implicit ctx: Context): String =
new Buffer().visitTree(tree).result()
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package util
import scala.annotation.switch

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

def showTree(tree: Tree)(implicit ctx: Context): String =
(new Buffer).printTree(tree).result()
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/util/TreeAccumulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.tasty.util
import scala.tasty.Tasty

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

// Ties the knot of the traversal: call `foldOver(x, tree))` to dive in the `tree` node.
def foldTree(x: X, tree: Tree)(implicit ctx: Context): X
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/util/TreeTraverser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.tasty.util
import scala.tasty.Tasty

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

def traverseTree(tree: Tree)(implicit ctx: Context): Unit = traverseTreeChildren(tree)
def traverseTypeTree(tree: TypeOrBoundsTree)(implicit ctx: Context): Unit = traverseTypeTreeChildren(tree)
Expand Down
9 changes: 4 additions & 5 deletions tests/neg/tasty-macro-assert/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.quoted._

import scala.tasty.Universe
import scala.tasty._

object Asserts {

Expand All @@ -12,11 +12,10 @@ object Asserts {
object Ops

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

def impl(cond: Expr[Boolean])(implicit u: Universe): Expr[Unit] = {
import u._
import u.tasty._
def impl(cond: Expr[Boolean])(implicit tasty: Tasty): Expr[Unit] = {
import tasty._

val tree = cond.toTasty

Expand Down
12 changes: 5 additions & 7 deletions tests/run/tasty-custom-show/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty.TopLevelSplice
import scala.tasty.Tasty
import scala.tasty.util.{TreeTraverser, Show}

object Macros {

implicit inline def printOwners[T](x: => T): Unit =
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~

def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
import u._
import u.tasty._
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~

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

val buff = new StringBuilder

val output = new TreeTraverser(u.tasty) {
val output = new TreeTraverser(tasty) {
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
// Use custom Show[_] here
implicit val printer = new DummyShow(tasty)
Expand Down
16 changes: 8 additions & 8 deletions tests/run/tasty-eval/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import scala.quoted._

import scala.tasty.Universe
import scala.tasty._
import scala.tasty.util.TreeTraverser

object Macros {

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

def impl(i: Expr[Int])(implicit u: Universe): Expr[String] = {
def impl(i: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
value(i).toString.toExpr
}

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

trait Valuable[X] {
def value(e: Expr[X])(implicit u: Universe): Option[X]
def value(e: Expr[X])(implicit tasty: Tasty): Option[X]
}

implicit def intIsEvalable: Valuable[Int] = new Valuable[Int] {
override def value(e: Expr[Int])(implicit u: Universe): Option[Int] = {
import u._
import u.tasty._
override def value(e: Expr[Int])(implicit tasty: Tasty): Option[Int] = {
import tasty._

e.toTasty.tpe match {
case Type.SymRef(ValDef(_, tpt, _), pre) =>
tpt.tpe match {
Expand Down
8 changes: 4 additions & 4 deletions tests/run/tasty-extractors-1/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty._

object Macros {

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

def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
import u._
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
import tasty._

val tree = x.toTasty
val treeStr = tree.show
val treeTpeStr = tree.tpe.show
Expand Down
8 changes: 4 additions & 4 deletions tests/run/tasty-extractors-2/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty._

object Macros {

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

def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
import u._
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
import tasty._

val tree = x.toTasty

val treeStr = tree.show
Expand Down
12 changes: 6 additions & 6 deletions tests/run/tasty-extractors-3/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty.TopLevelSplice
import scala.tasty.Tasty
import scala.tasty.util.TreeTraverser

object Macros {

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

def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = {
import u._
import u.tasty._
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
import tasty._

val buff = new StringBuilder
val traverser = new TreeTraverser(u.tasty) {
val traverser = new TreeTraverser(tasty) {
override def traverseTypeTree(tree: TypeOrBoundsTree)(implicit ctx: Context): Unit = {
buff.append(tree.tpe.show)
buff.append("\n\n")
Expand Down
9 changes: 4 additions & 5 deletions tests/run/tasty-extractors-constants-1/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty._
import scala.tasty.util._

object Macros {

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

def impl(implicit u: Universe): Expr[Unit] = {
import u._
import u.tasty._
def impl(implicit tasty: Tasty): Expr[Unit] = {
import tasty._

val buff = new StringBuilder
def stagedPrintln(x: Any): Unit = buff append java.util.Objects.toString(x) append "\n"
Expand Down
26 changes: 13 additions & 13 deletions tests/run/tasty-extractors-constants-2/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._

import scala.tasty.Universe
import scala.tasty._
import scala.tasty.util._

object Macros {

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

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

// n is a lifted constant
val n = 2
val show2 = power(n.toExpr, 4.0.toExpr)(u).show
val run2 = power(n.toExpr, 4.0.toExpr)(u).run
val show2 = power(n.toExpr, 4.0.toExpr).show
val run2 = power(n.toExpr, 4.0.toExpr).run

// n is a constant in a quote
val show3 = power('(2), 5.0.toExpr)(u).show
val run3 = power('(2), 5.0.toExpr)(u).run
val show3 = power('(2), 5.0.toExpr).show
val run3 = power('(2), 5.0.toExpr).run

// n2 is clearly not a constant
// FIXME
Expand All @@ -44,10 +44,10 @@ object Macros {
}
}

def power(n: Expr[Int], x: Expr[Double])(implicit u: Universe): Expr[Double] = {
import u._
import u.tasty._
val Constant = new ConstantExtractor(u.tasty)
def power(n: Expr[Int], x: Expr[Double])(implicit tasty: Tasty): Expr[Double] = {
import tasty._

val Constant = new ConstantExtractor(tasty)
n match {
case Constant(n1) => powerCode(n1, x)
case _ => '{ dynamicPower(~n, ~x) }
Expand Down
Loading