mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
fix const initialization of optional C pointer to null
This commit is contained in:
parent
74c123cd0a
commit
d0d615d819
@ -626,7 +626,7 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
|
||||
if (child_type->zero_bits) {
|
||||
entry->type_ref = LLVMInt1Type();
|
||||
entry->di_type = g->builtin_types.entry_bool->di_type;
|
||||
} else if (type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
} else if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
assert(child_type->di_type);
|
||||
// this is an optimization but also is necessary for calling C
|
||||
// functions where all pointers are maybe pointers
|
||||
@ -4170,10 +4170,10 @@ ZigType *get_codegen_ptr_type(ZigType *type) {
|
||||
}
|
||||
|
||||
bool type_is_nonnull_ptr(ZigType *type) {
|
||||
return type_is_codegen_pointer(type) && !ptr_allows_addr_zero(type);
|
||||
return type_is_non_optional_pointer(type) && !ptr_allows_addr_zero(type);
|
||||
}
|
||||
|
||||
bool type_is_codegen_pointer(ZigType *type) {
|
||||
bool type_is_non_optional_pointer(ZigType *type) {
|
||||
return get_codegen_ptr_type(type) == type;
|
||||
}
|
||||
|
||||
@ -4692,7 +4692,7 @@ bool handle_is_ptr(ZigType *type_entry) {
|
||||
return type_has_bits(type_entry->data.error_union.payload_type);
|
||||
case ZigTypeIdOptional:
|
||||
return type_has_bits(type_entry->data.maybe.child_type) &&
|
||||
!type_is_codegen_pointer(type_entry->data.maybe.child_type) &&
|
||||
!type_is_non_optional_pointer(type_entry->data.maybe.child_type) &&
|
||||
type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
|
||||
case ZigTypeIdUnion:
|
||||
assert(type_entry->data.unionation.zero_bits_known);
|
||||
|
||||
@ -61,7 +61,7 @@ ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **c
|
||||
Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
|
||||
Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
|
||||
void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
|
||||
bool type_is_codegen_pointer(ZigType *type);
|
||||
bool type_is_non_optional_pointer(ZigType *type);
|
||||
|
||||
ZigType *get_src_ptr_type(ZigType *type);
|
||||
ZigType *get_codegen_ptr_type(ZigType *type);
|
||||
|
||||
@ -3959,7 +3959,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
|
||||
if (child_type->zero_bits) {
|
||||
return maybe_handle;
|
||||
} else {
|
||||
bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
if (is_scalar) {
|
||||
return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
|
||||
} else {
|
||||
@ -3999,7 +3999,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
|
||||
if (child_type->zero_bits) {
|
||||
return nullptr;
|
||||
} else {
|
||||
bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
if (is_scalar) {
|
||||
return maybe_ptr;
|
||||
} else {
|
||||
@ -4862,7 +4862,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
|
||||
}
|
||||
|
||||
LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
|
||||
if (type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
return payload_val;
|
||||
}
|
||||
|
||||
@ -6099,9 +6099,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
|
||||
case ZigTypeIdOptional:
|
||||
{
|
||||
ZigType *child_type = type_entry->data.maybe.child_type;
|
||||
if (child_type->zero_bits) {
|
||||
if (!type_has_bits(child_type)) {
|
||||
return LLVMConstInt(LLVMInt1Type(), const_val->data.x_optional ? 1 : 0, false);
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
} else if (get_codegen_ptr_type(type_entry) != nullptr) {
|
||||
return gen_const_val_ptr(g, const_val, name);
|
||||
} else if (child_type->id == ZigTypeIdErrorSet) {
|
||||
return gen_const_val_err_set(g, const_val, name);
|
||||
@ -8513,7 +8513,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
|
||||
if (child_type->zero_bits) {
|
||||
buf_init_from_str(out_buf, "bool");
|
||||
return;
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
} else if (type_is_non_optional_pointer(child_type)) {
|
||||
return get_c_type(g, gen_h, child_type, out_buf);
|
||||
} else {
|
||||
zig_unreachable();
|
||||
|
||||
@ -124,3 +124,9 @@ test "implicit cast error unions with non-optional to optional pointer" {
|
||||
S.doTheTest();
|
||||
comptime S.doTheTest();
|
||||
}
|
||||
|
||||
test "initialize const optional C pointer to null" {
|
||||
const a: ?[*c]i32 = null;
|
||||
expect(a == null);
|
||||
comptime expect(a == null);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user