public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112556] New: Null pointer constants with enumeration type are not accepted
@ 2023-11-16  1:55 luigighiron at gmail dot com
  2023-11-16  5:15 ` [Bug c/112556] " luigighiron at gmail dot com
  2023-11-16 17:38 ` joseph at codesourcery dot com
  0 siblings, 2 replies; 3+ messages in thread
From: luigighiron at gmail dot com @ 2023-11-16  1:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112556

            Bug ID: 112556
           Summary: Null pointer constants with enumeration type are not
                    accepted
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luigighiron at gmail dot com
  Target Milestone: ---

The following code is not accepted in GCC:

void*p=(enum{E})E;

To make it clear why this code should be accepted, I have
provided some references to the current C standard.

> An integer constant expression with the value 0,
> or such an expression cast to type void *, is called
> a null pointer constant. If a null pointer constant is
> converted to a pointer type, the resulting pointer,
> called a null pointer, is guaranteed to compare unequal
> to a pointer to any object or function.
Section 6.3.2.3 "Pointers" Paragraph 1 ISO/IEC 9899:2018

> An integer constant expression shall have integer type
> and shall only have operands that are integer constants,
> enumeration constants, character constants, sizeof
> expressions whose results are integer constants,
> _Alignof expressions, and floating constants that are
> the immediate operands of casts. Cast operators in an
> integer constant expression shall only convert arithmetic
> types to integer types, except as part of an operand
> to the sizeof or _Alignof operator.
Section 6.6 "Constant expressions" Paragraph 6 ISO/IEC 9899:2018

> The type char, the signed and unsigned integer types,
> and the enumerated types are collectively called
> integer types. The integer and real floating types are
> collectively called real types.
> 
> Integer and floating types are collectively called
> arithmetic types. Each arithmetic type belongs to
> one type domain: the real type domain comprises the
> real types, the complex type domain comprises the
> complex types.
Section 6.2.5 "Types" Paragraphs 17 and 18 ISO/IEC 9899:2018

(enum{E})E is an integer constant expression because
E is an enumeration constant, and the cast is converting
an arithmetic type (int) to an integer type (the enumeration
type). This integer constant expression has the value zero,
so it is a null pointer constant.

This problem seems to be specific to null pointer constants,
for example the type int(*)[(enum{A,B})B] is not considered
to be a variable length array type (which is correct).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/112556] Null pointer constants with enumeration type are not accepted
  2023-11-16  1:55 [Bug c/112556] New: Null pointer constants with enumeration type are not accepted luigighiron at gmail dot com
@ 2023-11-16  5:15 ` luigighiron at gmail dot com
  2023-11-16 17:38 ` joseph at codesourcery dot com
  1 sibling, 0 replies; 3+ messages in thread
From: luigighiron at gmail dot com @ 2023-11-16  5:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112556

--- Comment #1 from Halalaluyafail3 <luigighiron at gmail dot com> ---
This bug also seems to happen with boolean types:

void*p=(_Bool)0;

Furthermore, this bug only seems to happen when the type
of the null pointer constant is an enumeration or boolean type:

void*p1=+(_Bool)0,*p2=+(enum{E})E;

No errors are generated for this code.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/112556] Null pointer constants with enumeration type are not accepted
  2023-11-16  1:55 [Bug c/112556] New: Null pointer constants with enumeration type are not accepted luigighiron at gmail dot com
  2023-11-16  5:15 ` [Bug c/112556] " luigighiron at gmail dot com
@ 2023-11-16 17:38 ` joseph at codesourcery dot com
  1 sibling, 0 replies; 3+ messages in thread
From: joseph at codesourcery dot com @ 2023-11-16 17:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112556

--- Comment #2 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
Yes, this is a bug; null_pointer_constant_p gets this right, but 
convert_for_assignment fails to handle enumerations and booleans as 
possible null pointer constants.  Other contexts such as comparisons and 
conditional expressions appear to be OK (through performing integer 
promotions so that enumerations and booleans can't appear, for example, or 
through handling all kinds of integer types together).

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-16 17:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-16  1:55 [Bug c/112556] New: Null pointer constants with enumeration type are not accepted luigighiron at gmail dot com
2023-11-16  5:15 ` [Bug c/112556] " luigighiron at gmail dot com
2023-11-16 17:38 ` joseph at codesourcery dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).