Broken enum arithmetic in C
Peter Osterlund
peter.osterlund@mailbox.swipnet.se
Sun Apr 30 05:48:00 GMT 2000
Consider this C code:
enum { v = 1 };
signed char f()
{
return -v;
}
On i686-pc-linux-gnu, compiling this code with recent snapshots
generats a bogus warning:
bug.c: In function `f':
bug.c:4: warning: overflow in implicit constant conversion
For this source code, correct assembly code is generated in spite of
the warning, but I think the bug in message
http://gcc.gnu.org/ml/gcc-bugs/2000-02/msg00523.html is also caused by
this problem, and in that case incorrect assembly code is generated
for valid source code. That bug report was probably a bit misleading,
because the root of the problem doesn't seem to be related to
optimization.
If I explicitly cast the enum to int before negating the value,
ie. writing "return -(int)v", the warning goes away. The warning also
goes away if I compile the code as C++.
(This problem is also the reason why the chess program crafty is
miscompiled with recent gcc snapshots.)
--
Peter ÃÂsterlund Email: peter.osterlund@mailbox.swipnet.se
Sköndalsvägen 35 f90-pos@nada.kth.se
S-128 66 Sköndal Home page: http://home1.swipnet.se/~w-15919
Sweden Phone: +46 8 942647
More information about the Gcc-bugs
mailing list