Skip to content

Commit 6d772ed

Browse files
committed
Factor out common StringContext extractor
1 parent 0b2c13d commit 6d772ed

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,9 @@ object StringContextMacro {
6161
import qctx.tasty._
6262
val sourceFile = strCtxExpr.unseal.pos.sourceFile
6363

64-
def notStatic =
65-
qctx.throwError("Expected statically known String Context", strCtxExpr)
66-
def splitParts(seq: Expr[Seq[String]]) = seq match {
67-
case Varargs(p1) =>
68-
p1 match
69-
case Consts(p2) => (p1.toList, p2.toList)
70-
case _ => notStatic
71-
case _ => notStatic
72-
}
7364
val (partsExpr, parts) = strCtxExpr match {
74-
case '{ StringContext($parts: _*) } => splitParts(parts)
75-
case '{ new StringContext($parts: _*) } => splitParts(parts)
76-
case _ => notStatic
65+
case Expr.StringContext(p1 @ Consts(p2)) => (p1.toList, p2.toList)
66+
case _ => qctx.throwError("Expected statically known String Context", strCtxExpr)
7767
}
7868

7969
val args = argsExpr match {

library/src/scala/compiletime/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ package object compiletime {
3838
transparent inline def (inline self: StringContext) code (inline args: Any*): String = ${ codeExpr('self, 'args) }
3939
private def codeExpr(using qctx: QuoteContext)(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] =
4040
(sc, args) match
41-
case ('{ StringContext(${Varargs(Consts(parts))}: _*) }, Varargs(args2)) =>
41+
case (Expr.StringContext(Consts(parts)), Varargs(args2)) =>
4242
Expr(StringContext(parts: _*).s(args2.map(_.show): _*))
4343
case _ =>
44-
qctx.throwError("compiletime.code must be used as a string interpolator code\"...\"")
44+
qctx.throwError("compiletime.code must be used as a string interpolator `code\"...\"`")
4545

4646
inline def constValueOpt[T]: Option[T] = ???
4747

library/src/scala/quoted/Expr.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,15 @@ object Expr {
219219
}
220220
}
221221

222+
object StringContext {
223+
/** Matches a `StringContext(part0, part1, ...)` and extracts the parts of a call to if the
224+
* parts are passed explicitly. Returns the equvalent to `Seq('{part0}, '{part1}, ...)`.
225+
*/
226+
def unapply(sc: Expr[StringContext])(using QuoteContext): Option[Seq[Expr[String]]] =
227+
sc match
228+
case '{ scala.StringContext(${Varargs(parts)}: _*) } => Some(parts)
229+
case '{ new scala.StringContext(${Varargs(parts)}: _*) } => Some(parts)
230+
case _ => None
231+
}
232+
222233
}

0 commit comments

Comments
 (0)