Zig Version
0.17.0-dev.857+2b2b85c5f
Steps to Reproduce, Observed Behavior, and Expected Behavior
The programs below work as expected if x is declared as a runtime int type, e.g. i16, but not when x is a comptime_int.
It seems like the compiler tries to coerce x to a type that's too small, fails, then never tries coercing to a type that actually fits. To work around this, I end up having to either assign problematic comptime values to temporary runtime variables or wrap them in @as.
Test programs
test1.zig:
pub fn main() void {
const x = 200;
const y: i8 = 100;
const z: i8 = x - y;
@import("std").debug.print("{}\n", .{z});
}
test2.zig:
pub fn main() void {
const x = -200;
const y: i8 = 100;
const z: i8 = x + y;
@import("std").debug.print("{}\n", .{z});
}
test3.zig:
pub fn main() void {
const x = 200;
const y: i8 = 2;
const z: i8 = x / y;
@import("std").debug.print("{}\n", .{z});
}
Observed behavior
> zig run test1.zig
test1.zig:4:19: error: type 'i8' cannot represent integer value '200'
const z: i8 = x - y;
^
> zig run test2.zig
test2.zig:4:19: error: type 'i8' cannot represent integer value '-200'
const z: i8 = x + y;
^
> zig run test3.zig
test3.zig:4:19: error: type 'i8' cannot represent integer value '200'
const z: i8 = x / y;
^
Expected behavior
> zig run test1.zig
100
> zig run test2.zig
-100
> zig run test3.zig
100
Or, if this is the intended behavior for coercing/resolving comptime_int, that should be documented.
### Zig Version
0.17.0-dev.857+2b2b85c5f
### Steps to Reproduce, Observed Behavior, and Expected Behavior
The programs below work as expected if `x` is declared as a runtime int type, e.g. `i16`, but not when `x` is a `comptime_int`.
It seems like the compiler tries to coerce `x` to a type that's too small, fails, then never tries coercing to a type that actually fits. To work around this, I end up having to either assign problematic comptime values to temporary runtime variables or wrap them in `@as`.
**Test programs**
test1.zig:
```
pub fn main() void {
const x = 200;
const y: i8 = 100;
const z: i8 = x - y;
@import("std").debug.print("{}\n", .{z});
}
```
test2.zig:
```
pub fn main() void {
const x = -200;
const y: i8 = 100;
const z: i8 = x + y;
@import("std").debug.print("{}\n", .{z});
}
```
test3.zig:
```
pub fn main() void {
const x = 200;
const y: i8 = 2;
const z: i8 = x / y;
@import("std").debug.print("{}\n", .{z});
}
```
**Observed behavior**
```
> zig run test1.zig
test1.zig:4:19: error: type 'i8' cannot represent integer value '200'
const z: i8 = x - y;
^
```
```
> zig run test2.zig
test2.zig:4:19: error: type 'i8' cannot represent integer value '-200'
const z: i8 = x + y;
^
```
```
> zig run test3.zig
test3.zig:4:19: error: type 'i8' cannot represent integer value '200'
const z: i8 = x / y;
^
```
**Expected behavior**
```
> zig run test1.zig
100
```
```
> zig run test2.zig
-100
```
```
> zig run test3.zig
100
```
Or, if this is the intended behavior for coercing/resolving `comptime_int`, that should be documented.