Skip to content

Fix quote report code example #11036

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 8, 2021
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 @@ -742,12 +742,12 @@ extension (inline sc: StringContext)
inline def showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }

private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using Quotes): Expr[String] =
import quotes.reflect.report
argsExpr match
case Varargs(argExprs) =>
val argShowedExprs = argExprs.map {
case '{ $arg: tp } =>
val showTp = Type.of[Show[tp]]
Expr.summon(using showTp) match
Expr.summon[Show[tp]] match
case Some(showExpr) =>
'{ $showExpr.show($arg) }
case None =>
Expand Down
18 changes: 8 additions & 10 deletions tests/run-macros/string-context-implicits/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ import scala.quoted._

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

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

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