# stage2: detection of comptime array literals · gitcafe/zig

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

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

Visibility: public

Requested revision: 336d0c97feabad4c93525ba6ef73a6b6163f49c7

Requested commit: 336d0c97feabad4c93525ba6ef73a6b6163f49c7

Commit: 336d0c97feabad4c93525ba6ef73a6b6163f49c7

Tree: 0cf55d52805897bc9ce255c903bd9dc18043973f

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: detection of comptime array literals

Introduce `validate_array_init_comptime`, similar to
`validate_struct_init_comptime` introduced in
713d2a9b3883942491b40738245232680877cc66.

`zirValidateArrayInit` is improved to detect comptime array literals and
emit AIR accordingly. This code is very similar to the changes
introduced in that same commit for `zirValidateStructInit`.

The C backend needed some improvements to continue passing the same set
of tests:
 * `resolveInst` for arrays now will add a local `static const` with the
   array value and so then `elem_val` instructions reference that local.
   It memoizes accesses using `value_map`, which is changed to use
   `Air.Inst.Ref` as the key rather than `Air.Inst.Index`.
 * This required a mechanism for writing to a "header" which is lines
   that appear at the beginning of a function body, before everything
   else.
 * dbg_stmt output comments rather than `#line` directives.
   TODO comment reproduced here:

We need to re-evaluate whether to emit these or not. If we naively emit
these directives, the output file will report bogus line numbers because
every newline after the #line directive adds one to the line.
We also don't print the filename yet, so the output is strictly unhelpful.
If we wanted to go this route, we would need to go all the way and not output
newlines until the next dbg_stmt occurs.
Perhaps an additional compilation option is in order?

`Value.elemValue` is improved to support `elem_ptr` values.

```

## Parents

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

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