Issue 13615 - stable sort not usable in @safe code
Summary: stable sort not usable in @safe code
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: safe
Depends on: 16139
Blocks:
  Show dependency treegraph
 
Reported: 2014-10-14 06:31 UTC by Brad Roberts
Modified: 2016-09-14 10:01 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Brad Roberts 2014-10-14 06:31:28 UTC
Adding safe to the stable sort unit tests, as follows:

@safe unittest
{
    // Showcase stable sorting
    string[] words = [ "aBc", "a", "abc", "b", "ABC", "c" ];
    sort!("toUpper(a) < toUpper(b)", SwapStrategy.stable)(words);
    assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
}

std/algorithm.d(10241): Error: safe function 'std.algorithm.__unittestL10237_301' cannot call system function 'std.algorithm.sort!("toUpper(a) < toUpper(b)", cast(SwapStrategy)2, string[]).sort'

I see no direct tests for either the quicksortimpl or timsortimpl, which is itself a problem.  Creation of tests for both of those impls and having them marked @safe should be a part of fixing this issue.
Comment 1 Walter Bright 2016-06-08 05:43:10 UTC
This sort() calls TimSortImpl.sort(). It is @system because it calls uninitializedArray(), which is an @system function.
Comment 2 Walter Bright 2016-06-08 05:58:02 UTC
Better test case:

@safe void foo()
{
    import std.algorithm;

    // Showcase stable sorting
    string[] words = [ "aBc", "a", "abc", "b", "ABC", "c" ];
    sort!("toUpper(a) < toUpper(b)", SwapStrategy.stable)(words);
    assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
}
Comment 3 Walter Bright 2016-06-08 06:06:48 UTC
This will also require https://issues.dlang.org/show_bug.cgi?id=16139 to be fixed first.
Comment 4 Walter Bright 2016-09-14 10:01:11 UTC
This is working in the latest master.