# compiler: change representation of closures · gitcafe/zig

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

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

Visibility: public

Requested revision: a6ca20b9a1dfc7b6e8d004cb166c0714bb8db2db

Requested commit: a6ca20b9a1dfc7b6e8d004cb166c0714bb8db2db

Commit: a6ca20b9a1dfc7b6e8d004cb166c0714bb8db2db

Tree: 8ecc7aae4a07f9e5a2fe1d2f2acfe0f40f5ea673

Author: mlugg

Committer: mlugg

## Message

```
compiler: change representation of closures

This changes the representation of closures in Zir and Sema. Rather than
a pair of instructions `closure_capture` and `closure_get`, the system
now works as follows:

* Each ZIR type declaration (`struct_decl` etc) contains a list of
  captures in the form of ZIR indices (or, for efficiency, direct
  references to parent captures). This is an ordered list; indexes into
  it are used to refer to captured values.
* The `extended(closure_get)` ZIR instruction refers to a value in this
  list via a 16-bit index (limiting this index to 16 bits allows us to
  store this in `extended`).
* `Module.Namespace` has a new field `captures` which contains the list
  of values captured in a given namespace. This is initialized based on
  the ZIR capture list whenever a type declaration is analyzed.

This change eliminates `CaptureScope` from semantic analysis, which is a
nice simplification; but the main motivation here is that this change is
a prerequisite for #18816.

```

## Parents

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

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