Bug 17263 – Issue diagnostic if mandatory template parameter is missing
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2017-03-17T18:00:13Z
Last change time
2022-11-18T16:49:31Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Ali Cehreli
Comments
Comment #0 by acehreli — 2017-03-17T18:00:13Z
import std.algorithm : map;
void main() {
[1, 2].map(i => i);
}
Error: template std.algorithm.iteration.map cannot deduce function from argument types !()(int[], void), candidates are:
/home/ali/dmd2.073/linux/bin64/../../src/phobos/std/algorithm/iteration.d(456): std.algorithm.iteration.map(fun...) if (fun.length >= 1)
Makefile:142: recipe for target 'deneme' failed
The programmer forgot to put ! after map. It would be much better if the error message said something like
"Missing mandatory template arguments for std.algorithm.iteration.map(fun...)"
Ali
Comment #1 by issues.dlang — 2017-03-17T18:06:20Z
Yeah, this is one of those that's usually surprisingly hard to figure out. I usually end up doing a lot of examination of template constraints before I figure out that I simply forgot the !. It's a lot more obvious in this example, because it's short, but one you start chaining templates or having complicated predicates, it quickly becomes non-obvious that the ! is missing.
Comment #2 by musicaljelly — 2017-03-20T03:48:40Z
+1
I have made this mistake many times, and spent an embarrassingly long time trying to deduce the solution each time. A better compiler error would make the fix immediately obvious in these cases.
Comment #3 by issues.dlang — 2019-08-01T03:38:55Z
LOL. I just ran into this issue again, wasted several minutes trying to figure out what was wrong, finally figured it out, and then decided to open an enhancement request. But voila, there's already one sitting here, and I commented on it previously. Honestly, I'd guess that this is quite possibly the dmd error message that would provide the most benefit in being improved. Maybe it gets harder to diagnose correctly when there are multiple overloads and they don't all necessarily have the same number of template arguments, but it at least _seems_ like it should be straightforward to figure out that the problem is probably the lack of a ! and indicate as much to the programmer, even if it's just a suggestion rather than an indicator that that's definitely the problem.
Comment #4 by razvan.nitu1305 — 2022-11-18T10:54:18Z
Hi Ali,
Now the diagnostic is:
test2.d(4): Error: none of the overloads of template `std.algorithm.iteration.map` are callable using argument types `!()(int[], void)`
/home/razvann/Dlang/dmd/generated/linux/release/64/../../../../../phobos/std/algorithm/iteration.d(436): Candidate is: `map(fun...)`
with `fun = ()`
must satisfy the following constraint:
` fun.length >= 1`
I know it's not what you expected but isn't this better?
I am afraid that the actual error ("You forgot `!`") cannot be outputted because when the template is instantiated we are in the semantic phase where we no longer know whether the ! was present or not. In other words, we cannot know just by looking at the AST if the user provided template instance was `[1, 2].map(i => i);` or `[1, 2].map!()(i => i);`. So. I think that this is the best we could probably do in this case.
Comment #5 by acehreli — 2022-11-18T13:45:38Z
Thank you!
Comment #6 by razvan.nitu1305 — 2022-11-18T13:46:26Z
(In reply to Ali Cehreli from comment #5)
> Thank you!
You are welcome! OK to close this?
Comment #7 by acehreli — 2022-11-18T16:22:07Z
If you are asking me, the reporter, I am happy with your explanation why it can't be better. I wouldn't mind closing.