Thread overview
[Issue 9934] New: template default argument should be semantically analysed
Apr 15, 2013
Martin Nowak
Apr 15, 2013
Andrej Mitrovic
Apr 16, 2013
Kenji Hara
Apr 16, 2013
Andrej Mitrovic
Apr 16, 2013
timon.gehr@gmx.ch
April 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9934

           Summary: template default argument should be semantically
                    analysed
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: code@dawg.eu


--- Comment #0 from Martin Nowak <code@dawg.eu> 2013-04-14 23:08:01 PDT ---
cat > bug.d << CODE
template foo(T = NonExistent)
{
}
CODE

dmd -c bug

----

This currently compiles even though the default parameter can never be valid. When the default parameter is a template instance it is missing from the object file. I think we should run full semantics on the default arguments when compiling a module.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9934


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

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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-15 02:05:23 PDT ---
What if the template is in a mixin template? Or would the change not affect this?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9934



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2013-04-15 18:46:04 PDT ---
(In reply to comment #1)
> What if the template is in a mixin template? Or would the change not affect this?

Even if the template is used for mixin, default template arguments are evaluated in declaration side.

alias NonExistent = int;   // [a]
template foo(T = NonExistent)   // [b]
{
    pragma(msg, T); // prints 'int'
}
class C {
    alias NonExistent = string;
    mixin foo;
}

If you comment out line [a], compiler reports "Error: undefined identifier NonExistent" in line [b].

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9934



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-16 03:08:18 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > What if the template is in a mixin template? Or would the change not affect this?
> 
> Even if the template is used for mixin, default template arguments are evaluated in declaration side.

Ok makes sense, I never tried it before.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9934


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch
           Severity|normal                      |enhancement


--- Comment #4 from timon.gehr@gmx.ch 2013-04-16 03:18:04 PDT ---
(In reply to comment #0)
> cat > bug.d << CODE
> template foo(T = NonExistent)
> {
> }
> CODE
> 
> dmd -c bug
> 
> ----
> 
> This currently compiles even though the default parameter can never be valid. When the default parameter is a template instance it is missing from the object file. I think we should run full semantics on the default arguments when compiling a module.

This is not really clear. Default parameters can depend on other template arguments. Therefore, running full semantics is not possible.

You'd have to specify clearly in what cases there is an error. However, I think it is not necessarily a good idea to add non-obvious rules about validity of a template declaration.

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