Issue 91257
Summary [Clang] CodeGen issues with C++20 bitfield initializers in anonymous structs/unions
Labels clang
Assignees
Reporter a2flo
    Hi,

I noticed some issues when using C++20 bitfield member initialization inside nested anonymous structs/unions. I'm not entirely sure how much of this is undefined (or intended) behavior, but it still seems to me that clangs behavior isn't right here.

Given the following code, this returns an uninitialized value from main() (compiles down to just a `ret` after optimization) and when using clang with assertions enabled, this triggers the `(Field->isUnnamedBitfield() || Field->isAnonymousStructOrUnion()) && "Only unnamed bitfields or ananymous class allowed"` assert in [CGExprAgg.cpp](https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGExprAgg.cpp#L1737). Also, ananymous should be anonymous.
```
struct nested_anon_bitfield_init_t {
    union {
        struct {
 int inner : 32 { 42 };
        };
        int outer;
 };
};

int main() {
    return nested_anon_bitfield_init_t{}.inner;
}
```
[on compiler-explorer](https://compiler-explorer.com/z/WoqvMdGK9)
Tested on current trunk and 18.1.
Note that GCC returns 0 here.

__

Similarly, when using less nesting, this no longer triggers the assertion, but still returns an uninitialized value:
```
union less_nested_anon_bitfield_init_t {
    struct {
 int inner : 32 { 42 };
    };
};

int main() {
 return less_nested_anon_bitfield_init_t{}.inner;
}
```
[on compiler-explorer](https://compiler-explorer.com/z/e188a3dPP)
Again, GCC returns 0 here.

Intuitively, both should return 42 of course.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to