www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3646] New: Default values of function arguments are ignored when instantiating a template.

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

           Summary: Default values of function arguments are ignored when
                    instantiating a template.
           Product: D
           Version: 2.035
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: e.insafutdinov gmail.com


--- Comment #0 from Eldar Insafutdinov <e.insafutdinov gmail.com> 2009-12-25
01:02:49 PST ---
void foo(Fn)(Fn fn)
{
    fn();
}

void bar(int i = 22) {
    writeln("bar ", i);
}
void bam(int i) {
    writeln("bam ", i);
}

void baz() {
    foo(&bar);
    foo(&bam);
}

This code compiles while it should not. You cannot call bam() without any
arguments. If you switch the instantiations and call foo(&bam) first - it does
not compile.


pragma(msg, typeof(&bar).stringof);
pragma(msg, typeof(&bam).stringof);

outputs

void function(int i = 22)
void function(int i)


But it is not respected by the template.

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


Koroskin Denis <2korden gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |2korden gmail.com


--- Comment #1 from Koroskin Denis <2korden gmail.com> 2009-12-25 08:32:32 PST
---
This is a bit more tricky than it looks like.

The question here is, how many times should foo be instanciated?
DMD answers "just 1 time", and I agree with it, since bar and bam have same
signature and mangling.

Having only one instantiation means that code for it will also be generated
once.

When you call a function that has default argument without providing that
argument, the call is actually *rewritten to include that argument*. I.e.
bar(); is rewritten as bar(22);

Not lets see how foo!(bar) looks like after a rewrite:

void foo(Fn)(Fn fn)
{
    fn(22);
}

Now there is no wonder why dmd behaves like this.

But this only happens if foo!(bar) is instanciated before foo!(bam), because
when templates are instantiates on first use. When you instanciate foo!(bam)
first, it doesn't get rewritten and therefore fails to compile.

I guess the only fix for this issue would be to create an implicit trampoline
to invoke functions with default arguments, but only Walter can say for sure.

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



--- Comment #2 from Eldar Insafutdinov <e.insafutdinov gmail.com> 2009-12-26
12:51:57 PST ---
(In reply to comment #1)
 This is a bit more tricky than it looks like.
 
 The question here is, how many times should foo be instanciated?
 DMD answers "just 1 time", and I agree with it, since bar and bam have same
 signature and mangling.
 But this only happens if foo!(bar) is instanciated before foo!(bam), because
 when templates are instantiates on first use. When you instanciate foo!(bam)
 first, it doesn't get rewritten and therefore fails to compile.
 
 I guess the only fix for this issue would be to create an implicit trampoline
 to invoke functions with default arguments, but only Walter can say for sure.
Yeah, that was pretty much what I was thinking about. The issue cuts down to a question, should Foo be instantiated once or are the function types equal? For binary size and efficiency purposes I agree that there should be one instantiation. On the other hand for meta programming and code generation which is what I am doing I would like to have them as separate types, as I don't want to loose this information. Walter or Andrei would probably have to comment on this issue. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 26 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3646


Stewart Gordon <smjg iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, spec
                 CC|                            |smjg iname.com
            Version|2.035                       |1.051
             Blocks|                            |340
         OS/Version|Linux                       |All


--- Comment #3 from Stewart Gordon <smjg iname.com> 2010-01-03 08:53:39 PST ---
I've never heard of default arguments being part of the function type's
properties.  I'd probably guessed that the type is simply void function(int)
but default arguments are filled in on the caller side.  In which case the
functions are the same type, and so this isn't meant to work.  Either way, it's
certainly a bug that the code is accepted.

And it applies to D1 too, but the code needs changing a bit:
----------
import std.stdio;

void foo(Fn)(Fn fn) {
    fn();
}

void bar(int i = 22) {
    writefln("bar ", i);
}
void bam(int i) {
    writefln("bam ", i);
}

void main() {
    foo(&bar);
    foo(&bam);
}
----------

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


yebblies <yebblies gmail.com> changed:

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


--- Comment #4 from yebblies <yebblies gmail.com> 2011-07-03 22:22:51 EST ---
*** Issue 4028 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 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3646


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer utulsa.edu


--- Comment #5 from yebblies <yebblies gmail.com> 2011-07-03 22:23:21 EST ---
*** Issue 5456 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 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3646


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies gmail.com
           Platform|Other                       |All


--- Comment #6 from yebblies <yebblies gmail.com> 2011-07-03 23:14:09 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=3646



--- Comment #7 from Stewart Gordon <smjg iname.com> 2011-07-03 08:42:20 PDT ---
(In reply to comment #1)
 Not lets see how foo!(bar) looks like after a rewrite:
 
 void foo(Fn)(Fn fn)
 {
     fn(22);
 }
Actually, before this rewrite comes substituting in the template argument. void fooInstance(void function(int = 22) fn) { fn(); } which then becomes void fooInstance(void function(int) fn) { fn(22); }
 Now there is no wonder why dmd behaves like this.
But it makes sense only if there are actually two template instances involved. The essence of the bug is that, after it has instantiated foo with argument void function(int = 22), the compiler matches any attempt to instantiate foo with a void function(int), with or without a default argument, to this instance. (In reply to comment #2)
 Yeah, that was pretty much what I was thinking about.  The issue 
 cuts down to a question, should Foo be instantiated once or are the 
 function types equal?  For binary size and efficiency purposes I 
 agree that there should be one instantiation.  On the other hand 
 for meta programming and code generation which is what I am doing I 
 would like to have them as separate types, as I don't want to loose 
 this information.  Walter or Andrei would probably have to comment 
 on this issue.
But this seems to me a rare use case. And you can do it with an alias template parameter, though this does create a separate instance for each function name: ---------- import std.stdio; void foo(alias fn)() { fn(); fn(42); } void bar(int i = 22) { writefln("bar %d", i); } void bam(int i) { writefln("bam %d", i); } void quux() { writefln("quux"); } void quux(int i) { writefln("quux %d", i); } void main() { foo!(bar)(); // works //foo!(bam)(); // correctly triggers compiler error (1.068 Win32) foo!(quux)(); // works with overloads as well } ---------- I can see three possible ways to resolve this issue: (a) Default arguments aren't part of the function type. Then only one template instance exists: foo!(void function(int)). The template instance is then illegal, since it tries to call a void function(int) with no arguments. (b) Default arguments are part of the function type. Then there are two template instances: foo(&bar) is foo!(void function(int = 22)) foo(&bam) is foo!(void function(int)) Then foo(&bar) is legal: foo(&bar)() calls bar(22) and foo(&bam) is illegal (c) Default arguments are part of the function type, but can be optimised away. Essentially, we detect while instantiating the template whether the template body makes use of a default argument. If so, create a separate instance for each default argument or absence thereof. Otherwise, create only one instance. Not sure whether this is desirable. -- 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=3646


yebblies <yebblies gmail.com> changed:

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


--- Comment #8 from yebblies <yebblies gmail.com> 2012-02-12 13:57:19 EST ---
This has the same causes (and same solutions) as issue 3866.

-- 
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=3646


Mike Parker <aldacron gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aldacron gmail.com
            Version|D1 & D2                     |D2
           Severity|normal                      |regression


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


Mike Parker <aldacron gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|D2                          |D1 & D2
           Severity|regression                  |normal


--- Comment #9 from Mike Parker <aldacron gmail.com> 2012-06-17 22:02:35 PDT ---
Somehow I managed to change the settings on the wrong bug. Sorry for the noise.

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


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |puneet coverify.org


--- Comment #10 from Jonathan M Davis <jmdavisProg gmx.com> 2012-07-02 08:34:37
PDT ---
*** Issue 8336 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 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3646


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #11 from Kenji Hara <k.hara.pg gmail.com> 2012-09-05 18:53:52 PDT
---
D2 pull:
https://github.com/D-Programming-Language/dmd/pull/1102

-- 
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=3646



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

https://github.com/D-Programming-Language/dmd/commit/877e7bfd7c29fb016323934bb04bd3420bffe5c7
Default args and arg identifiers are now volatile information in TypeFunction.

1. The extra informations would be kept after semantic analysis for function
pointer and delegate types. But merging type would strip all the extras from
the type structure, then cache the stripped copy into.

2. The types has extra informations cannot be named. In other words,
AliasDeclaration would always strip the aliased type. This rule also works for
Template Type Parameter, and fixes issue 3646 well.

3. CondExp would strip them from the type of its result.

https://github.com/D-Programming-Language/dmd/commit/bce2d07d6cd2c09a5153afc65260b63c32447ade
fix Issue 3646 - Default values of function arguments are ignored when
instantiating a template.

-- 
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=3646


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

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


--- Comment #13 from Kenji Hara <k.hara.pg gmail.com> 2013-07-04 04:51:50 PDT
---
Right now, default argument is changed to not part of function type in D2.

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