Bug 8900 – Using zip with char[] sometimes fails to compile

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-26T15:01:00Z
Last change time
2012-12-20T10:15:52Z
Keywords
rejects-valid
Assigned to
nobody
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2012-10-26T15:01:50Z
CODE: ----------------------------- import std.algorithm; import std.range; version = Bug; auto cartesianProd(R1,R2)(R1 range1, R2 range2) { version(Bug) { ElementType!R2 a; return zip(range1.save, repeat(a)); } else { // For some crazy reason, this one works. ElementType!R1 a; return zip(repeat(a), range2.save); } } void main() { auto arr = cartesianProd([1,2], ['a','b']); } ------------------------------ DMD output: ------------------------------ /usr/src/d/phobos/std/format.d(1937): Error: template std.range.walkLength does not match any function template declaration /usr/src/d/phobos/std/range.d(1321): Error: template std.range.walkLength cannot deduce template function from argument types !()(Repeat!(dchar)) /usr/src/d/phobos/std/format.d(2495): Error: template instance std.format.formatRange!(Appender!(string),Repeat!(dchar),char) error instantiating /usr/src/d/phobos/std/format.d(2172): instantiated from here: formatValue!(Appender!(string),Repeat!(dchar),char) /usr/src/d/phobos/std/typecons.d(552): instantiated from here: formatElement!(Appender!(string),Repeat!(dchar),char) /usr/src/d/phobos/std/range.d(3700): instantiated from here: Tuple!(int[],Repeat!(dchar)) /usr/src/d/phobos/std/range.d(4060): ... (1 instantiations, -v to show) ... test.d(11): instantiated from here: zip!(int[],Repeat!(dchar)) test.d(22): instantiated from here: cartesianProd!(int[],char[]) /usr/src/d/phobos/std/format.d(2172): Error: template instance std.format.formatValue!(Appender!(string),Repeat!(dchar),char) error instantiating /usr/src/d/phobos/std/typecons.d(552): instantiated from here: formatElement!(Appender!(string),Repeat!(dchar),char) /usr/src/d/phobos/std/range.d(3700): instantiated from here: Tuple!(int[],Repeat!(dchar)) /usr/src/d/phobos/std/range.d(4060): instantiated from here: Zip!(int[],Repeat!(dchar)) test.d(11): instantiated from here: zip!(int[],Repeat!(dchar)) test.d(22): instantiated from here: cartesianProd!(int[],char[]) /usr/src/d/phobos/std/typecons.d(552): Error: template instance std.format.formatElement!(Appender!(string),Repeat!(dchar),char) error instantiating /usr/src/d/phobos/std/range.d(3700): instantiated from here: Tuple!(int[],Repeat!(dchar)) /usr/src/d/phobos/std/range.d(4060): instantiated from here: Zip!(int[],Repeat!(dchar)) test.d(11): instantiated from here: zip!(int[],Repeat!(dchar)) test.d(22): instantiated from here: cartesianProd!(int[],char[]) /usr/src/d/phobos/std/range.d(4060): Error: template instance std.range.Zip!(int[],Repeat!(dchar)) error instantiating test.d(11): instantiated from here: zip!(int[],Repeat!(dchar)) test.d(22): instantiated from here: cartesianProd!(int[],char[]) test.d(11): Error: template instance std.range.zip!(int[],Repeat!(dchar)) error instantiating test.d(22): instantiated from here: cartesianProd!(int[],char[]) test.d(22): Error: template instance test.cartesianProd!(int[],char[]) error instantiating ------------------------------ Oddly enough, commenting out the "version = Bug" line at the top (which switches the order of a few parameters to zip) makes the problem go away.
Comment #1 by peter.alexander.au — 2012-10-26T16:47:21Z
What version of DMD are you using? I can't reproduce with 2.060, or from git HEAD. http://dpaste.dzfl.pl/949738af
Comment #2 by hsteoh — 2012-10-26T18:33:50Z
Hmm that is strange. I'm using git HEAD too, for dmd, druntime, and phobos. I just did a git pull and make clean ; make, and I'm still getting the same error. I'm on 64-bit Linux, if that makes any difference (though I can't see why it should).
Comment #3 by hsteoh — 2012-10-26T18:37:16Z
P.S. I meant that I did a git pull, make clean, and make, for all three, dmd, druntime, phobos (in that order). Still same error.
Comment #4 by peter.alexander.au — 2012-10-27T03:25:26Z
(In reply to comment #2) > Hmm that is strange. I'm using git HEAD too, for dmd, druntime, and phobos. I > just did a git pull and make clean ; make, and I'm still getting the same > error. My bad, I get the bug now on git HEAD. The break was caused by this: https://github.com/D-Programming-Language/phobos/pull/880 The problem is that walkLength is called on the second range, which is infinite. This makes no sense, which is the reason for that pull request. Where is walkLength called? Zip has a Tuple!R element. And Tuple!R's toString function tries to print out the range. Why is it only called in version=Bug? Because formatElement is specialised for strings, and just happens to not call walkLength eventually. I'll fix this, but needless to say this is a mess.
Comment #5 by peter.alexander.au — 2012-10-27T03:57:28Z
Comment #6 by github-bugzilla — 2012-12-13T09:00:22Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/a00c3c825c863c0c53cb69ab907c7be539bf51e4 Fix bug 8900 - Zip with infinite range fails. `std.range.Zip` contains a `Tuple`, whose `toString()` function calls `formatElement` which eventually calls this function, leading to a static call to `walkLength` on infinite ranges. Since pul request 880, `walkLength` doesn't work with infinite ranges, so it fails to compile. This change ensures that `walkLength` is called with only valid range types. Bug: http://d.puremagic.com/issues/show_bug.cgi?id=8900 https://github.com/D-Programming-Language/phobos/commit/259cd23d67b8025eebc5c46e3756e2c98337a663 Merge pull request #901 from Poita/bug8900 Fix bug 8900 - Zip with infinite char range fails.
Comment #7 by hsteoh — 2012-12-20T10:15:52Z
Confirmed fixed in latest git dmd (adcdfd7bd0cdea8b0f79cef4a687cd4eb77b3c53).