www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18573] New: std.algorithm each does not function correctly

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

          Issue ID: 18573
           Summary: std.algorithm each does not function correctly for
                    assignment under x86
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: gamblemj gmail.com

Seems to be a problem with using "each" under 32bit which can be fixed by using
foreach or switching to x64. 

Let to a long discussion on the learn forum that went past my pay grade:
https://forum.dlang.org/post/hjqqqmlhkjrqaisbhehp forum.dlang.org

Below is a silly case, that replicates the error. (i.e. I know I could use
iota(0,9,2).array), but that does not demonstrate the potential bug and would
not fix my actual program.)

import std.range;
import std.algorithm;
import std.stdio;

unittest
{
        auto a = new double[9];
        a[0] = 0;
        iota(1,a.length).each!(i => a[i] = a[i-1] + 2);
        writeln(a);
}

//x86, wrong, error
//[-nan, 2, 4, 6, 8, 10, 12, 14, 16]
//First-chance exception: std.format.FormatException Unterminated format
specifier: "%" at C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(1175)

//x64, correct
//[0, 2, 4, 6, 8, 10, 12, 14, 16]

unittest
{
        auto a = new double[9];
        a[0] = 0;
        foreach(i; 1..a.length) a[i] = a[i - 1] + 2;
        writeln(a);
}

//x86, correct
//[0, 2, 4, 6, 8, 10, 12, 14, 16]

//x64, correct
//[0, 2, 4, 6, 8, 10, 12, 14, 16]

Originally found on windows 10, DMD v2.076.1, but now confirmed by others on
linux and current version.

--
Mar 07 2018