authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-03 10:47:06-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-03 10:47:06-05:00
logd0d615d8197eeb196ac500009637296bd00c6583
treeeb45310d1e02a8be88574f593b12142e78fea4b3
parent74c123cd0a9a045aa5266ea10366863ad81310a0
signature Commit is signed but in an unrecognized format.

fix const initialization of optional C pointer to null


4 files changed, 17 insertions(+), 11 deletions(-)

src/analyze.cpp+4-4
......@@ -626,7 +626,7 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
626626 if (child_type->zero_bits) {
627627 entry->type_ref = LLVMInt1Type();
628628 entry->di_type = g->builtin_types.entry_bool->di_type;
629 } else if (type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
629 } else if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
630630 assert(child_type->di_type);
631631 // this is an optimization but also is necessary for calling C
632632 // functions where all pointers are maybe pointers
......@@ -4170,10 +4170,10 @@ ZigType *get_codegen_ptr_type(ZigType *type) {
41704170}
41714171
41724172bool type_is_nonnull_ptr(ZigType *type) {
4173 return type_is_codegen_pointer(type) && !ptr_allows_addr_zero(type);
4173 return type_is_non_optional_pointer(type) && !ptr_allows_addr_zero(type);
41744174}
41754175
4176bool type_is_codegen_pointer(ZigType *type) {
4176bool type_is_non_optional_pointer(ZigType *type) {
41774177 return get_codegen_ptr_type(type) == type;
41784178}
41794179
......@@ -4692,7 +4692,7 @@ bool handle_is_ptr(ZigType *type_entry) {
46924692 return type_has_bits(type_entry->data.error_union.payload_type);
46934693 case ZigTypeIdOptional:
46944694 return type_has_bits(type_entry->data.maybe.child_type) &&
4695 !type_is_codegen_pointer(type_entry->data.maybe.child_type) &&
4695 !type_is_non_optional_pointer(type_entry->data.maybe.child_type) &&
46964696 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
46974697 case ZigTypeIdUnion:
46984698 assert(type_entry->data.unionation.zero_bits_known);
src/analyze.hpp+1-1
......@@ -61,7 +61,7 @@ ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **c
6161Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
6262Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
6363void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
64bool type_is_codegen_pointer(ZigType *type);
64bool type_is_non_optional_pointer(ZigType *type);
6565
6666ZigType *get_src_ptr_type(ZigType *type);
6767ZigType *get_codegen_ptr_type(ZigType *type);
src/codegen.cpp+6-6
......@@ -3959,7 +3959,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
39593959 if (child_type->zero_bits) {
39603960 return maybe_handle;
39613961 } else {
3962 bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
3962 bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
39633963 if (is_scalar) {
39643964 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
39653965 } else {
......@@ -3999,7 +3999,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39993999 if (child_type->zero_bits) {
40004000 return nullptr;
40014001 } else {
4002 bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
4002 bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
40034003 if (is_scalar) {
40044004 return maybe_ptr;
40054005 } else {
......@@ -4862,7 +4862,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48624862 }
48634863
48644864 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
4865 if (type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
4865 if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
48664866 return payload_val;
48674867 }
48684868
......@@ -6099,9 +6099,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
60996099 case ZigTypeIdOptional:
61006100 {
61016101 ZigType *child_type = type_entry->data.maybe.child_type;
6102 if (child_type->zero_bits) {
6102 if (!type_has_bits(child_type)) {
61036103 return LLVMConstInt(LLVMInt1Type(), const_val->data.x_optional ? 1 : 0, false);
6104 } else if (type_is_codegen_pointer(child_type)) {
6104 } else if (get_codegen_ptr_type(type_entry) != nullptr) {
61056105 return gen_const_val_ptr(g, const_val, name);
61066106 } else if (child_type->id == ZigTypeIdErrorSet) {
61076107 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
85138513 if (child_type->zero_bits) {
85148514 buf_init_from_str(out_buf, "bool");
85158515 return;
8516 } else if (type_is_codegen_pointer(child_type)) {
8516 } else if (type_is_non_optional_pointer(child_type)) {
85178517 return get_c_type(g, gen_h, child_type, out_buf);
85188518 } else {
85198519 zig_unreachable();
test/stage1/behavior/pointers.zig+6
......@@ -124,3 +124,9 @@ test "implicit cast error unions with non-optional to optional pointer" {
124124 S.doTheTest();
125125 comptime S.doTheTest();
126126}
127
128test "initialize const optional C pointer to null" {
129 const a: ?[*c]i32 = null;
130 expect(a == null);
131 comptime expect(a == null);
132}