diff --git a/library/src-bootstrapped/scala/quoted/Type.scala b/library/src-bootstrapped/scala/quoted/Type.scala index 243b63b3889a..2ef786907296 100644 --- a/library/src-bootstrapped/scala/quoted/Type.scala +++ b/library/src-bootstrapped/scala/quoted/Type.scala @@ -6,14 +6,16 @@ import scala.runtime.quoted.Unpickler.Pickled sealed abstract class Type[T <: AnyKind] { type `$splice` = T - - /** Show a source code like representation of this type */ - final def show(implicit toolbox: Toolbox): String = toolbox.show(this.asInstanceOf[Type[Any]]) } /** Some basic type tags, currently incomplete */ object Type { + implicit class TypeOps[T](tpe: Type[T]) { + /** Show a source code like representation of this type */ + def show(implicit toolbox: Toolbox): String = toolbox.show(tpe.asInstanceOf[Type[Any]]) + } + implicit def UnitTag: Type[Unit] = new TaggedType[Unit] implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean] implicit def ByteTag: Type[Byte] = new TaggedType[Byte] diff --git a/library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala b/library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala index 3b2d471599d4..1bdacf395350 100644 --- a/library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala +++ b/library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala @@ -1,7 +1,7 @@ package scala.tasty.reflect /** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */ -trait QuotedOps extends Core { +trait QuotedOps extends Core { self: Printers => implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) { /** View this expression `quoted.Expr[T]` as a `Term` */ @@ -11,12 +11,20 @@ trait QuotedOps extends Core { /** Checked cast to a `quoted.Expr[U]` */ def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] = kernel.QuotedExpr_cast[U](expr) + + /** Show a source code like representation of this expression */ + def show(implicit ctx: Context): String = + unseal.showCode } implicit class QuotedTypeAPI[T <: AnyKind](tpe: scala.quoted.Type[T]) { /** View this expression `quoted.Type[T]` as a `TypeTree` */ def unseal(implicit ctx: Context): TypeTree = kernel.QuotedType_unseal(tpe) + + /** Show a source code like representation of this type */ + def show(implicit ctx: Context): String = + unseal.showCode } implicit class TermToQuotedAPI(term: Term) { diff --git a/library/src-non-bootstrapped/scala/quoted/Type.scala b/library/src-non-bootstrapped/scala/quoted/Type.scala index 0dfc0f944f16..206863ed2c41 100644 --- a/library/src-non-bootstrapped/scala/quoted/Type.scala +++ b/library/src-non-bootstrapped/scala/quoted/Type.scala @@ -6,13 +6,16 @@ import scala.runtime.quoted.Unpickler.Pickled sealed abstract class Type[T] { type `$splice` = T - - /** Show a source code like representation of this type */ - final def show(implicit toolbox: Toolbox): String = toolbox.show(this) } /** Some basic type tags, currently incomplete */ object Type { + + implicit class TypeOps[T](tpe: Type[T]) { + /** Show a source code like representation of this type */ + def show(implicit toolbox: Toolbox): String = toolbox.show(tpe.asInstanceOf[Type[Any]]) + } + implicit def UnitTag: Type[Unit] = new TaggedType[Unit] implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean] implicit def ByteTag: Type[Byte] = new TaggedType[Byte] diff --git a/library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala b/library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala index 2c01fe9d5aec..5c9a289d254e 100644 --- a/library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala +++ b/library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala @@ -1,7 +1,7 @@ package scala.tasty.reflect /** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */ -trait QuotedOps extends Core { +trait QuotedOps extends Core { self: Printers => implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) { /** View this expression `quoted.Expr[T]` as a `Term` */ @@ -11,12 +11,20 @@ trait QuotedOps extends Core { /** Checked cast to a `quoted.Expr[U]` */ def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] = kernel.QuotedExpr_cast[U](expr) + + /** Show a source code like representation of this expression */ + def show(implicit ctx: Context): String = + unseal.showCode } implicit class QuotedTypeAPI[T](tpe: scala.quoted.Type[T]) { /** View this expression `quoted.Type[T]` as a `TypeTree` */ def unseal(implicit ctx: Context): TypeTree = kernel.QuotedType_unseal(tpe) + + /** Show a source code like representation of this type */ + def show(implicit ctx: Context): String = + unseal.showCode } implicit class TermToQuotedAPI(term: Term) { diff --git a/library/src/scala/quoted/Expr.scala b/library/src/scala/quoted/Expr.scala index c15cad31e08d..79259718051c 100644 --- a/library/src/scala/quoted/Expr.scala +++ b/library/src/scala/quoted/Expr.scala @@ -10,15 +10,17 @@ sealed abstract class Expr[+T] { */ final def run(implicit toolbox: Toolbox): T = toolbox.run(this) - /** Show a source code like representation of this expression */ - final def show(implicit toolbox: Toolbox): String = toolbox.show(this) - } object Expr { // TODO simplify using new extension methods + implicit class ExprOps[T](expr: Expr[T]) { + /** Show a source code like representation of this expression */ + def show(implicit toolbox: Toolbox): String = toolbox.show(expr) + } + implicit class AsFunction0[R](private val f: Expr[() => R]) extends AnyVal { def apply(): Expr[R] = new Exprs.FunctionAppliedTo[R](f, Array.empty) } diff --git a/tests/neg/tasty-string-interpolator-position-b/Macro_1.scala b/tests/neg/tasty-string-interpolator-position-b/Macro_1.scala index 83f2eb139136..e5f48ed5ed62 100644 --- a/tests/neg/tasty-string-interpolator-position-b/Macro_1.scala +++ b/tests/neg/tasty-string-interpolator-position-b/Macro_1.scala @@ -10,7 +10,6 @@ object Macro { } object FIntepolator { - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[String] = { import reflect._ error("there are no args", argsExpr.unseal.underlyingArgument.pos) diff --git a/tests/run-with-compiler/quote-impure-by-name/quoted_1.scala b/tests/run-with-compiler/quote-impure-by-name/quoted_1.scala index 1b89b45714b4..947521db9f98 100644 --- a/tests/run-with-compiler/quote-impure-by-name/quoted_1.scala +++ b/tests/run-with-compiler/quote-impure-by-name/quoted_1.scala @@ -1,6 +1,8 @@ import scala.quoted._ import scala.quoted.autolift._ +import scala.tasty.Reflection + class Index[K, Keys](val index: String) extends AnyVal { override def toString: String = index } @@ -8,10 +10,10 @@ object Index { implicit def zero[K, T]: Index[K, (K, T)] = new Index("0") - implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ${ succImpl('prev)('[K], '[H], '[T]) } + implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ${ succImpl[K, H, T]('prev) } - def succImpl[K, H, T](prev: Expr[Index[K, T]])(implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = { - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) + def succImpl[K, H, T](prev: Expr[Index[K, T]])(implicit k: Type[K], h: Type[H], t: Type[T], relection: Reflection): Expr[Index[K, (H, T)]] = { + import relection._ val value = s"1 + {${prev.show}}" '{new Index(${value})} } diff --git a/tests/run-with-compiler/quote-inline-function.check b/tests/run-with-compiler/quote-inline-function.check index bb2c20fd6091..6d758e5c70ca 100644 --- a/tests/run-with-compiler/quote-inline-function.check +++ b/tests/run-with-compiler/quote-inline-function.check @@ -7,12 +7,11 @@ Normal function f.apply(x$1) i = i.+(1) } - while ({ + do { val x$2: scala.Int = i f.apply(x$2) i = i.+(1) - i.<(j) - }) () + } while (i.<(j)) } By name function @@ -20,14 +19,13 @@ By name function var i: scala.Int = 0 val j: scala.Int = 5 while (i.<(j)) { - val x$1: scala.Int = i - scala.Predef.println(x$1) + val x$3: scala.Int = i + scala.Predef.println(x$3) i = i.+(1) } - while ({ - val x$2: scala.Int = i - scala.Predef.println(x$2) + do { + val x$4: scala.Int = i + scala.Predef.println(x$4) i = i.+(1) - i.<(j) - }) () + } while (i.<(j)) } diff --git a/tests/run-with-compiler/quote-inline-function/quoted_1.scala b/tests/run-with-compiler/quote-inline-function/quoted_1.scala index eb9b712dfe21..e5d6ebee92f3 100644 --- a/tests/run-with-compiler/quote-inline-function/quoted_1.scala +++ b/tests/run-with-compiler/quote-inline-function/quoted_1.scala @@ -1,13 +1,14 @@ import scala.quoted._ import scala.quoted.autolift._ +import scala.tasty.Reflection object Macros { inline def foreach1(start: Int, end: Int, f: Int => Unit): String = ${impl('start, 'end, 'f)} inline def foreach2(start: Int, end: Int, f: => Int => Unit): String = ${impl('start, 'end, 'f)} - def impl(start: Expr[Int], end: Expr[Int], f: Expr[Int => Unit]): Expr[String] = { - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) + def impl(start: Expr[Int], end: Expr[Int], f: Expr[Int => Unit])(implicit reflect: Reflection): Expr[String] = { + import reflect._ val res = '{ var i = $start val j = $end diff --git a/tests/run-with-compiler/quote-unrolled-foreach.scala b/tests/run-with-compiler/quote-unrolled-foreach.scala index d4cc1bafe742..89cee178f3cd 100644 --- a/tests/run-with-compiler/quote-unrolled-foreach.scala +++ b/tests/run-with-compiler/quote-unrolled-foreach.scala @@ -29,7 +29,7 @@ object Test { println(code4.show) println() - val liftedArray = Array(1, 2, 3, 4) + val liftedArray: Expr[Array[Int]] = Array(1, 2, 3, 4) println(liftedArray.show) println() diff --git a/tests/run/type-show/Macro_1.scala b/tests/run/type-show/Macro_1.scala index cb509769bcba..ddfc00328580 100644 --- a/tests/run/type-show/Macro_1.scala +++ b/tests/run/type-show/Macro_1.scala @@ -4,7 +4,7 @@ import scala.tasty._ object TypeToolbox { inline def show[A]: String = ${ showImpl('[A]) } private def showImpl[A, B](a: Type[A])(implicit refl: Reflection): Expr[String] = { - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(this.getClass.getClassLoader) + import refl._ a.show.toExpr } }