[clang] [Clang] Avoid volatile copy for inactive union init (PR #239870)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 12:12:59 PST 2026


efriedma-quic wrote:

A compound literal is an lvalue.  So assignment is defined as performing an lvalue conversion on that lvalue, then storing it to the destination.  If the compound literal is volatile, skipping that conversions is clearly not legal.

The quirk here is that the union itself is not volatile, only the member is.  Technically, looking at the C standard, I don't see any requirement that we treat an access to a struct or union as a volatile access if the struct or union itself isn't volatile.  It looks like gcc disagrees with clang here: it ignores the volatile marking.  Probably worth opening an defect report.  CC @AaronBallman 

Looking at the initializer for the compound literal is intuitively appealing, but I don't think there's any support for that in the standard.

(For C++, [class.copy.ctor] and [class.copy.assign] define the implicit copy constructor as member-wise copy for structs, so volatile markings on members naturally apply, I think?  For unions, I don't think the current wording requires a volatile copy.)

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


More information about the cfe-commits mailing list