www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22372] New: Loop index incorrectly optimised out for -release


          Issue ID: 22372
           Summary: Loop index incorrectly optimised out for -release -O
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: teodor.dutu gmail.com

When compiling the code below with the -O -release flags, the index used by the
for loop gets optimised out by the compiler. Thus, when throws2ndCall throws,
the value of i in the catch body is 0 instead of 1.

This behaviour seems to occur when the size of S is somewhat larger. When S
only contains 1 or 2 ints, the behaviour is the one expected. The error appears
from a size of 3 ints and above.

static size_t n;

void throws2ndCall(T)(ref T x)
    if (n == 1)
        throw new Exception("n == 1");

void foo(T)(scope T[] arr)
    size_t i;
        for (i = 0; i < arr.length; i++)
    catch (Exception o)
        printf("Exception: i = %lu; n = %lu\n", i, n);  // prints 0 and 1
        assert(i == n);  // this fails

void main()
    static struct S { int a1, a2, a3; }
    foo([S(), S()]);

DMD64 D Compiler v2.098.0: -release -O

Oct 09 2021