Skip to content

Commit ba70cc0

Browse files
authored
Merge pull request #11111 from dotty-staging/fix-#10521
Constant fold String.==
2 parents 49ae535 + 730127b commit ba70cc0

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.core.NameKinds._
1111
import dotty.tools.dotc.core.Symbols._
1212
import dotty.tools.dotc.core.Types._
1313
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
14+
import dotty.tools.dotc.typer.ConstFold
1415

1516
/**
1617
* MiniPhase to transform s and raw string interpolators from using StringContext to string
@@ -162,6 +163,18 @@ class StringInterpolatorOpt extends MiniPhase {
162163
}
163164
}
164165
else
165-
tree
166+
tree.tpe match
167+
case _: ConstantType => tree
168+
case _ =>
169+
ConstFold.Apply(tree).tpe match
170+
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
171+
case _ => tree
166172
}
173+
174+
override def transformSelect(tree: Select)(using Context): Tree = {
175+
ConstFold.Select(tree).tpe match
176+
case ConstantType(x) => Literal(x).withSpan(tree.span).ensureConforms(tree.tpe)
177+
case _ => tree
178+
}
179+
167180
}

compiler/src/dotty/tools/dotc/typer/ConstFold.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ object ConstFold:
163163
case nme.MOD => Constant(x.doubleValue % y.doubleValue)
164164
case _ => null
165165
}
166+
private def foldStringOp(op: Name, x: Constant, y: Constant): Constant = op match {
167+
case nme.ADD => Constant(x.stringValue + y.stringValue)
168+
case nme.EQ => Constant(x.stringValue == y.stringValue)
169+
case _ => null
170+
}
166171

167172
private def foldBinop(op: Name, x: Constant, y: Constant): Constant =
168173
val optag =
@@ -176,7 +181,7 @@ object ConstFold:
176181
case LongTag => foldLongOp(op, x, y)
177182
case FloatTag => foldFloatOp(op, x, y)
178183
case DoubleTag => foldDoubleOp(op, x, y)
179-
case StringTag if op == nme.ADD => Constant(x.stringValue + y.stringValue)
184+
case StringTag => foldStringOp(op, x, y)
180185
case _ => null
181186
catch case ex: ArithmeticException => null // the code will crash at runtime,
182187
// but that is better than the

tests/pos/i10521.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
transparent inline def default(inline name: String): Any =
2+
inline if name == "Int" then 0
3+
else inline if name == "String" then ""
4+
else ???
5+
6+
7+
def test =
8+
default("Int")

tests/pos/stringConstantFold.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def test =
2+
("A" + "B": "AB")
3+
4+
("A" == "A": true)
5+
("A" == "B": false)

tests/pos/t5856b.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Test:
2+
def test = f("a" == s"a")
3+
inline def f(inline b: Boolean): Boolean = !b

tests/run-macros/tasty-extractors-1.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ ConstantType(IntConstant(2))
7373
Inlined(None, Nil, Try(Literal(IntConstant(3)), List(CaseDef(Ident("_"), None, Block(Nil, Literal(UnitConstant())))), Some(Literal(UnitConstant()))))
7474
OrType(ConstantType(IntConstant(3)), TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit"))
7575

76-
Inlined(None, Nil, Apply(Select(Literal(StringConstant("a")), "=="), List(Literal(StringConstant("b")))))
77-
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Boolean")
76+
Inlined(None, Nil, Literal(BooleanConstant(false)))
77+
ConstantType(BooleanConstant(false))
7878

7979
Inlined(None, Nil, Apply(Select(New(TypeIdent("Object")), "<init>"), Nil))
8080
TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "Object")

0 commit comments

Comments
 (0)