Skip to content

Add tasty.reflect.utils #5685

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 1 commit into from
Jan 17, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {
}
}
}

def TypeToQuoteDeco(tpe: Types.Type): TypeToQuotedAPI = new TypeToQuotedAPI {
def seal(implicit ctx: Context): quoted.Type[_] = {
val dummyPos = ctx.owner.pos // FIXME
new scala.quoted.Types.TreeType(tpd.TypeTree(tpe).withPos(dummyPos))
}
}
}
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Staging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ class Staging extends MacroTransformWithImplicits {
val rhs = transform(tag.select(tpnme.UNARY_~))
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs)

val original = typeRef.symbol.asType

val local = ctx.newSymbol(
owner = ctx.owner,
name = UniqueName.fresh("T".toTermName).toTypeName,
Expand Down
22 changes: 22 additions & 0 deletions library/src-bootstrapped/scala/tasty/reflect/utils/TreeUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package scala.tasty
package reflect.utils

import scala.quoted._

trait TreeUtils {

val reflect: Reflection
import reflect._

def let(rhs: Term)(in: Term.Ident => Term): Term = {
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[T]
val expr = '{
val x = ~rhsExpr
~in(('(x)).unseal.asInstanceOf[Term.Ident]).seal[Any]
}
expr.unseal
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scala.tasty
package reflect.utils

trait TreeUtils {

val reflect: Reflection
import reflect._

def let(rhs: Term)(in: Term.Ident => Term): Term = throw new Exception("non bootstrpped lib")

}
6 changes: 5 additions & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ abstract class Reflection
with TreeOps
with TreeUtils
with TypeOrBoundsTreeOps
with TypeOrBoundsOps {
with TypeOrBoundsOps { self =>

def typeOf[T: scala.quoted.Type]: Type =
implicitly[scala.quoted.Type[T]].unseal.tpe

val util: reflect.utils.TreeUtils { val reflect: self.type } = new reflect.utils.TreeUtils {
val reflect: self.type = self
}
}

object Reflection {
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/tasty/reflect/QuotedOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ trait QuotedOps extends Core {
}
implicit def TermToQuoteDeco(term: Term): TermToQuotedAPI

trait TypeToQuotedAPI {
/** Convert `Type` to an `quoted.Type[T]` */
def seal(implicit ctx: Context): scala.quoted.Type[_]
}
implicit def TypeToQuoteDeco(tpe: Type): TypeToQuotedAPI
}
7 changes: 7 additions & 0 deletions tests/run-with-compiler/tasty-unsafe-let.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1
1
null
null
foo
bar
bar
22 changes: 22 additions & 0 deletions tests/run-with-compiler/tasty-unsafe-let/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.quoted._

import scala.tasty._

object Macros {

inline def let[T](rhs: T)(body: => T => Unit): Unit =
~impl('(rhs), '(body))

private def impl[T](rhs: Expr[T], body: Expr[T => Unit])(implicit reflect: Reflection): Expr[Unit] = {
import reflect._

val rhsTerm = rhs.unseal

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


}
17 changes: 17 additions & 0 deletions tests/run-with-compiler/tasty-unsafe-let/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import Macros._

object Test {
def main(args: Array[String]): Unit = {
let(1)(x => { println(x); println(x) })
let(null)(x => { println(x); println(x) })
let {
println("foo")
"bar"
} { x =>
println(x)
println(x)
}
}

}