aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-05-01 15:02:06 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-05-01 15:02:06 -0700
commit60c2972c5d976e2dc8bf2432c8ab06edcf5c6d35 (patch)
tree2ed60b8283b58604b406446ed588f97f601b143d /src
parent615a98351768598db65fcfc521d3c67eb443eed6 (diff)
downloadzig-60c2972c5d976e2dc8bf2432c8ab06edcf5c6d35.tar.gz
zig-60c2972c5d976e2dc8bf2432c8ab06edcf5c6d35.zip
stage2: fix comptime fixed-width float division
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index b17ca031c2..012750e429 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -9847,25 +9847,37 @@ fn analyzeArithmetic(
// TODO: emit runtime safety for division by zero
//
// For floats:
- // If the rhs is zero, compile error for division by zero.
- // If the rhs is undefined, compile error because there is a possible
- // value (zero) for which the division would be illegal behavior.
+ // If the rhs is zero:
+ // * comptime_float: compile error for division by zero.
+ // * other float type:
+ // * if the lhs is zero: QNaN
+ // * otherwise: +Inf or -Inf depending on lhs sign
+ // If the rhs is undefined:
+ // * comptime_float: compile error because there is a possible
+ // value (zero) for which the division would be illegal behavior.
+ // * other float type: result is undefined
// If the lhs is undefined, result is undefined.
- if (maybe_lhs_val) |lhs_val| {
- if (!lhs_val.isUndef()) {
- if (lhs_val.compareWithZero(.eq)) {
- return sema.addConstant(resolved_type, Value.zero);
+ switch (scalar_tag) {
+ .Int, .ComptimeInt, .ComptimeFloat => {
+ if (maybe_lhs_val) |lhs_val| {
+ if (!lhs_val.isUndef()) {
+ if (lhs_val.compareWithZero(.eq)) {
+ return sema.addConstant(resolved_type, Value.zero);
+ }
+ }
}
- }
- }
- if (maybe_rhs_val) |rhs_val| {
- if (rhs_val.isUndef()) {
- return sema.failWithUseOfUndef(block, rhs_src);
- }
- if (rhs_val.compareWithZero(.eq)) {
- return sema.failWithDivideByZero(block, rhs_src);
- }
+ if (maybe_rhs_val) |rhs_val| {
+ if (rhs_val.isUndef()) {
+ return sema.failWithUseOfUndef(block, rhs_src);
+ }
+ if (rhs_val.compareWithZero(.eq)) {
+ return sema.failWithDivideByZero(block, rhs_src);
+ }
+ }
+ },
+ else => {},
}
+
if (maybe_lhs_val) |lhs_val| {
if (lhs_val.isUndef()) {
if (lhs_scalar_ty.isSignedInt() and rhs_scalar_ty.isSignedInt()) {