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
17071707 ZigType *wanted_type, LLVMValueRef expr_val)
17081708{
17091709 assert(actual_type->id == wanted_type->id);
1710 assert(expr_val != nullptr);
17111710
17121711 ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ?
17131712 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
17331732 zig_unreachable();
17341733 }
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
17361748 if (scalar_actual_type->id == ZigTypeIdInt && want_runtime_safety && (
17371749 // negative to unsigned
17381750 (!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" {
2020 try expect(f == a);
2121}
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
2331test "implicit unsigned integer to signed integer" {
2432 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
2533 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO