Skip to content

Commit 1ac8205

Browse files
committed
Fix #4732: Roots the desugared call to StringContext.
* Make desugared `s""` call `_root_.scala.StringContext` instead of just `StringContext` * Add a unit test
1 parent 64bd0fe commit 1ac8205

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,13 @@ 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+
val rootedStringContext = {
1114+
val rootPkg = Ident(nme.ROOTPKG)
1115+
val scalaPkg = Select(rootPkg, nme.scala_)
1116+
Select(scalaPkg, nme.StringContext)
1117+
}
1118+
Apply(Select(Apply(rootedStringContext, strs), id), elems)
11141119
case InfixOp(l, op, r) =>
11151120
if (ctx.mode is Mode.Type)
11161121
AppliedTypeTree(op, l :: r :: Nil) // op[l, r]

tests/run/rooted_stringcontext.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Answer: 42

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+
println(StringContext.s)
9+
}
10+
}

0 commit comments

Comments
 (0)