Bug 9934 – template default argument should be semantically analysed
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-14T23:08:00Z
Last change time
2014-07-13T10:17:05Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2013-04-14T23:08:01Z
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.
Comment #1 by andrej.mitrovich — 2013-04-15T02:05:23Z
What if the template is in a mixin template? Or would the change not affect this?
Comment #2 by k.hara.pg — 2013-04-15T18:46:04Z
(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].
Comment #3 by andrej.mitrovich — 2013-04-16T03:08:18Z
(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.
Comment #4 by timon.gehr — 2013-04-16T03:18:04Z
(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.
Comment #5 by verylonglogin.reg — 2014-07-13T10:17:05Z
*** This issue has been marked as a duplicate of issue 9698 ***