Bug 18804 – std.algorithm.mutation.copy puts whole source range into target range when it should put elements
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-04-27T15:31:21Z
Last change time
2018-06-18T22:34:20Z
Keywords
pull
Assigned to
No Owner
Creator
Ajieskola
Comments
Comment #0 by Ajieskola — 2018-04-27T15:31:21Z
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.
Comment #1 by ag0aep6g — 2018-04-30T01:07:11Z
Generally, please post complete code, including imports. Also include the contents of other files like "in".
What happens here is that `copy` puts the range as a whole into the NullSink. This is not DMD optimizing anything out.
Reduced test case:
----
import std.algorithm.mutation: copy;
struct NullSink
{
void put(E)(E) {}
}
int line = 0;
struct R
{
int front;
@property bool empty() { return line == 1; }
void popFront() { line = 1; }
}
void main()
{
R r;
copy(r, NullSink());
assert(line == 1); /* fails; should pass */
}
----
Pull request to fix this:
https://github.com/dlang/phobos/pull/6485
Comment #2 by Ajieskola — 2018-04-30T11:11:47Z
Already being fixed? Thanks!
imports are, on top of file:
import std.stdio, std.file, std.conv, std.algorithm, std.range, std.string, std.typecons, std.math, std.csv;
A few examples of correct lines from "in":
Kukat,Gif\Kukat\1_Tumma_patina\05_Ruusu_tu.gif,false,"0.07,0.18,0.0; 0.09,0.25,0.0"
Kukat,Gif\Kukat\1_Tumma_patina\06_Ruusu_tu.gif,false,"0.07,0.19,0.0; 0.1,0.28,0.0"
Kukat,Gif\Kukat\1_Tumma_patina\09_Ruusu_tu.gif,false,"0.1,0.4,0.0; 0.1,0.56,0.0"
Kukat,Gif\Kukat\1_Tumma_patina\10_Ruusukoynnos_tu.gif,false,"0.1,0.375,0.0"
Comment #3 by github-bugzilla — 2018-06-18T22:34:19Z