D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20377 - extern(C) void main(string[] args) has invalid 'args'
Summary: extern(C) void main(string[] args) has invalid 'args'
Status: RESOLVED DUPLICATE of issue 22831
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-11-10 04:27 UTC by mipri
Modified: 2022-03-29 18:34 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mipri 2019-11-10 04:27:10 UTC
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 elronnd 2019-11-10 05:15:58 UTC
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 Richard (Rikki) Andrew Cattermole 2019-11-10 05:30:01 UTC
(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
Comment 3 duser 2022-03-29 17:46:29 UTC
this has been fixed by https://github.com/dlang/dmd/pull/13749
Comment 4 moonlightsentinel 2022-03-29 18:34:01 UTC

*** This issue has been marked as a duplicate of issue 22831 ***