www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A Vision on Improved DMD Template Instantiation Diagonostics

reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
After having seen Andrei's & Walter's talks on DConf 2015 it's 
time reveal a dream of mine. It resolves around of feature that I 
believe is one of the most important improvements that will 
benefit aggregation of more *new* users to the D Community.

Namely a more clever DMD diagnostics when a template instantion 
fails to match any template definition in the current scope.

For instance, if

     f(a,b,c,d)

fails to match any of the overloads

     f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
     f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))

instead of saying

     > Error: neither of these matched
     > - f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
     > - f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))

it should instead say

     > Error: no template match to call of
     > f(a,b,c,d)
     > for neither

     > - f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
                                       -------------
     >   because template restriction `PRED_2(a,b,c)` evaluate to 
false

     > - f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))
                                     ---------
     >   because template restriction `PRED(b,c)` evaluated to 
false

Eventhough this might be a bit tricky to get right and may break 
lots of existing diagnostics (in DMD unittests), I'd say it's 
definitiely worth effort. Such a feature would attract lots of 
new users not used to D's advanced template restrictions. All 
users, newbies aswell as experts, would be benefit from this 
feature. I you DMD review guys are interested in getting this in 
and helping me out on source code directions I'd be very happy to 
start working on DMD PR for this.

Destroy.
Jun 12 2015
next sibling parent reply "Xinok" <xinok live.com> writes:
On Friday, 12 June 2015 at 12:20:58 UTC, Per Nordlöw wrote:
 ...

 Destroy.
Improving compiler diagnostics for failed template instantiations would be a great start. However, template constraints in D are much more powerful and flexible than C++ concepts. As such, there are bound to be several cases in which the compiler can't provide any useful information, no matter how "smart" it may be. In general, I think it needs to be easier for us programmers to produce our own error messages specifying why the template failed to instantiate. One trick that I use, which others have mentioned in other threads, is to use static asserts to find the exact condition that failed and print an informative error message. Another idea I've mentioned before is to make it easy to define a "default" instance when none of the template constraints are satisfied. This would be a good place to insert some diagnostics to determine why the template instantiation failed and provide a meaningful error message. A crude example: int foo(T)(T arg) if(...){ } int foo(T)(T arg) if(...){ } int foo(T)(T arg) default // Used when all other instances fail { // Insert diagnostics here static assert(isRandomAccessRange!T, "..."); ... }
Jun 12 2015
parent reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Friday, 12 June 2015 at 15:35:55 UTC, Xinok wrote:
 However, template constraints in D are much more powerful and 
 flexible than C++ concepts. As such, there are bound to be 
 several cases in which the compiler can't provide any useful 
 information, no matter how "smart" it may be.
Can you give an example of such a case where the compiler isn't smart enough?
Jun 12 2015
parent "Xinok" <xinok live.com> writes:
On Friday, 12 June 2015 at 15:51:02 UTC, Per Nordlöw wrote:
 On Friday, 12 June 2015 at 15:35:55 UTC, Xinok wrote:
 However, template constraints in D are much more powerful and 
 flexible than C++ concepts. As such, there are bound to be 
 several cases in which the compiler can't provide any useful 
 information, no matter how "smart" it may be.
Can you give an example of such a case where the compiler isn't smart enough?
The satisfiability problem, i.e. it may be there is no set of inputs that satisfies the template constraints. https://en.wikipedia.org/wiki/Boolean_satisfiability_problem
Jun 12 2015
prev sibling next sibling parent reply "Baz" <bb.temp gmx.com> writes:
On Friday, 12 June 2015 at 12:20:58 UTC, Per Nordlöw wrote:
 After having seen Andrei's & Walter's talks on DConf 2015 it's 
 time reveal a dream of mine. It resolves around of feature that 
 I believe is one of the most important improvements that will 
 benefit aggregation of more *new* users to the D Community.

 Namely a more clever DMD diagnostics when a template instantion 
 fails to match any template definition in the current scope.

 For instance, if

     f(a,b,c,d)

 fails to match any of the overloads

     f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
     f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))

 instead of saying

     > Error: neither of these matched
     > - f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
     > - f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))

 it should instead say

     > Error: no template match to call of
     > f(a,b,c,d)
     > for neither

     > - f(a,b,c) if (PRED_1(a,b,c) && PRED_2(a,b,c))
                                       -------------
     >   because template restriction `PRED_2(a,b,c)` evaluate 
 to false

     > - f(a,b,c,d) if (PRED(a,b) && PRED(b,c) && PRED(c,d))
                                     ---------
     >   because template restriction `PRED(b,c)` evaluated to 
 false

 Eventhough this might be a bit tricky to get right and may 
 break lots of existing diagnostics (in DMD unittests), I'd say 
 it's definitiely worth effort. Such a feature would attract 
 lots of new users not used to D's advanced template 
 restrictions. All users, newbies aswell as experts, would be 
 benefit from this feature. I you DMD review guys are interested 
 in getting this in and helping me out on source code directions 
 I'd be very happy to start working on DMD PR for this.

 Destroy.
This can be done yet, manually, at the library level. For example if every check in a constraint is warped like this: --- bool CheckAndSay(alias Fun, string diagnostic = "")() { enum result = Fun(); static if (!result && diagnostic.length) pragma(msg, diagnostic); return result; } --- Obviously, dmd still outputs the whole constraint in an error message, but at the top of the error you have a clear message: --- bool IsType(T,S)() { return (is(T == S)); } void bar(T1, T2)() if ( CheckAndSay!(IsType!(float,T1), "constraint error, expected float as T1 type") && CheckAndSay!(IsType!(int,T2), "constraint error, expected int as T2 type") ) {} void main(string[] args) { bar!(byte,int)(); } --- [+] compiler change not required. [-] lot of things to rewrite in phobos. However it would be certainly a great gain if DMD could be more clever on constraint failure.
Jun 12 2015
parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 12 June 2015 at 16:37:51 UTC, Baz wrote:
 This can be done yet, manually, at the library level. For 
 example if every check in a constraint is warped like this:
Won't this also trigger during overload resolution?
Jun 12 2015
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/12/15 5:20 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?= 
<per.nordlow gmail.com>" wrote:
 After having seen Andrei's & Walter's talks on DConf 2015 it's time
 reveal a dream of mine. It resolves around of feature that I believe is
 one of the most important improvements that will benefit aggregation of
 more *new* users to the D Community.
Yah, Walter and I were talking about it a short while ago. We figured conjunctions, disjunctions, and parentheses would be enough to decompose. The same applies to static assert and assert. Perhaps a good next step would be a principled DIP. Andrei
Jun 12 2015
parent "Atila Neves" <atila.neves gmail.com> writes:
On Friday, 12 June 2015 at 17:13:51 UTC, Andrei Alexandrescu 
wrote:
 On 6/12/15 5:20 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?= 
 <per.nordlow gmail.com>" wrote:
 After having seen Andrei's & Walter's talks on DConf 2015 it's 
 time
 reveal a dream of mine. It resolves around of feature that I 
 believe is
 one of the most important improvements that will benefit 
 aggregation of
 more *new* users to the D Community.
Yah, Walter and I were talking about it a short while ago. We figured conjunctions, disjunctions, and parentheses would be enough to decompose. The same applies to static assert and assert. Perhaps a good next step would be a principled DIP.
That's the DIP I wanted to work on. Still want to actually, just haven't had the time yet. Atila
Jun 13 2015