Bug 12265 – Puritiy inference fails with passing template function as an alias?

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-26T01:08:10Z
Last change time
2024-12-13T18:17:28Z
Assigned to
No Owner
Creator
Dmitry Olshansky
Moved to GitHub: dmd#18782 →

Comments

Comment #0 by dmitry.olsh — 2014-02-26T01:08:10Z
Distilled from std.algorithm.sort and is a blocker for its purity inference. Tested with DMD64 D Compiler v2.065-devel-671874e. The test case: // (things like this are introduced by binaryFun template) bool cmp(T)(auto ref T a, auto ref T b) { return a < b; } template sortImpl(alias pred, R) { void sort(R arr) //pure //fails to infer pure here { pred(arr[0], arr[1]); } } T[] sorted(T)(T[] stuff) { //this works as pure sortImpl!((a, b) => a < b, T[]).sort(stuff); //this doesn't sortImpl!(cmp, T[]).sort(stuff); return stuff; } void main() pure { int[] a = [1, 8, 3, 16]; a = sorted(a); }
Comment #1 by k.hara.pg — 2014-02-26T02:40:57Z
Because of the issue 10134, current dmd does not infer attributes for the sortImpl!(cmp, T[]).sort. Related compiler code: https://github.com/D-Programming-Language/dmd/blob/master/src/func.c#L1122 https://github.com/D-Programming-Language/dmd/blob/master/src/func.c#L2108
Comment #2 by k.hara.pg — 2014-04-18T06:48:49Z
*** Issue 12410 has been marked as a duplicate of this issue. ***
Comment #3 by monarchdodra — 2014-04-18T06:58:27Z
(In reply to Dmitry Olshansky from comment #0) > Distilled from std.algorithm.sort and is a blocker for its purity inference. Work around for sort: https://github.com/D-Programming-Language/phobos/pull/2075
Comment #4 by ttanjo — 2021-05-16T04:53:35Z
The above code does not work yet but it is due to a non-eponymous template rather than passing function as an alias parameter. Here is a reproducible code (also in https://run.dlang.io/is/DErgFM): ```dlang // eponymous template: infer pure template sortImpl1(alias pred, R) { void sortImpl1(R arr) { pred(arr[0], arr[1]); } } // non-eponymous template: failed to infer pure template sortImpl2(alias pred, R) { void sort(R arr) { pred(arr[0], arr[1]); } } void main() pure { int[] a = [1, 8, 3, 16]; sortImpl1!((a, b) => a < b, int[])(a); sortImpl2!((a, b) => a < b, int[]).sort(a); // line 23 } ``` I obtained the following error message: ``` onlineapp.d(23): Error: `pure` function `D main` cannot call impure function `onlineapp.main.sortImpl2!((a, b) => a < b, int[]).sort` ```
Comment #5 by robert.schadek — 2024-12-13T18:17:28Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18782 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB