Bug 17188 – stdc qsort predicate requires scope parameters

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-02-16T05:03:31Z
Last change time
2018-03-02T23:19:30Z
Keywords
safe
Assigned to
No Owner
Creator
Ketmar Dark

Comments

Comment #0 by ketmar — 2017-02-16T05:03:31Z
the following code doesn't compile anymore (git HEAD): import core.stdc.stdlib; extern(C) int cmp (const void *a, const void *b) { return 0; } void main () { int[4] arr; qsort(arr.ptr, arr[0].sizeof, arr.length, &cmp); } ...due to added `scope` in compare function prototype. i think we should not break user's code right away, there should be a deprecation stage first.
Comment #1 by bugzilla — 2017-04-12T04:47:46Z
The trouble is, I'm not sure how to deprecate in a way that still checks for scope.
Comment #2 by r.sagitario — 2017-10-08T13:38:14Z
As qsort is @system anyway, the callback does not have to guarantee anything. To still be able to pass a function with scope parameters, how about overloading it with both variants: alias int function(scope const void*, scope const void*) _scope_compare_fp_t; alias int function(const void*, const void*) _compare_fp_t; void qsort(scope void* base, size_t nmemb, size_t size, _scope_compare_fp_t compar); void qsort(scope void* base, size_t nmemb, size_t size, _compare_fp_t compar);
Comment #3 by r.sagitario — 2017-10-08T13:50:59Z
The overload with a non-scope callback should also not pretend to not escape the base pointer: void qsort(void* base, size_t nmemb, size_t size, _compare_fp_t compar);
Comment #4 by greensunny12 — 2017-12-13T09:20:23Z
It's been more than ten months since the bug was report, hence I think that triggering a deprecation message now, would be way too late -> closing.
Comment #5 by r.sagitario — 2017-12-16T13:23:21Z
My suggestion with two overloads would not need a deprecation, but allow both scope and non-scope versions.
Comment #6 by petar.p.kirov — 2017-12-16T14:22:47Z
I agree with Rainer that we should use overloading.
Comment #7 by code — 2018-02-28T18:22:04Z
> As qsort is @system anyway, the callback does not have to guarantee anything. To still be able to pass a function with scope parameters, how about overloading it with both variants I think we: - Need to add some sort of wildcard attribute for such cases, as declinating all 16 combinations of `@nogc`, `pure`, `nothrow`, `@safe` callbacks isn't feasible. - Should simply remove scope in this specific case as the function is @system and scope won't be checked anyhow, overloading with both variants doesn't buy us anything.
Comment #8 by github-bugzilla — 2018-03-02T23:19:28Z
Commits pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/02f30af1e4b90de78dfa959abd4ba0d3c77e0bef fix Issue 17188 - qsort predicate requires scope parameters - remove scope as it's not checked for `@system` functions anyhow - adding overloads would be an alternative approach, but it's 32 variants to support all of `scope`, `@safe`, `pure`, `nothrow`, `@nogc` https://github.com/dlang/druntime/commit/87656c3bf3661cbd7dca2eb2cbff48df2c022b52 Merge pull request #2122 from MartinNowak/fix17188 fix Issue 17188 - qsort predicate requires scope parameters