issue with each specifically for x86
Ali Çehreli
acehreli at yahoo.com
Wed Mar 7 19:21:21 UTC 2018
On 03/07/2018 10:57 AM, 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
>
Confirmed on Linux with dmd 2.078.1
It's somehow related to the unused return value of the lambda. The
following code has the same error:
iota(1,a.length).each!((i) {
a[i] = a[i-1] + 2;
return a[i];
});
The error disappears when that return statement is commented-out.
Please file a dmd bug after making sure that 2.079 still has it. (Too
lazy to install right now.)
An ldc that I have handy does not have this bug:
based on DMD v2.073.2 and LLVM 4.0.0
Ali
More information about the Digitalmars-d-learn
mailing list