# Sema: refactor detection of comptime-known consts · gitcafe/zig

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

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

Visibility: public

Requested revision: 644041b3a492558592e1306d2214c9e6b25de53b

Requested commit: 644041b3a492558592e1306d2214c9e6b25de53b

Commit: 644041b3a492558592e1306d2214c9e6b25de53b

Tree: cfddcc1e038f94a877181140c93912342a7239ad

Author: mlugg

Committer: Matthew Lugg

## Message

```
Sema: refactor detection of comptime-known consts

This was previously implemented by analyzing the AIR prior to the ZIR
`make_ptr_const` instruction. This solution was highly delicate, and in
particular broke down whenever there was a second `alloc` between the
`store` and `alloc` instructions, which is especially common in
destructure statements.

Sema now uses a different strategy to detect whether a `const` is
comptime-known. When the `alloc` is created, Sema begins tracking all
pointers and stores which refer to that allocation in temporary local
state. If any store is not comptime-known or has a higher runtime index
than the allocation, the allocation is marked as being runtime-known.
When we reach the `make_ptr_const` instruction, if the allocation is not
marked as runtime-known, it must be comptime-known. Sema will use the
set of `store` instructions to re-initialize the value in comptime
memory. We optimize for the common case of a single `store` instruction
by not creating a comptime alloc in this case, instead directly plucking
the result value from the instruction.

Resolves: #16083

```

## Parents

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

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