Skip to content

Commit fe6ab99

Browse files
authored
Merge pull request #4779 from lloydmeta/feature/rooted-stringcontext
Fix #4732: Root the desugared call to StringContext
2 parents acd0c6e + 086c891 commit fe6ab99

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,8 @@ object desugar {
11091109
case Block(Nil, expr) => expr // important for interpolated string as patterns, see i1773.scala
11101110
case t => t
11111111
}
1112-
1113-
Apply(Select(Apply(Ident(nme.StringContext), strs), id), elems)
1112+
// This is a deliberate departure from scalac, where StringContext is not rooted (See #4732)
1113+
Apply(Select(Apply(scalaDot(nme.StringContext), strs), id), elems)
11141114
case InfixOp(l, op, r) =>
11151115
if (ctx.mode is Mode.Type)
11161116
AppliedTypeTree(op, l :: r :: Nil) // op[l, r]

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ class StringInterpolatorOpt extends MiniPhase {
3434

3535
private object StringContextApply {
3636
def unapply(tree: Select)(implicit ctx: Context): Boolean = {
37-
tree.symbol.eq(defn.StringContextModule_apply) && {
38-
val qualifier = tree.qualifier
39-
qualifier.isInstanceOf[Ident] && qualifier.symbol.eq(defn.StringContextModule)
40-
}
37+
tree.symbol.eq(defn.StringContextModule_apply) &&
38+
tree.qualifier.symbol.eq(defn.StringContextModule)
4139
}
4240
}
4341

tests/run/rooted_stringcontext.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object StringContext {
2+
val i = 42
3+
val s = s"Answer: $i"
4+
}
5+
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
assert(StringContext.s == "Answer: 42")
9+
}
10+
}

0 commit comments

Comments
 (0)