diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2014-05-18 10:24:43 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2014-05-18 10:42:20 +0200 |
commit | ee611cd76c29fedd416162e482c7ab3f15b831ca (patch) | |
tree | bc6f3d23eaffa96f1e56140f4c78be5c088a26c1 /test/files | |
parent | 7872ea495a9e39f3c37e1662837cacb769a9f359 (diff) | |
download | scala-ee611cd76c29fedd416162e482c7ab3f15b831ca.tar.gz scala-ee611cd76c29fedd416162e482c7ab3f15b831ca.tar.bz2 scala-ee611cd76c29fedd416162e482c7ab3f15b831ca.zip |
SI-8601 Don't treat int/long division, or arraylength, as dead-code
`{i, l}div` and `{i, l}rem` throw an `ArithmeticException` if the
divisor is 0.
`arraylength` throws a `NullPointerException` on a null reference.
JVM Spec:
> The only integer operations that can throw an exception are the
> integer divide instructions (idiv and ldiv) and the integer
> remainder instructions (irem and lrem), which throw an
> ArithmeticException if the divisor is zero.
> The Java virtual machine's floating-point operators do not throw
> runtime exceptions
> If the arrayref is null, the arraylength instruction throws a
> NullPointerException.
I checked the other primitives in `ICode` to see if anything else
should be considered as live code.
Pure:
// jvm : {i, l, f, d}neg
case class Negation(kind: TypeKind) extends Primitive
// jvm : if{eq, ne, lt, ge, le, gt}, if{null, nonnull}
// if_icmp{eq, ne, lt, ge, le, gt}, if_acmp{eq,ne}
case class Test(op: TestOp, kind: TypeKind, zero: Boolean) extends Primitive
// jvm : lcmp, {f, d}cmp{l, g}
case class Comparison(op: ComparisonOp, kind: TypeKind) extends Primitive
Impure: {i, l}{div, rem}, otherwise pure
// jvm : {i, l, f, d}{add, sub, mul, div, rem}
case class Arithmetic(op: ArithmeticOp, kind: TypeKind) extends Primitive
Pure (overflow is silent, NaN.toInt is defined):
// jvm : {i, l}{and, or, xor}
case class Logical(op: LogicalOp, kind: TypeKind) extends Primitive
// jvm : {i, l}{shl, ushl, shr}
case class Shift(op: ShiftOp, kind: TypeKind) extends Primitive
// jvm : i2{l, f, d}, l2{i, f, d}, f2{i, l, d}, d2{i, l, f}, i2{b, c, s}
case class Conversion(src: TypeKind, dst: TypeKind) extends Primitive
Impure! May NPE!
// jvm : arraylength
case class ArrayLength(kind: TypeKind) extends Primitive
Pure (we know that StringBuilder.{<init>, append, toString} are pure
and `append` is null safe.)
// jvm : It should call the appropiate 'append' method on StringBuffer
case class StringConcat(el: TypeKind) extends Primitive
// jvm: it should create a new StringBuffer
case object StartConcat extends Primitive
// jvm: convert StringBuffer to a String
case object EndConcat extends Primitive
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/run/t8601.flags | 1 | ||||
-rw-r--r-- | test/files/run/t8601.scala | 15 | ||||
-rw-r--r-- | test/files/run/t8601b.flags | 1 | ||||
-rw-r--r-- | test/files/run/t8601b.scala | 9 |
4 files changed, 26 insertions, 0 deletions
diff --git a/test/files/run/t8601.flags b/test/files/run/t8601.flags new file mode 100644 index 0000000000..1182725e86 --- /dev/null +++ b/test/files/run/t8601.flags @@ -0,0 +1 @@ +-optimize
\ No newline at end of file diff --git a/test/files/run/t8601.scala b/test/files/run/t8601.scala new file mode 100644 index 0000000000..e1afc23cc4 --- /dev/null +++ b/test/files/run/t8601.scala @@ -0,0 +1,15 @@ +object Test { + def idiv(x: Int): Unit = x / 0 + def ldiv(x: Long): Unit = x / 0 + def irem(x: Int): Unit = x % 0 + def lrem(x: Long): Unit = x % 0 + + def check(x: => Any) = try { x; sys.error("failed to throw divide by zero!") } catch { case _: ArithmeticException => } + + def main(args: Array[String]) { + check(idiv(1)) + check(ldiv(1L)) + check(irem(1)) + check(lrem(1L)) + } +} diff --git a/test/files/run/t8601b.flags b/test/files/run/t8601b.flags new file mode 100644 index 0000000000..1182725e86 --- /dev/null +++ b/test/files/run/t8601b.flags @@ -0,0 +1 @@ +-optimize
\ No newline at end of file diff --git a/test/files/run/t8601b.scala b/test/files/run/t8601b.scala new file mode 100644 index 0000000000..42b562af96 --- /dev/null +++ b/test/files/run/t8601b.scala @@ -0,0 +1,9 @@ +object Test { + def len(x: Array[String]): Unit = x.length + + def check(x: => Any) = try { x; sys.error("failed to throw NPE!") } catch { case _: NullPointerException => } + + def main(args: Array[String]) { + check(len(null)) + } +} |