finding errors with templates without instantiating them

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Sep 5 10:48:34 PDT 2013


On Thu, Sep 05, 2013 at 07:41:14PM +0200, Andrej Mitrovic wrote:
> On 9/5/13, Timothee Cour <thelastmammoth at gmail.com> wrote:
> > So would it be possible to detect such kind of errors (ie CT error
> > regardless of template params) without having to instantiate the template?
> 
> How would you semantically analyze the following without instantiating it?:
> 
> -----
> template T(X)
> {
>     enum T = X.foo;
> }
> -----
> 
> Note how the above can be both valid and invalid based on the template
> parameters:

In this case, we temporarily accept it until instantiation time.


[...]
> There's so much context-dependent semantics in a template that eager
> semantic analysis of templates which haven't been instantiated would
> be limited to work for only very simple templates. So I don't think it
> would be worth even trying to analyze without instantiating.

It's actually not that hard. Consider if the template body refers to
identifier X, but X is neither a template parameter, nor is X defined in
any of the containing scopes of the template. Then no matter how you
instantiate the template, it can't possibly work. So it can be rejected
before instantiation is even attempted.


> Also, I think it would likely be extremely hard to implement in the
> compiler, and could possible lead to false-positives (compiles) or
> false-negatives (doesn't compile) cases.
> 
> And finally, compile times would literally explode with eager semantic
> analysis.

My proposal requires only lookup of identifiers in the containing
lexical scopes and a trivial check for whether it refers to a template
parameter (or a member thereof). It's a one-pass algorithm. It can't be
*that* bad.


T

-- 
My program has no bugs! Only undocumented features...


More information about the Digitalmars-d mailing list