Bug 8368 – std.algorithm.sort's template constraint is inadequate

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-10T00:24:00Z
Last change time
2015-06-09T05:15:00Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2012-07-10T00:24:37Z
This code fails to compile import std.algorithm; void main() { auto str = ["foo", "bar"].map!(x => x); auto f = str.sort(); } giving this string of errors /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7272): Error: r[i2] is not an lvalue /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7273): Error: r[i1] is not an lvalue /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7201): Error: template instance std.algorithm.swapAt!(Result) error instantiating /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7285): instantiated from here: getPivot!(binaryFun,Result) /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7034): instantiated from here: sortImpl!(binaryFun,cast(SwapStrategy)0,Result) q.d(8): instantiated from here: sort!("a < b",cast(SwapStrategy)0,Result) /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7285): Error: template instance std.algorithm.getPivot!(binaryFun,Result) error instantiating /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7034): instantiated from here: sortImpl!(binaryFun,cast(SwapStrategy)0,Result) q.d(8): instantiated from here: sort!("a < b",cast(SwapStrategy)0,Result) /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7239): Error: r[j] is not an lvalue /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7242): Error: r[j] is not an lvalue /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7357): Error: template instance std.algorithm.optimisticInsertionSort!(binaryFun,Result) error instantiating /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7034): instantiated from here: sortImpl!(binaryFun,cast(SwapStrategy)0,Result) q.d(8): instantiated from here: sort!("a < b",cast(SwapStrategy)0,Result) /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(7034): Error: template instance std.algorithm.sortImpl!(binaryFun,cast(SwapStrategy)0,Result) error instantiating q.d(8): instantiated from here: sort!("a < b",cast(SwapStrategy)0,Result) q.d(8): Error: template instance std.algorithm.sort!("a < b",cast(SwapStrategy)0,Result) error instantiating Clearly, sort's template constraint didn't properly catch the fact that the range being passed to map wouldn't compile with sort. sort fails to even test that the range is a random-access range! sort needs a proper template constraint which fails to compile with any range which won't work with it (either that or static asserts which give informative error messages when the type isn't going to work with sort). And some of the helper functions should probably have proper template constraints as well - e.g. the actual line that fails to compile is in swapAt, which has no template constraint.
Comment #1 by monarchdodra — 2013-01-02T06:53:16Z
*** Issue 9071 has been marked as a duplicate of this issue. ***
Comment #2 by github-bugzilla — 2013-01-13T16:21:52Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/2973b7ed2d50ca848628e7d5dcbbffbfa7f0b6ca Merge pull request #1048 from Poita/bug8368 Issue 8368 - Insufficient constraints for sort