# stage2: implement switching on unions · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/dfb3231959bb340d260ddbec2b8eabfb5063c1bf)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: dfb3231959bb340d260ddbec2b8eabfb5063c1bf

Requested commit: dfb3231959bb340d260ddbec2b8eabfb5063c1bf

Commit: dfb3231959bb340d260ddbec2b8eabfb5063c1bf

Tree: 1f91b36fc9ed4b0aaa35fd8f8657efb365f3ec74

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: implement switching on unions

 * AstGen: Move `refToIndex` and `indexToRef` to Zir
 * ZIR: the switch_block_*_* instruction tags are collapsed into one
   switch_block tag which uses 4 bits for flags, and reduces the
   scalar_cases_len field from 32 to 28 bits.
   This freed up more ZIR tags, 2 of which are now used for
   `switch_cond` and `switch_cond_ref` for producing the switch
   condition value. For example, for union values it returns the
   corresponding enum value.
 * switching with multiple cases and ranges is not yet supported because
   I want to change the ZIR encoding to store index pointers into the
   extra array rather than storing prong indexes. This will avoid O(N^2)
   iteration over prongs.
 * AstGen now adds a `switch_cond` on the operand and then passes the
   result of that to the `switch_block` instruction.
 * Sema: partially implement `switch_capture_*` instructions.
 * Sema: `unionToTag` notices if the enum type has only one possible value.

```

## Parents

- [4a76523b92bcf0e9b48438cb22f49e67e0ab3fa1](https://git.cafe/gitcafe/zig/commit/4a76523b92bcf0e9b48438cb22f49e67e0ab3fa1?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/dfb3231959bb340d260ddbec2b8eabfb5063c1bf?format=markdown)
