A Vision on Improved DMD Template Instantiation Diagonostics

Baz via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 12 09:37:50 PDT 2015


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.


More information about the Digitalmars-d mailing list