The following complete program succeed when run with no arguments.
What it should do is fail on the second assert.
extern(C) void main(string[] args) {
import core.stdc.stdio: printf;
assert(args.length == 1);
assert(args[0].ptr == null);
}
When 'extern(C)' is removed, this code fails as args[0] contains the
path used to invoke the program, as provided by the OS.
I noticed this when trying to write an extremely simple C program in D,
with -betterC, where I spent more than 20 minutes being perplexed by
'impossible' NULL dereferences. The behavior comes with the extern(C)
however, and doesn't require a -betterC flag. This behavior is observed
with LDC 1.15.0 and DMD64 D Compiler v2.086.1, and with the nightly DMD
build of v2.089.0-rc.1-master-2bbd37b
extern(C) void main(int argc, char **argv) works fine, but I argue if
string[] args isn't supported, compilation should fail with an error
instead of providing the current behavior.
Comment #1 by elronnd — 2019-11-10T05:15:58Z
I don't think straight up erroring is a good idea. extern(C) void main(string[]) is perfectly *valid*; I don't think the language says anything special about the type of extern(C) main.
However I do agree that, with the current behaviour, errors such as yours are likely to occur. Proposal: make it a warning with a 'did you mean (int argc, char **argv)?' which also notes that if you really do want that behaviour you can silence the warning with pragma(mangle, "main") void cmain(string[])
Comment #2 by alphaglosined — 2019-11-10T05:30:01Z
(In reply to elronnd from comment #1)
> I don't think straight up erroring is a good idea. extern(C) void
> main(string[]) is perfectly *valid*; I don't think the language says
> anything special about the type of extern(C) main.
The spec says nothing about extern(C) main at all.
https://dlang.org/spec/function.html#main
As far as its concerned it should be invalid.
https://issues.dlang.org/show_bug.cgi?id=20378