Skip to content

Commit b2cc163

Browse files
committed
Simplify XML interpolator implementation
- Remove unnecessary inline implicit conversion to `SCOps` - Use Symbol::fullName instead of tree extractors
1 parent 2dbec65 commit b2cc163

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

tests/run/xml-interpolation/Test_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XmlQuote._
33
object Test {
44
def main(args: Array[String]): Unit = {
55
// TODO: enable once #5119 is fixed
6-
// assert(xml"Hello Allan!" == Xml("Hello Allan!", Nil)
6+
// assert(xml"Hello Allan!" == Xml("Hello Allan!", Nil))
77

88
val name = new Object{}
99
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))

tests/run/xml-interpolation/XmlQuote_1.scala

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import scala.language.implicitConversions
55

66
case class Xml(parts: String, args: List[Any])
77

8-
// Ideally should be an implicit class but the implicit conversion
9-
// has to be a inline method
10-
class XmlQuote(val ctx: StringContext) {
11-
inline def xml(args: => Any*): Xml = ~XmlQuote.impl('(this), '(args))
12-
}
13-
148
object XmlQuote {
15-
implicit inline def XmlQuote(ctx: StringContext): XmlQuote = new XmlQuote(ctx)
169

17-
def impl(receiver: Expr[XmlQuote], args: Expr[Seq[Any]])
10+
implicit class SCOps(ctx: StringContext) {
11+
inline def xml(args: => Any*): Xml = ~XmlQuote.impl('(this), '(args))
12+
}
13+
14+
def impl(receiver: Expr[SCOps], args: Expr[Seq[Any]])
1815
(implicit tasty: Tasty): Expr[Xml] = {
1916
import tasty._
2017
import Term._
@@ -41,17 +38,21 @@ object XmlQuote {
4138
case _ => false
4239
}
4340

44-
// _root_.scala.StringContext.apply([p0, ...]: String*)
41+
def isSCOpsConversion(tree: Term) =
42+
tree.symbol.fullName == "XmlQuote$.SCOps"
43+
44+
def isStringContextApply(tree: Term) =
45+
tree.symbol.fullName == "scala.StringContext$.apply"
46+
47+
// XmlQuote.SCOps(StringContext.apply([p0, ...]: String*)
4548
val parts = receiver.toTasty.underlyingArgument match {
46-
case Apply(
47-
Select(New(_), _, _),
48-
List(
49-
Apply(
50-
Select(Select(Select(Ident("_root_"), "scala", _), "StringContext", _), "apply", _),
51-
List(Typed(Repeated(values), _))))) if values.forall(isStringConstant) =>
49+
case Apply(conv, List(Apply(fun, List(Typed(Repeated(values), _)))))
50+
if isSCOpsConversion(conv) &&
51+
isStringContextApply(fun) &&
52+
values.forall(isStringConstant) =>
5253
values.collect { case Literal(Constant.String(value)) => value }
5354
case tree =>
54-
abort(s"String literal expected, but $tree found")
55+
abort(s"String literal expected, but ${tree.show} found")
5556
}
5657

5758
// [a0, ...]: Any*

0 commit comments

Comments
 (0)