www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18804] New: Side effects incorrectly optimized out when

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

          Issue ID: 18804
           Summary: Side effects incorrectly optimized out when results
                    are discarded
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: Ajieskola gmail.com

I wrote this script to check my csv file for me:

void main()
{   int line = 1;

    try
    {   File("in")
        .byChunk(0x1000)
        .joiner
        .map!(to!(immutable char))
        .array
        .csvReader!(Tuple!(string, bool, string))
        .map!(a => a[2].substitute(';', '\n').csvReader!(Tuple!(float, float,
float)).array)
        .tee!(el => line++)
        .map!(tuples => tuples.map!(tup => tup[0] + tup[1] + tup[2]).sum)
        .copy(nullSink)
        ;

        writeln("No problems");
    }
    catch (Exception e)
    {   writeln("On line ", line, ": ", e);
    }
    readln;
}

It does not catch any errors reqardless whether there are, but replacing
range.copy(nullSink) with range.each!writeln fixes that. It seems the optimizer
has got a bit too eager here.

Just because nullSink discards what it receives does not mean the range
shouldn't be processed, if there may be side-effects.

--
Apr 27 2018