Issue 202387
Summary Incorrect __builtin_offsetof behavior of enum constants used as index
Labels new issue
Assignees
Reporter olson-dan
    This simple, reduced program fails:
```cpp
#include <stddef.h>
#include <stdint.h>

enum E : uint8_t { X = 127, Y = 229 };

struct S
{
    int arr[256];
};

static_assert(
    offsetof(S, arr[X]) ==
    offsetof(S, arr[127]));
static_assert(
    offsetof(S, arr[Y]) ==
    offsetof(S, arr[229]));
```

godbolt here: https://gcc.godbolt.org/z/P4ea35TKW against ("clang trunk") which has this version output:

```
clang version 23.0.0git (https://github.com/llvm/llvm-project.git 47ef7495ad781b742a0c4435ca72590d297ba8bf)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /cefs/9c/9c7999660da1c2af18e75609_clang-trunk-20260608/bin
Internal compiler ID: clang_trunk
```

Internally we saw this on a version 19 clang.

The compiler's output (from goldbolt) is 
```
<source>:15:5: error: static assertion failed due to requirement '__builtin_offsetof(S, arr[Y]) == __builtin_offsetof(S, arr[229])'
   15 |     offsetof(S, arr[Y]) ==
      | ^~~~~~~~~~~~~~~~~~~~~~
   16 |     offsetof(S, arr[229]));
      | ~~~~~~~~~~~~~~~~~~~~~
/cefs/9c/9c7999660da1c2af18e75609_clang-trunk-20260608/lib/clang/23/include/__stddef_offsetof.h:16:24: note: expanded from macro 'offsetof'
   16 | #define offsetof(t, d) __builtin_offsetof(t, d)
      |                        ^
<source>:15:25: note: _expression_ evaluates to '18446744073709551508 == 916'
   15 | offsetof(S, arr[Y]) ==
      |     ~~~~~~~~~~~~~~~~~~~~^~
   16 | offsetof(S, arr[229]));
      |     ~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Compiler returned: 1
```

it is treating the index as if it were an `int8_t` rather than a `uint8_t`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to