Warn when constant expression overflows
Brought to you by:
cabbey,
daveshields
I had the following fragment in my code:
System.currentTimeMillis() - date > 182 * 24 * 60 * 60 * 1000
This caused a hard to find bug, since the result of 182 * 24 * 60 * 60 * 1000 is too large to fit in an int and becomes a negative number.
It would be nice if jikes would warn about this. When a constant expression like this overflows it's almost certainly not what the programmer intended, so while not an error, it would be reasonable if the compiler would emit some kind of warning I think.
I catch this as part of an "overflow in constant folding" patch I'm working on for 1.20; here's what my current jikes says:
Issued 1 semantic warning compiling "Bug3704.java":
7. boolean a = System.currentTimeMillis() - date > 182 * 24 * 60 * 60 * 1000;
^-----------------------^
*** Semantic Caution: Overflow in int expression.
I wonder if it's worth emulating GCC and also having warnings like "expression always evaluates to false because of limited range of type"? I guess so, or we'll never spot things like this:
public void m(byte s) {
if (s > 768) {
System.err.println("Most peculiar!");
}
}
Fixed in CVS.
i'm putting off closing this, despite the fact that it's in 1.20, because:
1. we don't do anything for real types.
2. we don't do everything for integer types; we could warn about bad casts, or about comparisons that will always be true/false because of the limited range of a type, say.
i don't think this bug should be closed without either doing something about the points above, or raising new bugs.