# stage2: implement slicing · gitcafe/zig

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

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

Visibility: public

Requested revision: a0e195120dd3dd7919fd249a6ce2201da9395898

Requested commit: a0e195120dd3dd7919fd249a6ce2201da9395898

Commit: a0e195120dd3dd7919fd249a6ce2201da9395898

Tree: 1f06851929602eb29932c60418e774f0cb1cab03

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: implement slicing

 * New AIR instruction: slice, which constructs a slice out of a pointer
   and a length.
 * AstGen: use `coerced_ty` for start and end expressions, use `none`
   for the sentinel, and don't try to load the result of the slice
   operation because it returns a by-value result.
 * Sema: pointer arithmetic is extracted into analyzePointerArithmetic
   and it is used by the implementation of slice.
   - Also I implemented comptime pointer addition.
 * Sema: extract logic into analyzeSlicePtr, analyzeSliceLen and use them
   inside the slice semantic analysis.
   - The approach in stage2 is much cleaner than stage1 because it uses
     more granular analysis calls for obtaining the slice pointer, doing
     arithmetic on it, and checking if the length is comptime-known.
 * Sema: use the slice Value Tag for slices when doing coercion from
   pointer-to-array.
 * LLVM backend: detect when emitting a GEP instruction into a
   pointer-to-array and add the extra index that is required.
 * Type: ptrAlignment for c_void returns 0.
 * Implement Value.hash and Value.eql for slices.
 * Remove accidentally duplicated behavior test.

```

## Parents

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

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