summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker
diff options
context:
space:
mode:
authorOlli Helenius <liff@iki.fi>2016-05-02 16:51:37 +0300
committerLukas Rytz <lukas.rytz@typesafe.com>2016-05-02 15:51:37 +0200
commit1b1bc8148492af85f9d5507676ef7b677cf86bcb (patch)
treec5fd6325bdad889d81be698737c4d841016e3592 /src/compiler/scala/tools/nsc/typechecker
parent86fe7de15988ea511fdc5e4f44039154cd236149 (diff)
downloadscala-1b1bc8148492af85f9d5507676ef7b677cf86bcb.tar.gz
scala-1b1bc8148492af85f9d5507676ef7b677cf86bcb.tar.bz2
scala-1b1bc8148492af85f9d5507676ef7b677cf86bcb.zip
Emit a warning when a constant expression evaluates to an ArithmeticException (#5123)
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker')
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala
index 828a79ac4f..2cd4785fbf 100644
--- a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala
@@ -40,9 +40,10 @@ abstract class ConstantFolder {
if ((x ne null) && x.tag != UnitTag) tree setType ConstantType(x)
else tree
} catch {
- case _: ArithmeticException => tree // the code will crash at runtime,
- // but that is better than the
- // compiler itself crashing
+ case e: ArithmeticException =>
+ if (settings.warnConstant)
+ warning(tree.pos, s"Evaluation of a constant expression results in an arithmetic error: ${e.getMessage}")
+ tree
}
private def foldUnop(op: Name, x: Constant): Constant = (op, x.tag) match {
@@ -158,7 +159,7 @@ abstract class ConstantFolder {
else if (x.isNumeric && y.isNumeric) math.max(x.tag, y.tag)
else NoTag
- try optag match {
+ optag match {
case BooleanTag => foldBooleanOp(op, x, y)
case ByteTag | ShortTag | CharTag | IntTag => foldSubrangeOp(op, x, y)
case LongTag => foldLongOp(op, x, y)
@@ -167,8 +168,5 @@ abstract class ConstantFolder {
case StringTag if op == nme.ADD => Constant(x.stringValue + y.stringValue)
case _ => null
}
- catch {
- case _: ArithmeticException => null
- }
}
}