Skip to content

Fix #4784: remove unsound constant folding #4788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import ast.Trees._
// isBool(ift.tpe) && !elsep.const.booleanValue =>
// cond.select(defn.Boolean_&&).appliedTo(elsep)
// the other case ins't handled intentionally. See previous case for explanation

case If(t @ Select(recv, _), thenp, elsep) if t.symbol eq defn.Boolean_! =>
If(recv, elsep, thenp)

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

case (l, _) if (sym == defn.Boolean_&&) && isConst(l.tpe) =>
val const = asConst(l.tpe).value.booleanValue
if (const) Block(lhs :: Nil, rhs)
else l

case (l, x: Literal) if sym == defn.Boolean_== && isBool(l.tpe) && isBool(x.tpe) =>
if (x.const.booleanValue) l
else l.select(defn.Boolean_!).ensureApplied
Expand All @@ -119,11 +114,6 @@ import ast.Trees._
if (!x.const.booleanValue) l
else l.select(defn.Boolean_!).ensureApplied

case (l: Literal, _) if (sym == defn.Boolean_||) && isConst(l.tpe) =>
val const = asConst(l.tpe).value.booleanValue
if (l.const.booleanValue) l
else Block(lhs :: Nil, rhs)

// case (Literal(Constant(1)), _) if sym == defn.Int_* => rhs
// case (Literal(Constant(0)), _) if sym == defn.Int_+ => rhs
// case (Literal(Constant(1L)), _) if sym == defn.Long_* => rhs
Expand All @@ -143,7 +133,7 @@ import ast.Trees._

case (l: Literal, r: Literal) =>
(l.tpe.widenTermRefExpr, r.tpe.widenTermRefExpr) match {
case (ConstantType(_), ConstantType(_)) =>
case (ConstantType(_), ConstantType(_)) =>
val s = ConstFold.apply(t)
if ((s ne null) && s.tpe.isInstanceOf[ConstantType]) Literal(s.tpe.asInstanceOf[ConstantType].value)
else t
Expand All @@ -169,8 +159,6 @@ import ast.Trees._
case t => t
}



def isSimilar(t1: Tree, t2: Tree)(implicit ctx: Context): Boolean = t1 match {
case t1: Apply =>
t2 match {
Expand Down Expand Up @@ -207,6 +195,4 @@ import ast.Trees._
}

def isBool(tpe: Type)(implicit ctx: Context): Boolean = tpe.derivesFrom(defn.BooleanClass)
def isConst(tpe: Type)(implicit ctx: Context): Boolean = tpe.isInstanceOf[ConstantType]
def asConst(tpe: Type)(implicit ctx: Context): ConstantType = tpe.asInstanceOf[ConstantType]
}
18 changes: 11 additions & 7 deletions compiler/test/dotty/tools/dotc/SimplifyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import dotty.tools.dotc.config.CompilerCommand
import dotty.tools.dotc.core.Contexts.FreshContext
import scala.tools.asm.tree.MethodNode

class SimplifyPosTests extends SimplifyTests(optimise = true)
class SimplifyNegTests extends SimplifyTests(optimise = false)
class SimplifyPosTests extends SimplifyTests(pos = true)
class SimplifyNegTests extends SimplifyTests(pos = false)

abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
abstract class SimplifyTests(val pos: Boolean) extends DottyBytecodeTest {
override def initCtx = {
val ctx0 = super.initCtx
ctx0.setSetting(ctx0.settings.optimise, optimise)
ctx0.setSetting(ctx0.settings.optimise, pos)
}

def check(source: String, expected: String, shared: String = ""): Unit = {
def check(source: String, expected: String, shared: String = "", checkEqual: Boolean = pos): Unit = {
import ASMConverters._
val src =
s"""
Expand All @@ -41,7 +41,7 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
val A = instructions("A")
val B = instructions("B")
val diff = diffInstructions(A, B)
if (optimise)
if (checkEqual)
assert(A == B, s"Bytecode doesn't match: (lhs = source, rhs = expected) \n$diff")
else
assert(A != B, s"Same Bytecodes without -optimise: you are testing the wrong thing!")
Expand Down Expand Up @@ -141,7 +141,7 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
"""
|val i = 3
|val j = i + 4
|if(j - i >= (i + 1) / 2)
|if(j - i >= (i + 1) / 2)
| print(i + 1)
""",
"""
Expand Down Expand Up @@ -188,6 +188,10 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
|}
""")

@Test def test4784 =
check(
"""{ println(1); false } || true""",
"""true""", checkEqual = false)

// @Test def listPatmapExample =
// check(
Expand Down