authorgravatar for 12179851+leesongun@users.noreply.github.comleesongun <12179851+leesongun@users.noreply.github.com> 2022-03-27 17:49:54+09:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-27 11:49:54+03:00
log7ae22813eedbe3461f555b6f4c6e708b8c952b15
tree2d354145fcfa6a64d44fa88fc2747528cefc31eb
parentdbbda0f41a7c5e214801925f8447a15193c3c731
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

stage1: implement casting from u0


2 files changed, 21 insertions(+), 1 deletions(-)

src/stage1/codegen.cpp+13-1
...@@ -1707,7 +1707,6 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1707,7 +1707,6 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
1707 ZigType *wanted_type, LLVMValueRef expr_val)1707 ZigType *wanted_type, LLVMValueRef expr_val)
1708{1708{
1709 assert(actual_type->id == wanted_type->id);1709 assert(actual_type->id == wanted_type->id);
1710 assert(expr_val != nullptr);
17111710
1712 ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ?1711 ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ?
1713 actual_type->data.vector.elem_type : actual_type;1712 actual_type->data.vector.elem_type : actual_type;
...@@ -1733,6 +1732,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1733,6 +1732,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
1733 zig_unreachable();1732 zig_unreachable();
1734 }1733 }
17351734
1735 if (expr_val == nullptr) {
1736 if (scalar_actual_type->id == ZigTypeIdInt && actual_bits == 0) {
1737 if (wanted_bits == 0) {
1738 return expr_val;
1739 } else {
1740 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, wanted_type));
1741 return zero;
1742 }
1743 } else {
1744 zig_unreachable();
1745 }
1746 }
1747
1736 if (scalar_actual_type->id == ZigTypeIdInt && want_runtime_safety && (1748 if (scalar_actual_type->id == ZigTypeIdInt && want_runtime_safety && (
1737 // negative to unsigned1749 // negative to unsigned
1738 (!scalar_wanted_type->data.integral.is_signed && scalar_actual_type->data.integral.is_signed) ||1750 (!scalar_wanted_type->data.integral.is_signed && scalar_actual_type->data.integral.is_signed) ||
test/behavior/widening.zig+8
...@@ -20,6 +20,14 @@ test "integer widening" {...@@ -20,6 +20,14 @@ test "integer widening" {
20 try expect(f == a);20 try expect(f == a);
21}21}
2222
23fn zero() u0 {
24 return 0;
25}
26test "integer widening u0 to u8" {
27 const a: u8 = zero();
28 try expect(a == 0);
29}
30
23test "implicit unsigned integer to signed integer" {31test "implicit unsigned integer to signed integer" {
24 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO32 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO33 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO