issue with each specifically for x86

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 7 19:54:36 UTC 2018


On 3/7/18 1:57 PM, Matt Gamble wrote:
> This is a record for me with two 32bit vs 64bit issues in one day. Seems 
> to be a problem with using "each" under 32bit which can be fixed by 
> using foreach or switching to x64. Am I doing something wrong or is this 
> the second bug I've found today?
> 
> Below is a silly case, that replicates an 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]
> 
> This is windows 10, DMD v2.076.1
> 

It has something to do with the fact that you are returning the value:

iota(1, a.length).each!((i) {a[i] = a[i - 1] + 2;}); // ok
iota(1, a.length).each!((i) {return a[i] = a[i - 1] + 2;}); // shows error


Which is odd to say the least, I don't think each is supposed to do 
anything with the return value.

I don't get the exception BTW (2.078.1 Windows 10).

Looking at each, it looks like it does this:

cast(void) unaryFun!pred(r.front);

So I tried this:

auto pred = i => a[i] = a[i-1] + 2;
foreach(i; 1 .. a.length)
    cast(void)pred(i);

And I see the -nan value. Remove the cast(void) and I don't see it.

Clearly there is some codegen issue here.

-Steve


More information about the Digitalmars-d-learn mailing list