www.digitalmars.com         C & C++   DMDScript  

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


          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;

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

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

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