<html>
    <head>
      <base href="http://bugzilla.gdcproject.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Optimized GDC with no-bounds-checking skips operations in foreach if given statically"
   href="http://bugzilla.gdcproject.org/show_bug.cgi?id=188">188</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimized GDC with no-bounds-checking skips operations in foreach if given statically
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>GDC
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>4.9.x
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>x86_64
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>critical
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>Normal
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>gdc
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>ibuclaw@gdcproject.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>liran@weka.io
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Reproducible on commit b022dd4cac195d85e9c3a6a37f2501a07ade455a from April 7th
on the 4.9 branch.

Please consider the following code:
---------------------
module test;

import std.stdio;

enum R = 4;
enum S = 2;


int main() {
    uint[R+S] a = 6;
    uint[R+S] b = 9;
    writeln("a, b : ", a, b);
    ushort c = 3;
    foreach(i; 0..R) {
        b[i .. (i+1)] += a[i .. (i+1)];
        a[i .. (i+1)] = b[i .. (i+1)] ^ c;
    }
    writeln("After operation: a,b: ", a, b);
    return 0;
}
-------------------------

And the following output, first from dmd, then gdc:

bash-4.3# dmd test.d -ofdtest && ./dtest
a, b : [6, 6, 6, 6, 6, 6][9, 9, 9, 9, 9, 9]
After operation: a,b: [12, 12, 12, 12, 6, 6][15, 15, 15, 15, 9, 9]

bash-4.3# /opt/gdc/bin/gdc   -fno-bounds-check  -O  test.d -ogtest && ./gtest
a, b : [6, 6, 6, 6, 6, 6][9, 9, 9, 9, 9, 9]
After operation: a,b: [12, 6, 6, 6, 6, 6][15, 9, 9, 9, 9, 9] # WRONG!!!

When GDC compiles the code with static range in the foreach, only the first
index is being assigned and only the first calculation is being performed.

In this example there is also S set, but it reproduces perfectly without the S,
or when it's 0. I just added it to show that the code cannot be fixed with
foreach(i; 0 .. b.length).

I put two examples of operations (addition and xor), but it actually does not
matter. Any operation that I tried reproduces the problem (as long as there is
no function call in the foreach() ).</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are watching all bug changes.</li>
      </ul>
    </body>
</html>