Skip to content

Remove scala.quoted.unsafe.UnsafeExpr #10192

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
Nov 6, 2020
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
70 changes: 0 additions & 70 deletions library/src-bootstrapped/scala/quoted/unsafe/UnsafeExpr.scala

This file was deleted.

9 changes: 6 additions & 3 deletions tests/run-macros/i7898/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import quoted._
import quoted.unsafe._
import scala.quoted._

object Main {

def myMacroImpl(body: Expr[_])(using qctx: QuoteContext) : Expr[_] = {
import qctx.reflect._
val bodyTerm = UnsafeExpr.underlyingArgument(body).unseal
val bodyTerm = underlyingArgument(body).unseal
val showed = bodyTerm.show
'{
println(${Expr(showed)})
Expand All @@ -15,4 +15,7 @@ object Main {
transparent inline def myMacro(body: => Any): Any = ${
myMacroImpl('body)
}

def underlyingArgument[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
expr.unseal.underlyingArgument.seal.asInstanceOf[Expr[T]]
}
25 changes: 23 additions & 2 deletions tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import scala.quoted._

import scala.quoted.unsafe._

object Macros {

inline def liftString(inline a: DSL): String = ${implStringNum('a)}
Expand Down Expand Up @@ -64,6 +62,29 @@ object Macros {

}

object UnsafeExpr {
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
}
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
import qctx.reflect._
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
}

private def bodyFn[t](using qctx: QuoteContext)(e: qctx.reflect.Term, params: List[qctx.reflect.ValDef], args: List[qctx.reflect.Term]): qctx.reflect.Term = {
import qctx.reflect._
val map = params.map(_.symbol).zip(args).toMap
new TreeMap {
override def transformTerm(tree: Term)(using ctx: Context): Term =
super.transformTerm(tree) match
case tree: Ident => map.getOrElse(tree.symbol, tree)
case tree => tree
}.transformTerm(e)
}
}

def freshEnvVar()(using QuoteContext): (Int, Expr[DSL]) = {
v += 1
(v, '{envVar(${Expr(v)})})
Expand Down
25 changes: 23 additions & 2 deletions tests/run-macros/quote-matcher-symantics-3/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import scala.quoted._

import scala.quoted.unsafe._

object Macros {


Expand Down Expand Up @@ -76,6 +74,29 @@ object Macros {

}

object UnsafeExpr {
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
}
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
import qctx.reflect._
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
}

private def bodyFn[t](using qctx: QuoteContext)(e: qctx.reflect.Term, params: List[qctx.reflect.ValDef], args: List[qctx.reflect.Term]): qctx.reflect.Term = {
import qctx.reflect._
val map = params.map(_.symbol).zip(args).toMap
new TreeMap {
override def transformTerm(tree: Term)(using ctx: Context): Term =
super.transformTerm(tree) match
case tree: Ident => map.getOrElse(tree.symbol, tree)
case tree => tree
}.transformTerm(e)
}
}

def freshEnvVar[T: Type]()(using QuoteContext): (Int, Expr[T]) = {
v += 1
(v, '{envVar[T](${Expr(v)})})
Expand Down
35 changes: 34 additions & 1 deletion tests/run-macros/quote-matching-open/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scala.quoted._
import scala.quoted.unsafe._

object Macro {

inline def openTest(inline x: Any): Any = ${ Macro.impl('x) }
Expand All @@ -13,3 +13,36 @@ object Macro {
}

}


object UnsafeExpr {
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
}
def open[T1, T2, R, X](f: Expr[(T1, T2) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2]) => Expr[t]) => X)(using qctx: QuoteContext)(using DummyImplicit): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal)).seal.asInstanceOf[Expr[t]])
}

def open[T1, T2, T3, R, X](f: Expr[(T1, T2, T3) => R])(content: (Expr[R], [t] => Expr[t] => (Expr[T1], Expr[T2], Expr[T3]) => Expr[t]) => X)(using qctx: QuoteContext)(using DummyImplicit, DummyImplicit): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v1: Expr[T1], v2: Expr[T2], v3: Expr[T3]) => bodyFn[t](e.unseal, params, List(v1.unseal, v2.unseal, v3.unseal)).seal.asInstanceOf[Expr[t]])
}
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
import qctx.reflect._
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
}

private def bodyFn[t](using qctx: QuoteContext)(e: qctx.reflect.Term, params: List[qctx.reflect.ValDef], args: List[qctx.reflect.Term]): qctx.reflect.Term = {
import qctx.reflect._
val map = params.map(_.symbol).zip(args).toMap
new TreeMap {
override def transformTerm(tree: Term)(using ctx: Context): Term =
super.transformTerm(tree) match
case tree: Ident => map.getOrElse(tree.symbol, tree)
case tree => tree
}.transformTerm(e)
}
}
7 changes: 5 additions & 2 deletions tests/run-macros/quoted-matching-docs/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import scala.quoted._

import scala.quoted.unsafe._

inline def sum(args: Int*): Int = ${ sumExpr('args) }

inline def sumShow(args: Int*): String = ${ sumExprShow('args) }
Expand All @@ -27,4 +25,9 @@ private def sumExpr(argsExpr: Expr[Seq[Int]])(using qctx: QuoteContext) : Expr[I
case _ =>
'{ $argsExpr.sum }
}
}

object UnsafeExpr {
def underlyingArgument[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
expr.unseal.underlyingArgument.seal.asInstanceOf[Expr[T]]
}