Bug 4970 – Failed template instantiations need to propogate

Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-10-01T22:50:15Z
Last change time
2022-02-25T10:36:03Z
Assigned to
No Owner
Creator
Jonathan M Davis

Comments

Comment #0 by issues.dlang — 2010-10-01T22:50:15Z
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.
Comment #1 by dmitry.olsh — 2018-05-18T09:23:25Z
This now works and prints 1234567. Any better example Jonathan and more specific about actionable intent?
Comment #2 by razvan.nitu1305 — 2022-02-25T10:36:03Z
Closing this as the bug report is no longer valid and diagnostics for failed tempalte instantiations has been improved.