www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3866] New: anonymous delegate with default parameters cross-talks to another anonymous delegate

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866

           Summary: anonymous delegate with default parameters cross-talks
                    to another anonymous  delegate
           Product: D
           Version: 2.040
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: philippe.sigaud gmail.com


--- Comment #0 from Philippe Sigaud <philippe.sigaud gmail.com> 2010-03-01
18:36:25 CET ---
If you declare an anonymous delegate with default parameter, it affects another
anonymous delegate with the same type:

import std.stdio;
void main() {

    auto foo = (int a = 1) { return a;};
    auto bar = (int a) { return a;};

    writeln(foo()); // writes '1'
    writeln(bar()); // writes '1' also!
}

It provokes (correctly) an error if bar is defined before foo: bar() called
with 0 argument instead of 1.

It does not affect delegate with another type

    auto baz = (double a) { return a;}; 
    writeln(baz()); // Error, correct behaviour

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 01 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |clugdbug yahoo.com.au
           Severity|normal                      |critical


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies gmail.com
           Platform|Other                       |All
         OS/Version|Windows                     |All


--- Comment #1 from yebblies <yebblies gmail.com> 2011-07-03 23:14:23 EST ---
https://github.com/D-Programming-Language/dmd/pull/204

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|nobody puremagic.com        |yebblies gmail.com


--- Comment #2 from yebblies <yebblies gmail.com> 2012-02-12 13:55:08 EST ---
The problem occurs because default parameters are stored in the type, but types
are considered equivalent if their mangled names match, and mangled names don't
include parameter names or default arguments.

The solution is probably either to include parameter names and default
arguments in the comparison for equality, or to move them out of function types
and into function declarations.  I'm leaning towards the latter.

This case _can_ be fixed by refusing to merge function pointers, but the
problem will still exists for pointers to function pointers, structs containing
function pointers, etc.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 11 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2012-04-25
20:35:30 PDT ---
Moving the default arguments out of the type and into the function declaration
will resolve the problem, at the cost of you would no longer be able to have
default arguments for function literals or any pointers to functions.

Perhaps that's a good thing.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 25 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #4 from Jonathan M Davis <jmdavisProg gmx.com> 2012-04-26 10:33:19
PDT ---
It was my understanding that default arguments were just arguments that were
inserted when a function was called and you didn't provide all of the
arguments. They aren't actually part of the signature or type at all. As such,
they are only known if you call the function directly. If you use a function
pointer (or delegate), then all you have is the signature, so you don't have
any default arguments. As such, nested functions with default arguments such as

static auto foo(int a = 1)
{
    return a;
}

and

auto foo(int a = 1)
{
    return a + b;
}

should work just fine, but as with any function, as soon as you take their
address or turn them into a delegate variable, their default arguments are
lost. And so having default arguments in something like

auto foo = (int a = 1) { return a;};
auto bar = (int a) { return a;};

is completely pointless, because all you have is a variable which knows the
signature of the function/delegate to call. The function itself can't be called
directly, so it doesn't have any default arguments associated with it, and so
there's no point in the default arguments even being legal.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866



--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2012-04-26
23:06:52 PDT ---
Leaving this as-is for D1 to avoid breaking existing code.

For D2, changing behavior so that default args are part of the declaration, not
the type, and so both function calls in the example are illegal, since calling
through a function pointer now cannot have default args.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866



--- Comment #6 from github-bugzilla puremagic.com 2012-04-26 23:10:36 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/acc22ce25db42facfe4917aeceabd28a410f4c95
fix Issue 3866 - anonymous delegate with default parameters cross-talks to
another anonymous delegate

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eco gnuk.net


--- Comment #7 from Walter Bright <bugzilla digitalmars.com> 2012-07-22
13:08:12 PDT ---
*** Issue 8258 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sludwig outerproduct.org


--- Comment #8 from Walter Bright <bugzilla digitalmars.com> 2012-07-24
23:01:48 PDT ---
*** Issue 8430 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator gmail.com


--- Comment #9 from Walter Bright <bugzilla digitalmars.com> 2012-07-25
11:44:18 PDT ---
*** Issue 8438 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 25 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #10 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2012-07-30
17:37:07 PDT ---
Personally I never used default values in delegates but I think this pull broke
this real-world code from Derelict:

void loadPlatformGL(void delegate(void**, string, bool doThrow = true)
bindFunc)
{
    bindFunc(null, "foo1");
    bindFunc(null, "foo2");
    // ...
}

In this case the default value is useful since it's only used from within the
function, and it saves on typing since there are typically dozens of
invocations of the delegate (in Derelict).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 30 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kolos80 bk.ru


--- Comment #11 from Kenji Hara <k.hara.pg gmail.com> 2012-08-04 10:12:29 PDT
---
*** Issue 8402 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |spam extrawurst.org


--- Comment #12 from Walter Bright <bugzilla digitalmars.com> 2012-08-06
15:31:02 PDT ---
*** Issue 8515 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |


--- Comment #13 from Kenji Hara <k.hara.pg gmail.com> 2012-09-05 19:02:04 PDT
---
In the forum discussion:
http://forum.dlang.org/thread/mailman.1421.1346020012.31962.digitalmars-d puremagic.com

We decided to improve the status for default argument issues.
Then I've created a new pull which contains some fixups for this issue.

D2 pull:
https://github.com/D-Programming-Language/dmd/pull/1102

So I reopen this for D2.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866



--- Comment #14 from github-bugzilla puremagic.com 2013-07-04 03:04:04 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/d7362898f16d7d5e04ac4e9f374a39a5e8e0ff53
Improved fix for Issue 3866 - anonymous delegate with default parameters
cross-talks to another anonymous delegate

This reverts commit acc22ce25db42facfe4917aeceabd28a410f4c95,
and moves original test into runnable/functype.d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 04 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3866


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
            Version|D1 & D2                     |D2
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 04 2013