Bug 13805 – Nested struct initialization error

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-12-01T16:42:00Z
Last change time
2015-02-18T03:40:53Z
Keywords
rejects-valid
Assigned to
peter.alexander.au
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-12-01T16:42:09Z
From issue 13595. This looks like a compiler bug: void main() { import std.algorithm: map, groupBy; //[""].map!((string s) => s).groupBy!((x, y) => true); // OK [""].map!((s) => s).groupBy!((x, y) => true); // Error } dmd 2.067alpha gives: ...\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(4659): Error: field _prev must be initialized in constructor, because it is nested struct ...\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(4770): Error: template instance temp.main.groupBy!(__lambda2, cast(Flag)false, MapResult!(__lambda1, string[])) error instantiating temp.d(5): instantiated from here: groupBy!((x, y) => true, MapResult!(__lambda1, string[])) The error doesn't appear if you replace "(s)" with "(string s)".
Comment #1 by k.hara.pg — 2014-12-01T17:17:07Z
This would be a Phobos bug in GroupByImpl struct. static if (isForwardRange!Range) { private Range _prev; private void savePrev() { _prev = r.save; } private @property ElementType!Range prev() { return _prev.front; } } else { private ElementType!Range _prev; private void savePrev() { _prev = r.front; } private alias prev = _prev; } this(Range _r) { r = _r; if (!empty) { // Check reflexivity if predicate is claimed to be an equivalence // relation. assert(!equivRelation || pred(r.front, r.front), "predicate " ~ pred.stringof ~ " is claimed to be "~ "equivalence relation yet isn't reflexive"); savePrev(); // <---- here } } As error message says, the _prev field is not correctly initialized inside constructor. Indeed, it's assigned in savePrev member function, but compiler cannot handle the assignment as the initialization.
Comment #2 by peter.alexander.au — 2015-01-02T00:45:17Z
Comment #3 by github-bugzilla — 2015-01-02T01:40:28Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/463fe6931136d1743279b98d30577c523f4efb30 Fix Issue 13805 - groupBy over nested struct range GroupByImpl needs to initialize _prev directly in the constructor when it is of a nested struct range. https://issues.dlang.org/show_bug.cgi?id=13805 https://github.com/D-Programming-Language/phobos/commit/e9ee55c6759f30c0b89c329fb62a3c80974e72b9 Merge pull request #2832 from Poita/Issue13805 Fix Issue 13805 - groupBy over nested struct range
Comment #4 by github-bugzilla — 2015-02-18T03:40:53Z