Sema: rewrite comptime arithmetic

This commit reworks how Sema handles arithmetic on comptime-known
values, fixing many bugs in the process.

The general pattern is that arithmetic on comptime-known values is now
handled by the new namespace `Sema.arith`. Functions handling comptime
arithmetic no longer live on `Value`; this is because some of them can
emit compile errors, so some *can't* go on `Value`. Only semantic
analysis should really be doing arithmetic on `Value`s anyway, so it
makes sense for it to integrate more tightly with `Sema`.

This commit also implements more coherent rules surrounding how
`undefined` interacts with comptime and mixed-comptime-runtime
arithmetic. The rules are as follows.

* If an operation cannot trigger Illegal Behavior, and any operand is
  `undefined`, the result is `undefined`. This includes operations like
  `0 *| undef`, where the LHS logically *could* be used to determine a
  defined result. This is partly to simplify the language, but mostly to
  permit codegen backends to represent `undefined` values as completely
  invalid states.

* If an operation *can* trigger Illegal Behvaior, and any operand is
  `undefined`, then Illegal Behavior results. This occurs even if the
  operand in question isn't the one that "decides" illegal behavior; for
  instance, `undef / 1` is undefined. This is for the same reasons as
  described above.

* An operation which would trigger Illegal Behavior, when evaluated at
  comptime, instead triggers a compile error. Additionally, if one
  operand is comptime-known undef, such that the other (runtime-known)
  operand isn't needed to determine that Illegal Behavior would occur,
  the compile error is triggered.

* The only situation in which an operation with one comptime-known
  operand has a comptime-known result is if that operand is undefined,
  in which case the result is either undefined or a compile error per
  the above rules. This could potentially be loosened in future (for
  instance, `0 * rt` could be comptime-known 0 with a runtime assertion
  that `rt` is not undefined), but at least for now, defining it more
  conservatively simplifies the language and allows us to easily change
  this in future if desired.

This commit fixes many bugs regarding the handling of `undefined`,
particularly in vectors. Along with a collection of smaller tests, two
very large test cases are added to check arithmetic on `undefined`.

The operations which have been rewritten in this PR are:

* `+`, `+%`, `+|`, `@addWithOverflow`
* `-`, `-%`, `-|`, `@subWithOverflow`
* `*`, `*%`, `*|`, `@mulWithOverflow`
* `/`, `@divFloor`, `@divTrunc`, `@divExact`
* `%`, `@rem`, `@mod`

Other arithmetic operations are currently unchanged.

Resolves: #22743
Resolves: #22745
Resolves: #22748
Resolves: #22749
Resolves: #22914
This commit is contained in:
mlugg
2025-03-10 03:31:36 +00:00
committed by Matthew Lugg
parent aa3db7cc15
commit 2a4e06bcb3
27 changed files with 11233 additions and 2517 deletions

View File

@@ -1265,12 +1265,6 @@ test "allow signed integer division/remainder when values are comptime-known and
try expect(5 % 3 == 2);
try expect(-6 % 3 == 0);
var undef: i32 = undefined;
_ = &undef;
if (0 % undef != 0) {
@compileError("0 as numerator should return comptime zero independent of denominator");
}
}
test "quad hex float literal parsing accurate" {
@@ -1861,3 +1855,134 @@ test "runtime int comparison to inf is comptime-known" {
comptime S.doTheTest(f64, 123);
comptime S.doTheTest(f128, 123);
}
test "float divide by zero" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
const S = struct {
fn doTheTest(comptime F: type, zero: F, one: F) !void {
try expect(math.isPositiveInf(@divTrunc(one, zero)));
try expect(math.isPositiveInf(@divFloor(one, zero)));
try expect(math.isNan(@rem(one, zero)));
try expect(math.isNan(@mod(one, zero)));
}
};
try S.doTheTest(f16, 0, 1);
comptime S.doTheTest(f16, 0, 1) catch unreachable;
try S.doTheTest(f32, 0, 1);
comptime S.doTheTest(f32, 0, 1) catch unreachable;
try S.doTheTest(f64, 0, 1);
comptime S.doTheTest(f64, 0, 1) catch unreachable;
try S.doTheTest(f80, 0, 1);
comptime S.doTheTest(f80, 0, 1) catch unreachable;
try S.doTheTest(f128, 0, 1);
comptime S.doTheTest(f128, 0, 1) catch unreachable;
}
test "partially-runtime integer vector division would be illegal if vector elements were reordered" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
var lhs: @Vector(2, i8) = .{ -128, 5 };
const rhs: @Vector(2, i8) = .{ 1, -1 };
const expected: @Vector(2, i8) = .{ -128, -5 };
lhs = lhs; // suppress error
const trunc = @divTrunc(lhs, rhs);
try expect(trunc[0] == expected[0]);
try expect(trunc[1] == expected[1]);
const floor = @divFloor(lhs, rhs);
try expect(floor[0] == expected[0]);
try expect(floor[1] == expected[1]);
const exact = @divExact(lhs, rhs);
try expect(exact[0] == expected[0]);
try expect(exact[1] == expected[1]);
}
test "float vector division of comptime zero by runtime nan is nan" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
const ct_zero: @Vector(1, f32) = .{0};
var rt_nan: @Vector(1, f32) = .{math.nan(f32)};
rt_nan = rt_nan; // suppress error
try expect(math.isNan((@divTrunc(ct_zero, rt_nan))[0]));
try expect(math.isNan((@divFloor(ct_zero, rt_nan))[0]));
try expect(math.isNan((ct_zero / rt_nan)[0]));
}
test "float vector multiplication of comptime zero by runtime nan is nan" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const ct_zero: @Vector(1, f32) = .{0};
var rt_nan: @Vector(1, f32) = .{math.nan(f32)};
rt_nan = rt_nan; // suppress error
try expect(math.isNan((ct_zero * rt_nan)[0]));
try expect(math.isNan((rt_nan * ct_zero)[0]));
}
test "comptime float vector division of zero by nan is nan" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const ct_zero: @Vector(1, f32) = .{0};
const ct_nan: @Vector(1, f32) = .{math.nan(f32)};
comptime assert(math.isNan((@divTrunc(ct_zero, ct_nan))[0]));
comptime assert(math.isNan((@divFloor(ct_zero, ct_nan))[0]));
comptime assert(math.isNan((ct_zero / ct_nan)[0]));
}
test "comptime float vector multiplication of zero by nan is nan" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const ct_zero: @Vector(1, f32) = .{0};
const ct_nan: @Vector(1, f32) = .{math.nan(f32)};
comptime assert(math.isNan((ct_zero * ct_nan)[0]));
comptime assert(math.isNan((ct_nan * ct_zero)[0]));
}