Skip to content

Commit ac7ab0a

Browse files
committed
New tests and fix
1 parent 7626e05 commit ac7ab0a

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,29 @@ class StringInterpolatorOpt extends MiniPhase {
3232
}
3333
}
3434

35+
private object StringContextIdent {
36+
def unapply(tree: Ident)(implicit ctx: Context): Option[Ident] = {
37+
if (tree.symbol.eq(defn.StringContextModule)) Some(tree) else None
38+
}
39+
}
40+
41+
private object StringContextApply {
42+
def unapply(tree: Select)(implicit ctx: Context): Option[Select] = {
43+
tree match {
44+
case Select(StringContextIdent(_), _)
45+
if tree.symbol.eq(defn.StringContextModule_apply) => Some(tree)
46+
case _ => None
47+
}
48+
}
49+
}
50+
3551
/** Matches an s or raw string interpolator */
3652
private object SOrRawInterpolator {
3753
def unapply(tree: Tree)(implicit ctx: Context): Option[(List[Literal], List[Tree])] = {
3854
if (tree.symbol.eq(defn.StringContextRaw) || tree.symbol.eq(defn.StringContextS)) {
3955
tree match {
40-
case Apply(Select(Apply(strContextApply, List(Literals(strs))), _),
41-
List(SeqLiteral(elems, _)))
42-
if strContextApply.symbol.eq(defn.StringContextModule_apply) &&
43-
elems.length == strs.length - 1 =>
56+
case Apply(Select(Apply(StringContextApply(_), List(Literals(strs))), _),
57+
List(SeqLiteral(elems, _))) if elems.length == strs.length - 1 =>
4458
Some(strs, elems)
4559
case _ => None
4660
}

tests/run/interpolation-opt.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ a1two3.0b
77

88
Hello World
99
Side effect!
10-
Hello World
10+
Foo Bar
11+
Side effect n2!
12+
Titi Toto

tests/run/interpolation-opt.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ object Test extends App {
2222
println(StringContext(foo, bar).s(" "))
2323

2424
def myStringContext= { println("Side effect!"); StringContext }
25-
println(myStringContext(foo, bar).s(" ")) // this shouldn't be optimised away
25+
println(myStringContext("Foo", "Bar").s(" ")) // this shouldn't be optimised away
26+
27+
// this shouldn't be optimised away
28+
println({ println("Side effect n2!"); StringContext }.apply("Titi", "Toto").s(" "))
2629
}

0 commit comments

Comments
 (0)