D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19829 - __traits(isSame) returns true for some non-local delegate lambdas even when they are different
Summary: __traits(isSame) returns true for some non-local delegate lambdas even when t...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: pull, wrong-code
Depends on:
Blocks:
 
Reported: 2019-04-26 08:34 UTC by Simen Kjaeraas
Modified: 2020-03-21 03:56 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 Simen Kjaeraas 2019-04-26 08:34:14 UTC
From issue 19828:

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");
}
Comment 1 ag0aep6g 2019-04-26 09:43:38 UTC
Two observations:

1) This prints "true" even though the two lambdas are clearly not the same:

    pragma(msg, __traits(isSame, i => x[i], a => a));

This is a bug in itself.

2) In the original code, when I move `arr` to global scope, then all writelns correctly print "Different".

This suggests that, in the context of `test` and __traits(isSame, ...), map1 might be considered to not have access to arr.
Comment 2 basile-z 2019-04-26 14:51:35 UTC
looks like a 2.079.z regression : https://run.dlang.io/is/LCV9pX
Comment 3 Simen Kjaeraas 2019-04-26 21:02:11 UTC
Not really - lambda comparison was implemented in 2.079. Before that any comparison of lambdas with isSame would return false.

Anyways, the core issue is `x` doesn't impact the serialization of lambdas, while it should render the lambda incomparable. Fixed here: https://github.com/dlang/dmd/pull/9710
Comment 4 Dlang Bot 2019-04-26 21:03:11 UTC
@Biotronic created dlang/dmd pull request #9710 "Fix issue 19829 - __traits(isSame) returns true for some non-local de…" fixing this issue:

- Fix issue 19829 - __traits(isSame) returns true for some non-local delegate lambdas even when they are different

https://github.com/dlang/dmd/pull/9710
Comment 5 Dlang Bot 2019-05-30 06:43:52 UTC
dlang/dmd pull request #9710 "Fix issue 19829 - __traits(isSame) returns true for some non-local de…" was merged into master:

- c4782963597ce3e719a10d218a7933b69d2dafac by Biotronic:
  Fix issue 19829 - __traits(isSame) returns true for some non-local delegate lambdas even when they are different

https://github.com/dlang/dmd/pull/9710