Proposal for tagged_by attribute to describe union discriminators
Proposes a GCC attribute to explicitly link enum discriminators to union members for improved static analysis.
Ashton Warner proposes a new GCC attribute tagged_by to associate enum discriminators with specific union members. This attribute would provide additional information for static analysis tools, enabling them to issue diagnostics when a union is accessed with an incorrect member for its current discriminator value. The attribute is explicit and does not alter runtime behavior.
- proposer
Proposes a new `tagged_by` GCC attribute to explicitly describe tagged unions by mapping enum discriminators to union members. The goal is to enhance static analysis and diagnostics without affecting runtime behavior.
“I propose the GCC attribute __attribute__((tagged_by(...))) with the following syntax: tagged_by(discriminator, mapping-list) where: - discriminator is an identifier - mapping-list is a comma-separated list of one or more mappings. - Each mapping has the form: (enumerator, union-member) For example: struct number { enum num_type type; union { int ival; float fval; } __attribute__((…”
- reviewer
Asks for clarification on the implications of the proposed attribute, specifically regarding potential changes to the C standard or compiler behavior.
“Am Donnerstag, dem 16.07.2026 um 09:17 +1200 schrieb Ashton Warner via Gcc: > Hello, > > I would like to discuss the possibility of adding a GCC attribute for > describing tagged unions (discriminated unions) to improve static > analysis diagnostics.”
Technical Tradeoffs
- Adds a new language extension for improved type safety.
- Requires front-end changes to parse and interpret the attribute.
- No runtime performance impact.
In Details
This proposal introduces a new GCC extension attribute, tagged_by, for tagged unions. It aims to provide explicit semantic information for analyzing the relationship between an enum discriminator and union members, enabling improved compiler diagnostics for type safety. The attribute would reside within the C front-end, processed during semantic analysis.