aboutsummaryrefslogtreecommitdiff
path: root/test/cases/compile_errors/mult_wrap_on_undefined_value.zig
diff options
context:
space:
mode:
authorTechatrix <19954306+Techatrix@users.noreply.github.com>2023-10-30 00:02:57 +0100
committerAndrew Kelley <andrew@ziglang.org>2024-01-16 16:27:31 -0800
commitec358d6db5a10989590e11bbbbd3bed9f81cf1f4 (patch)
treebd5affb8ff6bbbb4ea8ac22554c0a782d73bb261 /test/cases/compile_errors/mult_wrap_on_undefined_value.zig
parent4ace1f5a7ffeb569e97dc78807a1eacc5565a272 (diff)
downloadzig-ec358d6db5a10989590e11bbbbd3bed9f81cf1f4.tar.gz
zig-ec358d6db5a10989590e11bbbbd3bed9f81cf1f4.zip
sema: fix safe integer arithmetic operations on undefined values
Previously `@as(i64, undefined) +% 1` would produce `@as(@TypeOf(undefined), undefined)` which now gives `@as(i64, undefined)`. Previously `@as(i64, undefined) +| 1` would hit an assertion which now gives `@as(i64, undefined)`.
Diffstat (limited to 'test/cases/compile_errors/mult_wrap_on_undefined_value.zig')
-rw-r--r--test/cases/compile_errors/mult_wrap_on_undefined_value.zig38
1 files changed, 38 insertions, 0 deletions
diff --git a/test/cases/compile_errors/mult_wrap_on_undefined_value.zig b/test/cases/compile_errors/mult_wrap_on_undefined_value.zig
new file mode 100644
index 0000000000..2ecb6753f6
--- /dev/null
+++ b/test/cases/compile_errors/mult_wrap_on_undefined_value.zig
@@ -0,0 +1,38 @@
+comptime {
+ const undef: i64 = undefined;
+ const not_undef: i64 = 32;
+
+ // If either of the operands are zero, the result is zero.
+ @compileLog(undef *% 0);
+ @compileLog(not_undef *% 0);
+ @compileLog(0 *% undef);
+ @compileLog(0 *% not_undef);
+
+ // If either of the operands are one, result is the other operand.
+ @compileLog(undef *% 1);
+ @compileLog(not_undef *% 1);
+ @compileLog(1 *% undef);
+ @compileLog(1 *% not_undef);
+
+ // If either of the operands are undefined, result is undefined.
+ @compileLog(undef *% 2);
+ @compileLog(2 *% undef);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :6:5: error: found compile log statement
+//
+// Compile Log Output:
+// @as(i64, 0)
+// @as(i64, 0)
+// @as(i64, 0)
+// @as(i64, 0)
+// @as(i64, undefined)
+// @as(i64, 32)
+// @as(i64, undefined)
+// @as(i64, 32)
+// @as(i64, undefined)
+// @as(i64, undefined)