D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15225 - cannot overload directly aliased function literals
Summary: cannot overload directly aliased function literals
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, rejects-valid
: 16099 (view as issue list)
Depends on:
Blocks:
 
Reported: 2015-10-19 15:06 UTC by Kenji Hara
Modified: 2020-12-21 02:15 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2015-10-19 15:06:54 UTC
Test case:
---
alias foo = (int x) => x;
alias foo = (string x) => x;

void main()
{
    foo(1);
    foo("a");    // line 7
}

Output:
---
test.d(7): Error: function literal __lambda4 (int x) is not callable using argument types (string)
Comment 1 Vladimir Panteleev 2015-10-27 09:11:07 UTC
(In reply to Kenji Hara from comment #0)
> alias foo = (int x) => x;
> alias foo = (string x) => x;

This syntax doesn't work in released DMD versions, but if I replace it with:

alias I(alias X) = X;
alias foo = I!((int x) => x);
alias foo = I!((string x) => x);

then compilation succeeds (though then I get a linker error).
Comment 2 Vladimir Panteleev 2017-07-03 20:40:19 UTC
(In reply to Vladimir Panteleev from comment #1)
> then compilation succeeds (though then I get a linker error).

Linking succeeds since 2.071.0 (fixed by https://github.com/dlang/dmd/pull/5523). Kenji's example still doesn't compile.
Comment 3 ag0aep6g 2018-02-25 12:19:53 UTC
*** Issue 16099 has been marked as a duplicate of this issue. ***
Comment 4 basile-z 2019-04-22 23:34:07 UTC
A second level of aliasing is required:

    alias lambda1 = (int x) => x;
    alias lambda2 = (string x) => x;
    alias lambda = lambda1;
    alias lambda = lambda2;

    void main()
    {
        auto r0 = lambda(1);
        auto r1 = lambda("a");
    }  

But this should not be the case since, as found in one of the duplicated reports, this works fine if you replace lambda1/lambda2 by standard functions.

It turns out that the problem is that when doing aliasSemantic() for lambda1, its `.overnext` member is null. so without the second level of aliasing, the first match is always used.

It's quite possible that the semantic is done in the wrong order., e.g the call is resolved before that the overload set is built.
Comment 5 Dlang Bot 2020-12-21 01:27:14 UTC
@BorisCarvajal created dlang/dmd pull request #12041 "Fix Issue 15225 - cannot overload directly aliased function literals" fixing this issue:

- Fix Issue 15225 - cannot overload directly aliased function literals

https://github.com/dlang/dmd/pull/12041
Comment 6 Dlang Bot 2020-12-21 02:15:41 UTC
dlang/dmd pull request #12041 "Fix Issue 15225 - cannot overload directly aliased function literals" was merged into master:

- f04ab2275a948b3b4ea858523b326db971201eb4 by Boris Carvajal:
  Fix Issue 15225 - cannot overload directly aliased function literals

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