www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4970] New: Failed template instantiations need to propogate

http://d.puremagic.com/issues/show_bug.cgi?id=4970

           Summary: Failed template instantiations need to propogate
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg gmx.com> 2010-10-01 22:50:15
PDT ---
Take this program:

import std.conv;
import std.stdio;

void main()
{
    dchar[7] numStr = "1234567";

    writeln(to!long(numStr));
}


It fails to compile, giving this error:

/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/conv.d(95):
Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) &&
isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S)))
toImpl(T,S) if (!implicitlyConverts!(S,T) && isSomeString!(T) &&
isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) matches more than
one template declaration,
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/conv.d(217):toImpl(T,S)
if (isStaticArray!(S)) and
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/conv.d(588):toImpl(T,S)
if ((is(S : const(wchar)[]) || is(S : const(dchar)[])) && !isSomeString!(T))


From the looks of it, the template instantiation has failed due to failing a
template constraint. Looking at std.conv, it looks like the problem is that the
template constraint for toImpl!() fails inside of the template to!(). The error
is given for the instantiation point of toImpl!() inside of to!(). However,
where it needs to be in order to be useful is inside of main(). Granted, adding
a template constraint to to!() would put the error in the correct place, but
really, ideally, dmd would list each of the template instatiations which is
failing here.

Given the code in main(), toImpl!() fails. Listing that is fine. But that means
that to!() fails and the compiler doesn't say a thing about that. It gives the
line in to!(), but you have no way of knowing what code was trying to
instantiate to!(), so you have no idea where the error is. If the error were
simply inside a function, then it's understandable that the error would be
there rather than in the caller, but it's a template function, so the error is
likely in the caller, not the function itself - and in this case, it is.

I would expect that the compiler would be able to realize that it's the middle
of instantiating a chain of templates and thus be able to report an error for
each of them, indicating the instantation failure rather than just the last
one, which buried in code that isn't being called or instantiated directly by
the programmer, which is a pretty useless error all things considered.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 01 2010