[Issue 22372] New: Loop index incorrectly optimised out for -release -O

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Oct 9 10:29:59 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22372

          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 at puremagic.com
          Reporter: teodor.dutu at 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");
    ++n;
}

void foo(T)(scope T[] arr)
{
    size_t i;
    try
    {
        for (i = 0; i < arr.length; i++)
            throws2ndCall(arr[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

--


More information about the Digitalmars-d-bugs mailing list