Comment #0 by jeromerichard111 — 2019-04-26T07:29:35Z
Hello,
The following code give the result 4 with recent compiler (DMD, LDC) versions while the correct result is 1.
GDC and an old DMD give the correct result while LDC and the newer versions of DMD fail.
The version tested is LDC 1.15.0 from the Nix package manager (last package update).
The same issue appear in LDC 1.12.0 from the Debian testing repository.
The latest CI version seems to produce the same result: https://godbolt.org/z/rdnwH3 .
According to run.dlang.io, it worked for DMD up to version 2.079 (returning 1), but regressed since v2.080 (returning 4).
See the previous issue (3064) sent to LDC for more information.
Moreover, it seems affected by whether using a lambda or not.
module main;
import std.stdio;
import std.range;
import std.algorithm.searching;
void main(string[] args)
{
int[] tab = [1, 2, 3, 4];
int[] arr = [23, 42, 17, 5, 31];
tab.maxElement!((i) { return arr[i]; }).writeln;
}
Comment #1 by simen.kjaras — 2019-04-26T08:36:38Z
This issue boils down to this line in std.algorithm.searching:
static if (__traits(isSame, map, a => a))
For some reason, that returns true for some lambdas that are absolutely not identical. Reduced version:
import std.stdio : writeln;
unittest {
int[] arr = [0, 1];
// Different.
writeln(__traits(isSame, i => arr[i], a => a) ? "Same" : "Different");
// Same.
test!(i => arr[i], a => a);
// Different.
test!(i => arr[i], a => a * 4);
}
auto test(alias map1, alias map2)() {
writeln(__traits(isSame, map1, map2) ? "Same" : "Different");
}
Filed that as a separate issue 19829 as this can be fixed in Phobos without fixing DMD.