Bug 4597 – std.algorithm.filter fails with a const range

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2010-08-07T22:29:00Z
Last change time
2014-02-25T17:51:45Z
Keywords
rejects-valid
Assigned to
nobody
Creator
braddr

Comments

Comment #0 by braddr — 2010-08-07T22:29:10Z
Index: std/algorithm.d =================================================================== --- std/algorithm.d (revision 1807) +++ std/algorithm.d (working copy) @@ -830,6 +830,9 @@ // With chain assert(equal(filter!overX(chain(a, nums)), [22, 42])); + + const int[] list = [ 1, 2, 10, 11, 3, 4 ]; + assert(equal(filter!overX(list), [ 1, 2, 10, 3, 4 ])); } // move Adding that test produces the folling failure: std/algorithm.d(737): Error: this is not mutable That's the opSlice function of struct Filter: ref Filter opSlice() { return this; }
Comment #1 by andrei — 2010-08-07T22:37:26Z
This is quite problematic. Most of std.algorithm fails on const and immutable arrays because it does not do the conversion const(int[]) -> const(int)[] and immutable(int[]) -> immutable(int)[], both of which are legal. I'm not sure how this can be addressed. A brute force solution would be to special-case all algorithms for certain array types, but that would be horrible. Ideas are welcome.
Comment #2 by braddr — 2010-08-07T22:56:28Z
note, the assert is wrong, should be: assert(equal(filter!overX(list), [ 11 ]));
Comment #3 by yebblies — 2011-09-14T19:16:36Z
(In reply to comment #1) > I'm not sure how this can be addressed. A brute force solution would be to > special-case all algorithms for certain array types, but that would be > horrible. Ideas are welcome. One idea is issue 6289
Comment #4 by peter.alexander.au — 2014-01-26T06:42:53Z
This works now that head-const is removed for arrays.
Comment #5 by andrei — 2014-01-26T10:39:30Z
(In reply to comment #4) > This works now that head-const is removed for arrays. Could you please point to the related language change? Thanks!
Comment #6 by peter.alexander.au — 2014-01-26T12:14:00Z
(In reply to comment #5) > (In reply to comment #4) > > This works now that head-const is removed for arrays. > > Could you please point to the related language change? Thanks! I've determined that it was fixed in 2.050 and there's this on the change log: http://dlang.org/changelog.html#new2_050 "std.traits: Most higher-order ranges now work with const/immutable arrays and other ranges with a natural tail const, and ranges w/ const/immutable elements." Unfortunately this was before we used git, so I can't find any pull requests.
Comment #7 by peter.alexander.au — 2014-01-26T13:13:52Z
(In reply to comment #5) > (In reply to comment #4) > > This works now that head-const is removed for arrays. > > Could you please point to the related language change? Thanks! Ok, I've tracked it down: This commit was technically the first that fixed the bug: https://github.com/D-Programming-Language/phobos/commit/b597d23f83c957520476b5d90c889d312b1a4ddb However, in dmd 2.056 the language was changed so that head const was stripped from array slices so that would have fixed the issue as well. https://github.com/D-Programming-Language/dmd/pull/236
Comment #8 by peter.alexander.au — 2014-02-16T14:25:36Z
Changed to WORKSFORME so that it doesn't appear in the changelog for next update.
Comment #9 by andrei — 2014-02-25T17:51:45Z
Thanks Peter for tracking this down!