Issue 9715 - Implement basic template error checking
Summary: Implement basic template error checking
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-14 02:18 UTC by Don
Modified: 2023-03-27 13:51 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 Don 2013-03-14 02:18:34 UTC
int foo(T)(T x, int yesterday)
{
    return yesteray;
}
----
There is a typo in this template, it will fail for any type T. Would be nice if this reported an error, without having to instantiate the template.

We could do this by creating a new Unspecified type and UnspecifiedExp in the compiler. Whenever a template declaration is encountered, instantiate it with Unspecified types. These guys would propagate exactly like ErrorExp.

Then, we'd get error messages for the simple cases like those shown above.

You could actually do 'static if' the same way (types declared in is() expressions inside static if will be Unspecified type if they depend on template parameters), but the challenge is when you have multiple independent static ifs in a template:

static if (T.sizeof == 2)  int x = 3;
static if (T.sizeof > 2)  int x = 4;  // this isn't a redefinition error

There are ways to deal with this, eg treating the inside of the static if as a scope, and when the static if has been analyzed, moving everything declared in that scope into the parent scope, marking it as PossiblyDeclared.
(so that redefining it doesn't cause an error, but it's OK to use it from inside another static if).

The simpler option would be to stop analysis after the first 'static if' is encountered. That would still catch errors in a lot of useful cases.
Comment 1 Walter Bright 2013-03-14 13:04:34 UTC
It's a good idea, but the pervasiveness of ErrorExp is still pretty incomplete.
Comment 2 Walter Bright 2013-03-14 14:34:05 UTC
Another issue is what happens with value and alias parameters. A fair amount of reengineering of the internals would be necessary to make this work.
Comment 3 RazvanN 2023-03-27 13:51:43 UTC
Yes, this would require a major redesign of how the template system works for a rather small benefit.