diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index bfe58ae912123cdfa0f0d926da140dc86fc970fd..40e444010231110ad36588f08c483b6c0b0a4799 100644 --- a/src/stage1/codegen.cpp +++ b/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) || diff --git a/test/behavior/widening.zig b/test/behavior/widening.zig index 0d5722db2b53d97c58e740b05c00500529a2b14e..5c25f7f6274f6e09d628656903219a6fad19dc71 100644 --- a/test/behavior/widening.zig +++ b/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