On Thu, Sep 05, 2013 at 10:14:01AM -0700, Timothee Cour wrote: > Currently D will compile templated code that is syntactically correct > but semantically always incorrect (ie regardless of template > parameters), eg the following: > regardless of T, b is not in scope and hence this template cannot be > instantiated without CT error. > > So would it be possible to detect such kind of errors (ie CT error > regardless of template params) without having to instantiate the > template? Sure this could be detected with unittests in a perfect > world but that gives an additional level of safety. > > Likewise with template constraints, where the code wouldn't be able to > compile given template constraints, but this case is harder. > > ---- > void fun(T)(){ > int a=b; > } > > void main(){ > //fun!double; //uncomment for CT error > } > ----
I don't know enough about DMD internals to be able to say for sure, but at least in theory, this should be doable by checking if each identifier either refers to a template parameter (e.g., T.a, T.b, in which case we let it pass, since we wouldn't know until instantiation time whether or not that reference will actually succeed), or to an identifier in the containing scopes (which may be themselves template parameters). If it refers to neither, reject it as invalid. The idea being, given some identifier x.y in the template body, x.y must either be defined either in the global scope, in the containing lexical scope, or x is a template parameter and y refers to an as-yet unknown member of that parameter. If none of this holds, then no matter what template parameters are given, the template can't possibly compile. I'd say file an enhancement request for this. T -- Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn