Bug 113011 - main declared with enumerated type is not accepted
Summary: main declared with enumerated type is not accepted
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-14 01:46 UTC by Halalaluyafail3
Modified: 2023-12-14 02:04 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Halalaluyafail3 2023-12-14 01:46:38 UTC
The following code is rejected in GCC with pedantic mode enabled:

enum E{e=-1};
_Static_assert(
    _Generic((enum E)e,int:1,default:0),
    "incorrect enumeration type"
);
enum E main(){}

If the static assertion succeeds, then the type enum E is compatible with int. Given that enum E is compatible with int, it should be a valid return type of main:

> If the return type of the main function is a type compatible with int,
> a return from the initial call to the main function is equivalent to
> calling the exit function with the value returned by the main function
> as its argument; reaching the } that terminates the main function returns
> a value of 0. If the return type is not compatible with int, the termination
> status returned to the host environment is unspecified.
Section 5.1.2.2.3 "Program termination" Paragraph 1 ISO/IEC 9899:2018

Additionally, here is the paragraph explaining that an enumerated type is compatible with its underlying type:

> Each enumerated type shall be compatible with char, a signed integer type,
> or an unsigned integer type. The choice of type is implementation-defined,
> but shall be capable of representing the values of all the members of the
> enumeration.
Section 6.7.2.2 "Enumeration specifiers" Paragraph 4 ISO/IEC 9899:2018

Outside of pedantic mode, GCC accepts the code but returns a non-zero value when the closing brace is reached which isn't correct either.
Comment 1 Andrew Pinski 2023-12-14 01:54:25 UTC
clang even errors out:
<source>:6:1: error: 'main' must return 'int'
    6 | enum E main(){}
      | ^~~~~~
      | int
Comment 2 Andrew Pinski 2023-12-14 02:02:09 UTC
From C99 draft: 5.1.2.2.1 Program startup
The function called at program startup is named main.The implementation declares no prototype for this function. It shall be defined with a return type ofintand with no parameters
...
or with two parameters (referred to here asargcandargv, though anynames may be used, as theyare local to the function in which theyare declared):
Comment 3 Andrew Pinski 2023-12-14 02:04:26 UTC
C23 working draft has the same wording there too:
The function called at program startup is named main.The implementation declares no prototype for this function. It shallbe defined with a return type of int

Nowhere it says compatiable with int. just the return type of int.
Comment 4 Andrew Pinski 2023-12-14 02:04:51 UTC
C23 working draft can be found at https://open-std.org/JTC1/SC22/WG14/www/docs/n3096.pdf .