Skip to content

Commit df323e7

Browse files
Merge pull request #8295 from dotty-staging/update-implementation-of-StringContextMacro
Update implementation of StringContextMacro
2 parents f9cddcf + 1342f49 commit df323e7

File tree

1 file changed

+16
-47
lines changed

1 file changed

+16
-47
lines changed

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

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import reflect._
99
object StringContextMacro {
1010

1111
/** Implementation of scala.StringContext.f used in Dotty */
12-
inline def f(sc: => StringContext)(args: Any*): String = ${ interpolate('sc, 'args) }
12+
inline def f(inline sc: StringContext)(inline args: Any*): String = ${ interpolate('sc, 'args) }
1313

1414
/** This trait defines a tool to report errors/warnings that do not depend on Position. */
1515
trait Reporter {
@@ -53,45 +53,6 @@ object StringContextMacro {
5353
def restoreReported() : Unit
5454
}
5555

56-
/** Retrieves the parts from a StringContext, given inside an Expr, and returns them as a list of Expr of String
57-
*
58-
* @param strCtxExpr the Expr containing the StringContext
59-
* @return a list of Expr containing Strings, each corresponding to one parts of the given StringContext
60-
* quotes an error if the given Expr does not correspond to a StringContext
61-
*/
62-
def getPartsExprs(strCtxExpr: Expr[scala.StringContext])(using qctx: QuoteContext): Option[(List[Expr[String]], List[String])] = {
63-
def notStatic = {
64-
qctx.error("Expected statically known String Context", strCtxExpr)
65-
None
66-
}
67-
def splitParts(seq: Expr[Seq[String]]) = (seq, seq) match {
68-
case (ExprSeq(p1), ConstSeq(p2)) => Some((p1.toList, p2.toList))
69-
case (_, _) => notStatic
70-
}
71-
strCtxExpr match {
72-
case '{ StringContext($parts: _*) } => splitParts(parts)
73-
case '{ new StringContext($parts: _*) } => splitParts(parts)
74-
case _ => notStatic
75-
}
76-
}
77-
78-
/** Retrieves a list of Expr, each containing an argument, from an Expr of list of arguments
79-
*
80-
* @param argsExpr the Expr containing the list of arguments
81-
* @return a list of Expr containing arguments
82-
* quotes an error if the given Expr does not contain a list of arguments
83-
*/
84-
def getArgsExprs(argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Option[List[Expr[Any]]] = {
85-
import qctx.tasty.{_, given _}
86-
argsExpr.unseal.underlyingArgument match {
87-
case Typed(Repeated(args, _), _) =>
88-
Some(args.map(_.seal))
89-
case tree =>
90-
qctx.error("Expected statically known argument list", argsExpr)
91-
None
92-
}
93-
}
94-
9556
/** Interpolates the arguments to the formatting String given inside a StringContext
9657
*
9758
* @param strCtxExpr the Expr that holds the StringContext which contains all the chunks of the formatting string
@@ -102,13 +63,21 @@ object StringContextMacro {
10263
import qctx.tasty.{_, given _}
10364
val sourceFile = strCtxExpr.unseal.pos.sourceFile
10465

105-
val (partsExpr, parts) = getPartsExprs(strCtxExpr) match {
106-
case Some(x) => x
107-
case None => return '{""}
66+
def notStatic =
67+
qctx.throwError("Expected statically known String Context", strCtxExpr)
68+
def splitParts(seq: Expr[Seq[String]]) = (seq, seq) match {
69+
case (ExprSeq(p1), ConstSeq(p2)) => (p1.toList, p2.toList)
70+
case (_, _) => notStatic
71+
}
72+
val (partsExpr, parts) = strCtxExpr match {
73+
case '{ StringContext($parts: _*) } => splitParts(parts)
74+
case '{ new StringContext($parts: _*) } => splitParts(parts)
75+
case _ => notStatic
10876
}
109-
val args = getArgsExprs(argsExpr) match {
110-
case Some(args) => args
111-
case None => return '{""}
77+
78+
val args = argsExpr match {
79+
case ExprSeq(args) => args
80+
case _ => qctx.throwError("Expected statically known argument list", argsExpr)
11281
}
11382

11483
val reporter = new Reporter{
@@ -164,7 +133,7 @@ object StringContextMacro {
164133
* @param reporter the reporter to return any error/warning when a problem is encountered
165134
* @return the Expr containing the formatted and interpolated String or an error/warning report if the parameters are not correct
166135
*/
167-
def interpolate(parts0 : List[String], args : List[Expr[Any]], argsExpr: Expr[Seq[Any]], reporter : Reporter)(using qctx: QuoteContext) : Expr[String] = {
136+
def interpolate(parts0 : List[String], args : Seq[Expr[Any]], argsExpr: Expr[Seq[Any]], reporter : Reporter)(using qctx: QuoteContext) : Expr[String] = {
168137
import qctx.tasty.{_, given _}
169138

170139
/** Checks if the number of arguments are the same as the number of formatting strings

0 commit comments

Comments
 (0)