# AstGen: make sure for range start and end are usizes · gitcafe/zig

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

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

Visibility: public

Requested revision: 0b1e8690dadb5d81b4d63832e27a117f57b4be81

Requested commit: 0b1e8690dadb5d81b4d63832e27a117f57b4be81

Commit: 0b1e8690dadb5d81b4d63832e27a117f57b4be81

Tree: 20cc3fb63d08286bd78a1b77b529ff509c5e969a

Author: r00ster91

Committer: Andrew Kelley

## Message

````
AstGen: make sure for range start and end are usizes

Fixes #16311

The actual cause of #16311 is the `start_is_zero` special case:
```zig
                const range_len = if (end_val == .none or start_is_zero)
                    end_val
                else
                    try parent_gz.addPlNode(.sub, input, Zir.Inst.Bin{
                        .lhs = end_val,
                        .rhs = start_val,
                    });
```
It only happens if the range start is 0. In that case we would not perform any type checking.
Only in the other cases coincidentally `.sub` performs type checking in Sema, but the errors are still rather poor:
```
$ zig test x.zig
x.zig:9:15: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    for ("abc".."def") |val| {
         ~~~~~^~~~~~~
```
Note how it's the same as if I use `-`:
```
x.zig:9:11: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    "abc" - "def";
    ~~~~~~^~~~~~~
```
Now after this PR, the errors are much clearer for both range start and end:
```
x.zig:9:10: error: expected type 'usize', found '*const [3:0]u8'
    for ("abc".."def") |val| {
         ^~~~~
```
This is why I decided to use `.ty` instead of `.coerced_ty` for both range start and end rather than
just perform type checking in that `end_val == .none or start_is_zero` case.

````

## Parents

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

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