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);
}
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