Skip to content

Commit 0d0274b

Browse files
Merge pull request #6258 from dotty-staging/use-current-compiler-context-to-show-expr-in-macros
Use current compiler context to show Expr in macros
2 parents eae5a38 + c3cc821 commit 0d0274b

File tree

11 files changed

+52
-29
lines changed

11 files changed

+52
-29
lines changed

library/src-bootstrapped/scala/quoted/Type.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import scala.runtime.quoted.Unpickler.Pickled
66

77
sealed abstract class Type[T <: AnyKind] {
88
type `$splice` = T
9-
10-
/** Show a source code like representation of this type */
11-
final def show(implicit toolbox: Toolbox): String = toolbox.show(this.asInstanceOf[Type[Any]])
129
}
1310

1411
/** Some basic type tags, currently incomplete */
1512
object Type {
1613

14+
implicit class TypeOps[T](tpe: Type[T]) {
15+
/** Show a source code like representation of this type */
16+
def show(implicit toolbox: Toolbox): String = toolbox.show(tpe.asInstanceOf[Type[Any]])
17+
}
18+
1719
implicit def UnitTag: Type[Unit] = new TaggedType[Unit]
1820
implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean]
1921
implicit def ByteTag: Type[Byte] = new TaggedType[Byte]

library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.tasty.reflect
22

33
/** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */
4-
trait QuotedOps extends Core {
4+
trait QuotedOps extends Core { self: Printers =>
55

66
implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
77
/** View this expression `quoted.Expr[T]` as a `Term` */
@@ -11,12 +11,20 @@ trait QuotedOps extends Core {
1111
/** Checked cast to a `quoted.Expr[U]` */
1212
def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] =
1313
kernel.QuotedExpr_cast[U](expr)
14+
15+
/** Show a source code like representation of this expression */
16+
def show(implicit ctx: Context): String =
17+
unseal.showCode
1418
}
1519

1620
implicit class QuotedTypeAPI[T <: AnyKind](tpe: scala.quoted.Type[T]) {
1721
/** View this expression `quoted.Type[T]` as a `TypeTree` */
1822
def unseal(implicit ctx: Context): TypeTree =
1923
kernel.QuotedType_unseal(tpe)
24+
25+
/** Show a source code like representation of this type */
26+
def show(implicit ctx: Context): String =
27+
unseal.showCode
2028
}
2129

2230
implicit class TermToQuotedAPI(term: Term) {

library/src-non-bootstrapped/scala/quoted/Type.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import scala.runtime.quoted.Unpickler.Pickled
66

77
sealed abstract class Type[T] {
88
type `$splice` = T
9-
10-
/** Show a source code like representation of this type */
11-
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
129
}
1310

1411
/** Some basic type tags, currently incomplete */
1512
object Type {
13+
14+
implicit class TypeOps[T](tpe: Type[T]) {
15+
/** Show a source code like representation of this type */
16+
def show(implicit toolbox: Toolbox): String = toolbox.show(tpe.asInstanceOf[Type[Any]])
17+
}
18+
1619
implicit def UnitTag: Type[Unit] = new TaggedType[Unit]
1720
implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean]
1821
implicit def ByteTag: Type[Byte] = new TaggedType[Byte]

library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.tasty.reflect
22

33
/** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */
4-
trait QuotedOps extends Core {
4+
trait QuotedOps extends Core { self: Printers =>
55

66
implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
77
/** View this expression `quoted.Expr[T]` as a `Term` */
@@ -11,12 +11,20 @@ trait QuotedOps extends Core {
1111
/** Checked cast to a `quoted.Expr[U]` */
1212
def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] =
1313
kernel.QuotedExpr_cast[U](expr)
14+
15+
/** Show a source code like representation of this expression */
16+
def show(implicit ctx: Context): String =
17+
unseal.showCode
1418
}
1519

1620
implicit class QuotedTypeAPI[T](tpe: scala.quoted.Type[T]) {
1721
/** View this expression `quoted.Type[T]` as a `TypeTree` */
1822
def unseal(implicit ctx: Context): TypeTree =
1923
kernel.QuotedType_unseal(tpe)
24+
25+
/** Show a source code like representation of this type */
26+
def show(implicit ctx: Context): String =
27+
unseal.showCode
2028
}
2129

2230
implicit class TermToQuotedAPI(term: Term) {

library/src/scala/quoted/Expr.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ sealed abstract class Expr[+T] {
1010
*/
1111
final def run(implicit toolbox: Toolbox): T = toolbox.run(this)
1212

13-
/** Show a source code like representation of this expression */
14-
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
15-
1613
}
1714

1815
object Expr {
1916

2017
// TODO simplify using new extension methods
2118

19+
implicit class ExprOps[T](expr: Expr[T]) {
20+
/** Show a source code like representation of this expression */
21+
def show(implicit toolbox: Toolbox): String = toolbox.show(expr)
22+
}
23+
2224
implicit class AsFunction0[R](private val f: Expr[() => R]) extends AnyVal {
2325
def apply(): Expr[R] = new Exprs.FunctionAppliedTo[R](f, Array.empty)
2426
}

tests/neg/tasty-string-interpolator-position-b/Macro_1.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ object Macro {
1010
}
1111

1212
object FIntepolator {
13-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
1413
def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[String] = {
1514
import reflect._
1615
error("there are no args", argsExpr.unseal.underlyingArgument.pos)

tests/run-with-compiler/quote-impure-by-name/quoted_1.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import scala.quoted._
22
import scala.quoted.autolift._
33

4+
import scala.tasty.Reflection
5+
46
class Index[K, Keys](val index: String) extends AnyVal {
57
override def toString: String = index
68
}
79
object Index {
810

911
implicit def zero[K, T]: Index[K, (K, T)] = new Index("0")
1012

11-
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ${ succImpl('prev)('[K], '[H], '[T]) }
13+
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ${ succImpl[K, H, T]('prev) }
1214

13-
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)]] = {
14-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
15+
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)]] = {
16+
import relection._
1517
val value = s"1 + {${prev.show}}"
1618
'{new Index(${value})}
1719
}

tests/run-with-compiler/quote-inline-function.check

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,25 @@ Normal function
77
f.apply(x$1)
88
i = i.+(1)
99
}
10-
while ({
10+
do {
1111
val x$2: scala.Int = i
1212
f.apply(x$2)
1313
i = i.+(1)
14-
i.<(j)
15-
}) ()
14+
} while (i.<(j))
1615
}
1716

1817
By name function
1918
{
2019
var i: scala.Int = 0
2120
val j: scala.Int = 5
2221
while (i.<(j)) {
23-
val x$1: scala.Int = i
24-
scala.Predef.println(x$1)
22+
val x$3: scala.Int = i
23+
scala.Predef.println(x$3)
2524
i = i.+(1)
2625
}
27-
while ({
28-
val x$2: scala.Int = i
29-
scala.Predef.println(x$2)
26+
do {
27+
val x$4: scala.Int = i
28+
scala.Predef.println(x$4)
3029
i = i.+(1)
31-
i.<(j)
32-
}) ()
30+
} while (i.<(j))
3331
}

tests/run-with-compiler/quote-inline-function/quoted_1.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import scala.quoted._
22
import scala.quoted.autolift._
3+
import scala.tasty.Reflection
34

45
object Macros {
56

67
inline def foreach1(start: Int, end: Int, f: Int => Unit): String = ${impl('start, 'end, 'f)}
78
inline def foreach2(start: Int, end: Int, f: => Int => Unit): String = ${impl('start, 'end, 'f)}
89

9-
def impl(start: Expr[Int], end: Expr[Int], f: Expr[Int => Unit]): Expr[String] = {
10-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
10+
def impl(start: Expr[Int], end: Expr[Int], f: Expr[Int => Unit])(implicit reflect: Reflection): Expr[String] = {
11+
import reflect._
1112
val res = '{
1213
var i = $start
1314
val j = $end

tests/run-with-compiler/quote-unrolled-foreach.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object Test {
2929
println(code4.show)
3030
println()
3131

32-
val liftedArray = Array(1, 2, 3, 4)
32+
val liftedArray: Expr[Array[Int]] = Array(1, 2, 3, 4)
3333
println(liftedArray.show)
3434
println()
3535

tests/run/type-show/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.tasty._
44
object TypeToolbox {
55
inline def show[A]: String = ${ showImpl('[A]) }
66
private def showImpl[A, B](a: Type[A])(implicit refl: Reflection): Expr[String] = {
7-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(this.getClass.getClassLoader)
7+
import refl._
88
a.show.toExpr
99
}
1010
}

0 commit comments

Comments
 (0)