Skip to content

Integrate Reflection.utils into TreeUtils #6885

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
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
5 changes: 0 additions & 5 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,4 @@ class Reflection(val kernel: Kernel)
def typeChecks(code: String)(implicit ctx: Context): Boolean = kernel.typeChecks(code)(ctx)
}

// TODO integrate with TreeUtils
val util: reflect.utils.TreeUtils { val reflect: self.type } = new reflect.utils.TreeUtils {
val reflect: self.type = self
}

}
27 changes: 26 additions & 1 deletion library/src/scala/tasty/reflect/TreeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait TreeUtils
extends Core
with PatternOps
with SymbolOps
with TreeOps {
with TreeOps { self: Reflection =>

abstract class TreeAccumulator[X] {

Expand Down Expand Up @@ -283,4 +283,29 @@ trait TreeUtils

}

/** Bind the `rhs` to a `val` and use it in `body` */
def let(rhs: Term)(body: Ident => Term): Term = {
import scala.quoted.QuoteContext
delegate for QuoteContext = new QuoteContext(this)
type T // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
implicit val rhsTpe: quoted.Type[T] = rhs.tpe.seal.asInstanceOf[quoted.Type[T]]
val rhsExpr = rhs.seal.cast[T]
val expr = '{
val x = $rhsExpr
${
val id = ('x).unseal.asInstanceOf[Ident]
body(id).seal
}
}
expr.unseal
}

/** Bind the given `terms` to names and use them in the `body` */
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
case Nil => body(acc)
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
}
rec(terms, Nil)
}
}
35 changes: 0 additions & 35 deletions library/src/scala/tasty/reflect/utils/TreeUtils.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/run-with-compiler/tasty-unsafe-let/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macros {

val rhsTerm = rhs.unseal

import qctx.tasty.util.{let => letTerm}
import qctx.tasty.{let => letTerm}
letTerm(rhsTerm) { rhsId =>
body(rhsId.seal.asInstanceOf[Expr[T]]).unseal // Dangerous uncheked cast!
}.seal.cast[Unit]
Expand Down