Skip to content

Commit defc5d8

Browse files
authored
Merge pull request #3150 from ntrel/auto-main
[spec] `main` can be declared with `auto` or `noreturn` Signed-off-by: Nicholas Wilson <thewilsonator@users.noreply.github.com> Merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 3eaea19 + 4663861 commit defc5d8

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

spec/function.dd

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,19 +2955,38 @@ $(H3 $(LNAME2 anonymous, Anonymous Functions and Anonymous Delegates))
29552955
$(H2 $(LNAME2 main, $(D main()) Function))
29562956

29572957
$(P For console programs, $(D main()) serves as the entry point.
2958-
It gets called after all the module initializers are run, and
2959-
after any unittests are run.
2958+
It gets called after all the $(DDSUBLINK spec/module, staticorder, module initializers)
2959+
are run, and after any $(DDLINK spec/unittest, Unit Tests, unittests) are run.
29602960
After it returns, all the module destructors are run.
2961-
$(D main()) must be declared using one of the following forms:
2961+
$(D main()) must be declared as follows:
2962+
)
2963+
2964+
$(GRAMMAR
2965+
$(GNAME MainFunction):
2966+
$(GLINK MainReturnDecl) $(D main$(LPAREN)$(RPAREN)) $(GLINK2 statement, BlockStatement)
2967+
$(GLINK MainReturnDecl) $(D main$(LPAREN)string[]) $(GLINK_LEX Identifier)$(D $(RPAREN)) $(GLINK2 statement, BlockStatement)
2968+
2969+
$(GNAME MainReturnDecl):
2970+
$(D void)
2971+
$(D int)
2972+
$(GLINK2 type, noreturn)
2973+
$(RELATIVE_LINK2 auto-functions, $(D auto))
29622974
)
29632975

29642976
$(UL
2965-
$(LI `void main() { ... }`)
2966-
$(LI `void main(string[] args) { ... }`)
2967-
$(LI `int main() { ... }`)
2968-
$(LI `int main(string[] args) { ... }`)
2977+
$(LI If `main` returns `void`, the OS will receive a zero value on success.)
2978+
$(LI If `main` returns `void` or `noreturn`, the OS will receive a non-zero
2979+
value on abnormal termination, such as an uncaught exception.)
2980+
$(LI If `main` is declared as `auto`, the inferred return type must be
2981+
one of `void`, `int` and `noreturn`.)
29692982
)
29702983

2984+
$(P If the $(D string[]) parameter is declared, the parameter will hold
2985+
arguments passed to the program by the OS. The first argument is typically
2986+
the executable name, followed by any command-line arguments.)
2987+
2988+
$(NOTE The runtime can remove any arguments prefixed `--DRT-`.)
2989+
29712990
$(P The main function must have D linkage.)
29722991

29732992
$(P Attributes may be added as needed, e.g. `@safe`, `@nogc`, `nothrow`, etc.)

0 commit comments

Comments
 (0)