Strange behaviour of enums in for loops

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Fri Jul 17 04:53:46 PDT 2009


I've found what I believe to be a pretty serious bug in DMD (I've tried 
it with DMD 2.031):

    enum real ONE = 1.0;
    for (real x=0.0; x<=10.0; x+=ONE)  writeln(x);

The above loops only once, printing "0". And it gets weirder: Replace 
real by double, and the program starts a near-infinite loop, spitting 
out values on the order of 1e-312. Using float gives the same result as 
real.

Removing "enum" from the declaration of ONE makes the program behave as 
expected, as does writing "x+=1.0" in the for loop. The following works, 
which means it's probably not an issue with the += operator in general:

    enum real ONE = 1.0;
    real x = 1.0;
    x += ONE;
    assert (x == 2.0);

I can't seem to find an existing report on this issue in Bugzilla, but I 
find it hard to believe this hasn't been noticed before. That's why I 
thought I'd bring it up here before writing a bug report.

-Lars



More information about the Digitalmars-d mailing list