Commit 7ae22813ee

leesongun <12179851+leesongun@users.noreply.github.com>
2022-03-27 10:49:54
stage1: implement casting from u0
1 parent dbbda0f
Changed files (2)
src
test
behavior
src/stage1/codegen.cpp
@@ -1707,7 +1707,6 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
         ZigType *wanted_type, LLVMValueRef expr_val)
 {
     assert(actual_type->id == wanted_type->id);
-    assert(expr_val != nullptr);
 
     ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ?
         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
         zig_unreachable();
     }
 
+    if (expr_val == nullptr) {
+        if (scalar_actual_type->id == ZigTypeIdInt && actual_bits == 0) {
+            if (wanted_bits == 0) {
+                return expr_val;
+            } else {
+                LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, wanted_type));
+                return zero;
+            }
+        } else {
+            zig_unreachable();
+        }
+    }
+
     if (scalar_actual_type->id == ZigTypeIdInt && want_runtime_safety && (
         // negative to unsigned
         (!scalar_wanted_type->data.integral.is_signed && scalar_actual_type->data.integral.is_signed) ||
test/behavior/widening.zig
@@ -20,6 +20,14 @@ test "integer widening" {
     try expect(f == a);
 }
 
+fn zero() u0 {
+    return 0;
+}
+test "integer widening u0 to u8" {
+    const a: u8 = zero();
+    try expect(a == 0);
+}
+
 test "implicit unsigned integer to signed integer" {
     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO