Skip to content

Commit 5a1a4f0

Browse files
authored
Merge pull request #6885 from dotty-staging/move-refect-utils-into-TreeUtils
Integrate Reflection.utils into TreeUtils
2 parents ff2a65f + 299b734 commit 5a1a4f0

File tree

4 files changed

+27
-42
lines changed

4 files changed

+27
-42
lines changed

library/src/scala/tasty/Reflection.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,4 @@ class Reflection(val kernel: Kernel)
4040
def typeChecks(code: String)(implicit ctx: Context): Boolean = kernel.typeChecks(code)(ctx)
4141
}
4242

43-
// TODO integrate with TreeUtils
44-
val util: reflect.utils.TreeUtils { val reflect: self.type } = new reflect.utils.TreeUtils {
45-
val reflect: self.type = self
46-
}
47-
4843
}

library/src/scala/tasty/reflect/TreeUtils.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait TreeUtils
66
extends Core
77
with PatternOps
88
with SymbolOps
9-
with TreeOps {
9+
with TreeOps { self: Reflection =>
1010

1111
abstract class TreeAccumulator[X] {
1212

@@ -283,4 +283,29 @@ trait TreeUtils
283283

284284
}
285285

286+
/** Bind the `rhs` to a `val` and use it in `body` */
287+
def let(rhs: Term)(body: Ident => Term): Term = {
288+
import scala.quoted.QuoteContext
289+
delegate for QuoteContext = new QuoteContext(this)
290+
type T // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
291+
implicit val rhsTpe: quoted.Type[T] = rhs.tpe.seal.asInstanceOf[quoted.Type[T]]
292+
val rhsExpr = rhs.seal.cast[T]
293+
val expr = '{
294+
val x = $rhsExpr
295+
${
296+
val id = ('x).unseal.asInstanceOf[Ident]
297+
body(id).seal
298+
}
299+
}
300+
expr.unseal
301+
}
302+
303+
/** Bind the given `terms` to names and use them in the `body` */
304+
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
305+
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
306+
case Nil => body(acc)
307+
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
308+
}
309+
rec(terms, Nil)
310+
}
286311
}

library/src/scala/tasty/reflect/utils/TreeUtils.scala

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

tests/run-with-compiler/tasty-unsafe-let/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Macros {
1010

1111
val rhsTerm = rhs.unseal
1212

13-
import qctx.tasty.util.{let => letTerm}
13+
import qctx.tasty.{let => letTerm}
1414
letTerm(rhsTerm) { rhsId =>
1515
body(rhsId.seal.asInstanceOf[Expr[T]]).unseal // Dangerous uncheked cast!
1616
}.seal.cast[Unit]

0 commit comments

Comments
 (0)