-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13652 from dotty-staging/add-quotes-change-owner
Allow nested Quotes with a different owners
- Loading branch information
Showing
10 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import scala.quoted.* | ||
|
||
inline def checked2[A](inline n: A): A = | ||
${ checkedImpl2[A]('{n}) } | ||
|
||
private def checkedImpl2[A](n: Expr[A])(using Quotes, Type[A]): Expr[A] = | ||
import quotes.reflect.* | ||
val tree: Term = n.asTerm | ||
val acc = new TreeMap: | ||
override def transformTerm(tree: Term)(owner: Symbol): Term = | ||
tree match | ||
case Apply(Select(x, "*"), List(y)) => | ||
given Quotes = owner.asQuotes | ||
'{ | ||
val xt = ${x.asExprOf[Long]} | ||
xt | ||
}.asTerm | ||
case _ => | ||
super.transformTerm(tree)(owner) | ||
acc.transformTerm(tree)(Symbol.spliceOwner).asExprOf[A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def test = { | ||
val u = 3L | ||
checked2(List(1L, 2L).map { k => | ||
u * 2L | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.quoted.* | ||
|
||
inline def checked2[A](inline n: A): A = | ||
${ checkedImpl2[A]('{n}) } | ||
|
||
private def checkedImpl2[A](n: Expr[A])(using Quotes, Type[A]): Expr[A] = | ||
import quotes.reflect.* | ||
val tree: Term = n.asTerm | ||
val acc = new TreeMap: | ||
override def transformTerm(tree: Term)(owner: Symbol): Term = | ||
tree match | ||
case Apply(Select(x, "*"), List(y)) => | ||
bindLong(x.asExprOf[Long])(using owner.asQuotes).asTerm | ||
case _ => | ||
super.transformTerm(tree)(owner) | ||
acc.transformTerm(tree)(Symbol.spliceOwner).asExprOf[A] | ||
|
||
def bindLong(expr: Expr[Long])(using Quotes): Expr[Long] = | ||
'{ | ||
val xt = $expr | ||
xt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def test = { | ||
val u = 3L | ||
checked2(List(1L, 2L).map { k => | ||
u * 2L | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import scala.quoted.* | ||
|
||
inline def optPrettyPrinter[T]: Option[T] => Option[T] = | ||
${ optPrettyPrinterImpl[T] } | ||
|
||
private def optPrettyPrinterImpl[T: Type](using Quotes): Expr[Option[T] => Option[T]] = { | ||
import quotes.reflect.* | ||
|
||
val tpe = TypeRepr.of[T] | ||
|
||
val fn = Lambda( | ||
Symbol.spliceOwner, | ||
MethodType(List("macroVal"))( | ||
_ => List(tpe), | ||
_ => tpe | ||
), | ||
{ | ||
case (m, List(arg: Term)) => | ||
given Quotes = m.asQuotes | ||
ValDef.let(m, "v", arg) { v => | ||
'{ | ||
val vv = ${ v.asExprOf[T] } | ||
println("v=" + vv.toString()) | ||
vv | ||
}.asTerm | ||
} | ||
|
||
case _ => | ||
report.errorAndAbort("Fails compile") | ||
} | ||
).asExprOf[T => T] | ||
|
||
'{ (_: Option[T]).map(${ fn }) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
def test = optPrettyPrinter(Some("foo")) |