# stage2: LLVM backend: fix lowering of union constants · gitcafe/zig

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

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

Visibility: public

Requested revision: 4b9b9e725777a8f9e4ea9391beaeea34c834615f

Requested commit: 4b9b9e725777a8f9e4ea9391beaeea34c834615f

Commit: 4b9b9e725777a8f9e4ea9391beaeea34c834615f

Tree: f1cbd788134cdbcde7f90dc2b9ada91dcd9159ba

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: LLVM backend: fix lowering of union constants

Comment from this commit reproduced here:

LLVM does not allow us to change the type of globals. So we must
create a new global with the correct type, copy all its attributes,
and then update all references to point to the new global,
delete the original, and rename the new one to the old one's name.
This is necessary because LLVM does not support const bitcasting
a struct with padding bytes, which is needed to lower a const union value
to LLVM, when a field other than the most-aligned is active. Instead,
we must lower to an unnamed struct, and pointer cast at usage sites
of the global. Such an unnamed struct is the cause of the global type
mismatch, because we don't have the LLVM type until the *value* is created,
whereas the global needs to be created based on the type alone, because
lowering the value may reference the global as a pointer.

```

## Parents

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

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