Skip to content

Commit 67c556f

Browse files
Constant fold string equality
1 parent c0097c8 commit 67c556f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ object ConstFold {
183183
case nme.MOD => Constant(x.doubleValue % y.doubleValue)
184184
case _ => null
185185
}
186+
private def foldStringOp(op: Name, x: Constant, y: Constant): Constant = op match {
187+
case nme.EQ => Constant(x.stringValue == y.stringValue)
188+
case nme.NE => Constant(x.stringValue != y.stringValue)
189+
case nme.ADD => Constant(x.stringValue + y.stringValue)
190+
case _ => null
191+
}
186192

187193
private def foldBinop(op: Name, x: Constant, y: Constant): Constant = {
188194
val optag =
@@ -196,7 +202,7 @@ object ConstFold {
196202
case LongTag => foldLongOp(op, x, y)
197203
case FloatTag => foldFloatOp(op, x, y)
198204
case DoubleTag => foldDoubleOp(op, x, y)
199-
case StringTag if op == nme.ADD => Constant(x.stringValue + y.stringValue)
205+
case StringTag => foldStringOp(op, x, y)
200206
case _ => null
201207
}
202208
catch {

tests/pos/dependent-eq.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
def assertT(t: { true }) = ()
3+
def assertF(f: { false }) = ()
4+
5+
dependent def r1 = 1 == 1 ; assertT(r1)
6+
dependent def r2 = "1" =="1" ; assertT(r2)
7+
dependent def r3 = 1 == 2 ; assertF(r3)
8+
dependent def r4 = "1" =="2" ; assertF(r4)
9+
dependent def r6 = 1 != 1 ; assertF(r6)
10+
dependent def r7 = "1" !="1" ; assertF(r7)
11+
dependent def r8 = 1 != 2 ; assertT(r8)
12+
dependent def r9 = "1" !="2" ; assertT(r9)
13+
}

0 commit comments

Comments
 (0)