[clang] [Clang][Sema]: Allow flexible arrays in unions and alone in structs (PR #84428)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 13 10:09:57 PDT 2024


AaronBallman wrote:

Separating out some bits of discussion here.

Standardization of flexible arrays in unions
-------------------------------------------
We can dispense with this topic quickly: the standard doesn't allow this, if the standard should be relaxed then someone needs to write a paper to propose it. I can help shepherd the proposal through WG14, but I don't think I have the time or the motivation to write the paper myself.

However, what the standard says doesn't matter too much for our discussion because it's conforming to support this as an extension.

Supporting flexible arrays in unions as an extension
----------------------------------------------------
Personally, I find flexible arrays to be a pretty weird thing to support in a union. Unions have a set of members that all overlap, but flexible array members are not really members as such, they're a name used to refer to trailing storage after the end of the object and don't contribute to the object directly (e.g., don't matter for `sizeof`). However, because we support zero-sized unions as an extension (https://godbolt.org/z/GG3odEhev), and we support zero-sized arrays as an extension (https://godbolt.org/z/q74EoGTWf), including in unions (https://godbolt.org/z/EhvTTb9os)... I don't see a compelling reason to disallow using flexible array syntax in unions as they're morally equivalent. And the same logic applies to structures where the sole member is a flexible array (https://godbolt.org/z/KxsPvqo3v).

https://github.com/llvm/llvm-project/pull/84428


More information about the cfe-commits mailing list