public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113804] New: offsetof(type, array[run_time_value]) failing
@ 2024-02-07 12:03 jlame646 at gmail dot com
  2024-02-07 12:27 ` [Bug c++/113804] " jakub at gcc dot gnu.org
  2024-02-07 15:06 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: jlame646 at gmail dot com @ 2024-02-07 12:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113804
           Summary: offsetof(type, array[run_time_value]) failing
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

The following code starts failing in gcc but is accepted by clang and msvc. 

```
#include <cstddef>

struct type {
    int array[4];
};

int main(int argc, char**) {
    return offsetof(type, array[argc]);
}
```

GCC says: 

```
In file included from
/opt/compiler-explorer/gcc-trunk-20240207/include/c++/14.0.1/cstddef:50,
                 from <source>:1:
<source>: In function 'int main(int, char**)':
<source>:8:33: error: 'argc' is not a constant expression
    8 |     return offsetof(type, array[argc]);
      |              
```

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

* [Bug c++/113804] offsetof(type, array[run_time_value]) failing
  2024-02-07 12:03 [Bug c++/113804] New: offsetof(type, array[run_time_value]) failing jlame646 at gmail dot com
@ 2024-02-07 12:27 ` jakub at gcc dot gnu.org
  2024-02-07 15:06 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-07 12:27 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |jason at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
struct S { int s[4]; };

int
foo (int x)
{
  return __builtin_offsetof (struct S, s[x]);
}
This is rejected since r11-557-gbeb019d346b903c16b9fd349937de444b6a8b6c0 in
C++, accepted in C.
This is I believe invalid in both C99 and C23 and C++ just inherits the C
definition and adds additional constraints to that:
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t, the value
of which is the offset in bytes, to the subobject (designated by
member-designator), from the beginning of any object of type type. The type and
member designator shall be such that given
static type t;
then the expression &(t. member-designator) evaluates to an address constant.
If the specified type defines a new type or if the specified member is a
bit-field, the behavior is undefined.

Now &(t. s[x]) definitely doesn't evaluate to address constant and offsetof
(struct S, s[x]) doesn't yield integer constant, so this is definitely outside
of the standards, accepted in C and before in C++ as well as an extension.

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

* [Bug c++/113804] offsetof(type, array[run_time_value]) failing
  2024-02-07 12:03 [Bug c++/113804] New: offsetof(type, array[run_time_value]) failing jlame646 at gmail dot com
  2024-02-07 12:27 ` [Bug c++/113804] " jakub at gcc dot gnu.org
@ 2024-02-07 15:06 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-07 15:06 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup.

*** This bug has been marked as a duplicate of bug 95942 ***

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

end of thread, other threads:[~2024-02-07 15:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-07 12:03 [Bug c++/113804] New: offsetof(type, array[run_time_value]) failing jlame646 at gmail dot com
2024-02-07 12:27 ` [Bug c++/113804] " jakub at gcc dot gnu.org
2024-02-07 15:06 ` pinskia at gcc dot gnu.org

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).