Skip to content

Commit 2e53688

Browse files
Merge pull request #4788 from dotty-staging/fix-4784
Fix #4784: remove unsound constant folding
2 parents 2548ef0 + 46828e7 commit 2e53688

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import ast.Trees._
7575
// isBool(ift.tpe) && !elsep.const.booleanValue =>
7676
// cond.select(defn.Boolean_&&).appliedTo(elsep)
7777
// the other case ins't handled intentionally. See previous case for explanation
78-
78+
7979
case If(t @ Select(recv, _), thenp, elsep) if t.symbol eq defn.Boolean_! =>
8080
If(recv, elsep, thenp)
8181

@@ -98,11 +98,6 @@ import ast.Trees._
9898
case (lhs, Literal(_)) if !lhs.isInstanceOf[Literal] && simplifyPhase.CommutativePrimitiveOperations.contains(sym) =>
9999
rhs.select(sym).appliedTo(lhs)
100100

101-
case (l, _) if (sym == defn.Boolean_&&) && isConst(l.tpe) =>
102-
val const = asConst(l.tpe).value.booleanValue
103-
if (const) Block(lhs :: Nil, rhs)
104-
else l
105-
106101
case (l, x: Literal) if sym == defn.Boolean_== && isBool(l.tpe) && isBool(x.tpe) =>
107102
if (x.const.booleanValue) l
108103
else l.select(defn.Boolean_!).ensureApplied
@@ -119,11 +114,6 @@ import ast.Trees._
119114
if (!x.const.booleanValue) l
120115
else l.select(defn.Boolean_!).ensureApplied
121116

122-
case (l: Literal, _) if (sym == defn.Boolean_||) && isConst(l.tpe) =>
123-
val const = asConst(l.tpe).value.booleanValue
124-
if (l.const.booleanValue) l
125-
else Block(lhs :: Nil, rhs)
126-
127117
// case (Literal(Constant(1)), _) if sym == defn.Int_* => rhs
128118
// case (Literal(Constant(0)), _) if sym == defn.Int_+ => rhs
129119
// case (Literal(Constant(1L)), _) if sym == defn.Long_* => rhs
@@ -143,7 +133,7 @@ import ast.Trees._
143133

144134
case (l: Literal, r: Literal) =>
145135
(l.tpe.widenTermRefExpr, r.tpe.widenTermRefExpr) match {
146-
case (ConstantType(_), ConstantType(_)) =>
136+
case (ConstantType(_), ConstantType(_)) =>
147137
val s = ConstFold.apply(t)
148138
if ((s ne null) && s.tpe.isInstanceOf[ConstantType]) Literal(s.tpe.asInstanceOf[ConstantType].value)
149139
else t
@@ -169,8 +159,6 @@ import ast.Trees._
169159
case t => t
170160
}
171161

172-
173-
174162
def isSimilar(t1: Tree, t2: Tree)(implicit ctx: Context): Boolean = t1 match {
175163
case t1: Apply =>
176164
t2 match {
@@ -207,6 +195,4 @@ import ast.Trees._
207195
}
208196

209197
def isBool(tpe: Type)(implicit ctx: Context): Boolean = tpe.derivesFrom(defn.BooleanClass)
210-
def isConst(tpe: Type)(implicit ctx: Context): Boolean = tpe.isInstanceOf[ConstantType]
211-
def asConst(tpe: Type)(implicit ctx: Context): ConstantType = tpe.asInstanceOf[ConstantType]
212198
}

compiler/test/dotty/tools/dotc/SimplifyTests.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import dotty.tools.dotc.config.CompilerCommand
77
import dotty.tools.dotc.core.Contexts.FreshContext
88
import scala.tools.asm.tree.MethodNode
99

10-
class SimplifyPosTests extends SimplifyTests(optimise = true)
11-
class SimplifyNegTests extends SimplifyTests(optimise = false)
10+
class SimplifyPosTests extends SimplifyTests(pos = true)
11+
class SimplifyNegTests extends SimplifyTests(pos = false)
1212

13-
abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
13+
abstract class SimplifyTests(val pos: Boolean) extends DottyBytecodeTest {
1414
override def initCtx = {
1515
val ctx0 = super.initCtx
16-
ctx0.setSetting(ctx0.settings.optimise, optimise)
16+
ctx0.setSetting(ctx0.settings.optimise, pos)
1717
}
1818

19-
def check(source: String, expected: String, shared: String = ""): Unit = {
19+
def check(source: String, expected: String, shared: String = "", checkEqual: Boolean = pos): Unit = {
2020
import ASMConverters._
2121
val src =
2222
s"""
@@ -41,7 +41,7 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
4141
val A = instructions("A")
4242
val B = instructions("B")
4343
val diff = diffInstructions(A, B)
44-
if (optimise)
44+
if (checkEqual)
4545
assert(A == B, s"Bytecode doesn't match: (lhs = source, rhs = expected) \n$diff")
4646
else
4747
assert(A != B, s"Same Bytecodes without -optimise: you are testing the wrong thing!")
@@ -141,7 +141,7 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
141141
"""
142142
|val i = 3
143143
|val j = i + 4
144-
|if(j - i >= (i + 1) / 2)
144+
|if(j - i >= (i + 1) / 2)
145145
| print(i + 1)
146146
""",
147147
"""
@@ -188,6 +188,10 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
188188
|}
189189
""")
190190

191+
@Test def test4784 =
192+
check(
193+
"""{ println(1); false } || true""",
194+
"""true""", checkEqual = false)
191195

192196
// @Test def listPatmapExample =
193197
// check(

0 commit comments

Comments
 (0)