Skip to content

Update TASTy Reflect to summon and given #7687

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
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
4 changes: 2 additions & 2 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,13 @@ sum
### Find implicits within a macro

Similarly to the `summonFrom` construct, it is possible to make implicit search available
in a quote context. For this we simply provide `scala.quoted.matching.searchImplicitExpr:
in a quote context. For this we simply provide `scala.quoted.matching.summonExpr:

```scala
inline def setFor[T]: Set[T] = ${ setForExpr[T] }

def setForExpr[T: Type](given QuoteContext): Expr[Set[T]] = {
searchImplicitExpr[Ordering[T]] match {
summonExpr[Ordering[T]] match {
case Some(ord) => '{ new TreeSet[T]()($ord) }
case _ => '{ new HashSet[T] }
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/matching/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package object matching {
* @param tpe quoted type of the implicit parameter
* @param qctx current context
*/
def searchImplicitExpr[T](given tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = {
def summonExpr[T](given tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = {
import qctx.tasty.{_, given}
searchImplicit(tpe.unseal.tpe) match {
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ class Reflection(private[scala] val internal: CompilerInterface)
with TypeOrBoundsOps { self =>

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

}
6 changes: 3 additions & 3 deletions tests/run-macros/quote-implicitMatch/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.quoted.matching._
inline def f1[T]() = ${ f1Impl[T] }

def f1Impl[T: Type](given QuoteContext) = {
searchImplicitExpr[Ordering[T]] match {
summonExpr[Ordering[T]] match {
case Some(ord) => '{ new TreeSet[T]()($ord) }
case _ => '{ new HashSet[T] }
}
Expand All @@ -18,7 +18,7 @@ class B
inline def g = ${ gImpl }

def gImpl(given QuoteContext) = {
if (searchImplicitExpr[A].isDefined) '{ println("A") }
else if (searchImplicitExpr[B].isDefined) '{ println("B") }
if (summonExpr[A].isDefined) '{ println("A") }
else if (summonExpr[B].isDefined) '{ println("B") }
else throw new MatchError("")
}
4 changes: 4 additions & 0 deletions tests/run-macros/string-context-implicits.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Int(1) Str(abc)
Int(1) Str(abc)
xyz
Int(1) Str(xyz)
31 changes: 31 additions & 0 deletions tests/run-macros/string-context-implicits/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import scala.quoted._
import scala.quoted.matching._

inline def (sc: StringContext) showMe(args: =>Any*): String = ${ showMeExpr('sc, 'args) }

private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Expr[String] = {
argsExpr match {
case ExprSeq(argExprs) =>
val argShowedExprs = argExprs.map {
case '{ $arg: $tp } =>
val showTp = '[Show[$tp]]
summonExpr(given showTp, summon[QuoteContext]) match {
case Some(showExpr) => '{ $showExpr.show($arg) }
case None => qctx.error(s"could not find implicit for ${showTp.show}", arg); '{???}
}
}
val newArgsExpr = Expr.ofSeq(argShowedExprs)
'{ $sc.s($newArgsExpr: _*) }
case _ =>
// `new StringContext(...).showMeExpr(args: _*)` not an explicit `showMeExpr"..."`
qctx.error(s"Args must be explicit", argsExpr)
'{???}
}
}

trait Show[-T] {
def show(x: T): String
}

given Show[Int] = x => s"Int($x)"
given Show[String] = x => s"Str($x)"
9 changes: 9 additions & 0 deletions tests/run-macros/string-context-implicits/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test {

def main(args: Array[String]): Unit = {
println(showMe"${1: Int} ${"abc": String}")
println(showMe"${1} ${"abc"}")
println(showMe"${1} ${println("xyz"); "xyz"}")
}

}
4 changes: 4 additions & 0 deletions tests/run/string-context-implicits-with-conversion.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Int(1) Str(abc)
Int(1) Str(abc)
xyz
Int(1) Str(xyz)
24 changes: 24 additions & 0 deletions tests/run/string-context-implicits-with-conversion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

object Lib {
def (sc: StringContext) showMe(args: Showed*): String = sc.s(args: _*)

opaque type Showed = String

given [T](given show: Show[T]): Conversion[T, Showed] = x => show(x)

trait Show[T] {
def apply(x: T): String
}

given Show[Int] = x => s"Int($x)"
given Show[String] = x => s"Str($x)"
}
object Test {
import Lib._
def main(args: Array[String]): Unit = {
println(showMe"${1: Int} ${"abc": String}")
println(showMe"${1} ${"abc"}")
println(showMe"${1} ${println("xyz"); "xyz"}")
}

}