# Sema: resolve union payload switch captures with peer type resolution · gitcafe/zig

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

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

Visibility: public

Requested revision: bcb673d94ac09ec381e5b1ad1edf64b603ac68f1

Requested commit: bcb673d94ac09ec381e5b1ad1edf64b603ac68f1

Commit: bcb673d94ac09ec381e5b1ad1edf64b603ac68f1

Tree: 7624448f2c157b81b003935583deeaae5ec264df

Author: mlugg

Committer: mlugg

## Message

```
Sema: resolve union payload switch captures with peer type resolution

This is a bit harder than it seems at first glance. Actually resolving
the type is the easy part: the interesting thing is actually getting the
capture value. We split this into three cases:

* If all payload types are the same (as is required in status quo), we
  can just do what we already do: get the first field value.
* If all payloads are in-memory coercible to the resolved type, we still
  fetch the first field, but we also emit a `bitcast` to convert to the
  resolved type.
* Otherwise, we need to handle each case separately. We emit a nested
  `switch_br` which, for each possible case, gets the corresponding
  union field, and coerces it to the resolved type. As an optimization,
  the inner switch's 'else' prong is used for any peer which is
  in-memory coercible to the target type, and the bitcast approach
  described above is used.

Pointer captures have the additional constraint that all payload types
must be in-memory coercible to the resolved type.

Resolves: #2812

```

## Parents

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

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