diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 646633a90df43c5e309fa847f1d250950a62dfe5..1a4bada192e91de160970e4c5d22f633f135f595 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -2450,21 +2450,22 @@ pub const FuncGen = struct { const operand = try self.resolveInst(un_op); const operand_ty = self.air.typeOf(un_op); const optional_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; - var buf: Type.Payload.ElemType = undefined; - const payload_ty = optional_ty.optionalChild(&buf); - if (!payload_ty.hasCodeGenBits()) { - if (invert) { - return self.builder.buildNot(operand, ""); - } else { - return operand; - } - } if (optional_ty.isPtrLikeOptional()) { const optional_llvm_ty = try self.dg.llvmType(optional_ty); const loaded = if (operand_is_ptr) self.builder.buildLoad(operand, "") else operand; return self.builder.buildICmp(pred, loaded, optional_llvm_ty.constNull(), ""); } + var buf: Type.Payload.ElemType = undefined; + const payload_ty = optional_ty.optionalChild(&buf); + if (!payload_ty.hasCodeGenBits()) { + if (invert) { + return self.builder.buildNot(operand, ""); + } else { + return operand; + } + } + if (operand_is_ptr or isByRef(optional_ty)) { const index_type = self.context.intType(32); diff --git a/src/type.zig b/src/type.zig index e77780d9ca7d92a00f9ba072f0502a96e39827aa..7ac235e68e7e76e157be747afea6fbdc8ca7e5b2 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2347,11 +2347,13 @@ pub const Type = extern union { } } - /// Asserts that the type is an optional + /// Asserts that the type is an optional or a pointer that can be null. pub fn isPtrLikeOptional(self: Type) bool { switch (self.tag()) { .optional_single_const_pointer, .optional_single_mut_pointer, + .c_const_pointer, + .c_mut_pointer, => return true, .optional => { @@ -2367,6 +2369,8 @@ pub const Type = extern union { .Many, .One => return !info.@"allowzero", } }, + + .pointer => return self.castTag(.pointer).?.data.size == .C, else => unreachable, } }