aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-07-05 22:24:25 +0000
committerGitHub <noreply@github.com>2020-07-05 22:24:25 +0000
commitdcca5cf1a9241e7274ae03a587b7e3313d673f3e (patch)
tree6b4ca8bd860d267f3c75ba37e78d0acd9bc49753 /src/codegen.cpp
parent289eab9177443bdfadfe750afda8f7f32f43be0f (diff)
parentb8553b4813e971e50dcae7b8181e5f6771f3dbff (diff)
downloadzig-dcca5cf1a9241e7274ae03a587b7e3313d673f3e.tar.gz
zig-dcca5cf1a9241e7274ae03a587b7e3313d673f3e.zip
Merge pull request #5797 from xackus/intcast-runtime-safety
stage1: `@intcast` runtime safety for unsigned -> signed of same bit count
Diffstat (limited to 'src/codegen.cpp')
-rw-r--r--src/codegen.cpp10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 18ef7d9182..2f72861bc2 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -1535,9 +1535,11 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
zig_unreachable();
}
- if (actual_type->id == ZigTypeIdInt &&
- !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
- want_runtime_safety)
+ if (actual_type->id == ZigTypeIdInt && want_runtime_safety && (
+ // negative to unsigned
+ (!wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed) ||
+ // unsigned would become negative
+ (wanted_type->data.integral.is_signed && !actual_type->data.integral.is_signed && actual_bits == wanted_bits)))
{
LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
@@ -1547,7 +1549,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
LLVMPositionBuilderAtEnd(g->builder, fail_block);
- gen_safety_crash(g, PanicMsgIdCastNegativeToUnsigned);
+ gen_safety_crash(g, actual_type->data.integral.is_signed ? PanicMsgIdCastNegativeToUnsigned : PanicMsgIdCastTruncatedData);
LLVMPositionBuilderAtEnd(g->builder, ok_block);
}