authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-18 17:51:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-18 17:51:50-04:00
log345f8db1c49efe3d9ca9859a296f24e728a3b43a
treec0b6d17a4ef6f444365f0af57682bb9a29c5cbd5
parentc1af3605328d21f59ee8ceba3c7350193f0a2429
signaturelock-open Commit is signed but in an unrecognized format.

fix optional pointer to empty struct incorrectly being non-null

closes #1178

4 files changed, 28 insertions(+), 8 deletions(-)

src/analyze.cpp+11-3
......@@ -3971,7 +3971,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
39713971 }
39723972}
39733973
3974ZigType *get_codegen_ptr_type(ZigType *type) {
3974ZigType *get_src_ptr_type(ZigType *type) {
39753975 if (type->id == ZigTypeIdPointer) return type;
39763976 if (type->id == ZigTypeIdFn) return type;
39773977 if (type->id == ZigTypeIdPromise) return type;
......@@ -3983,12 +3983,19 @@ ZigType *get_codegen_ptr_type(ZigType *type) {
39833983 return nullptr;
39843984}
39853985
3986ZigType *get_codegen_ptr_type(ZigType *type) {
3987 ZigType *ty = get_src_ptr_type(type);
3988 if (ty == nullptr || !type_has_bits(ty))
3989 return nullptr;
3990 return ty;
3991}
3992
39863993bool type_is_codegen_pointer(ZigType *type) {
39873994 return get_codegen_ptr_type(type) == type;
39883995}
39893996
39903997uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
3991 ZigType *ptr_type = get_codegen_ptr_type(type);
3998 ZigType *ptr_type = get_src_ptr_type(type);
39923999 if (ptr_type->id == ZigTypeIdPointer) {
39934000 return (ptr_type->data.pointer.explicit_alignment == 0) ?
39944001 get_abi_alignment(g, ptr_type->data.pointer.child_type) : ptr_type->data.pointer.explicit_alignment;
......@@ -3996,6 +4003,7 @@ uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
39964003 // I tried making this use LLVMABIAlignmentOfType but it trips this assertion in LLVM:
39974004 // "Cannot getTypeInfo() on a type that is unsized!"
39984005 // when getting the alignment of `?extern fn() void`.
4006 // See http://lists.llvm.org/pipermail/llvm-dev/2018-September/126142.html
39994007 return (ptr_type->data.fn.fn_type_id.alignment == 0) ? 1 : ptr_type->data.fn.fn_type_id.alignment;
40004008 } else if (ptr_type->id == ZigTypeIdPromise) {
40014009 return get_coro_frame_align_bytes(g);
......@@ -4005,7 +4013,7 @@ uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
40054013}
40064014
40074015bool get_ptr_const(ZigType *type) {
4008 ZigType *ptr_type = get_codegen_ptr_type(type);
4016 ZigType *ptr_type = get_src_ptr_type(type);
40094017 if (ptr_type->id == ZigTypeIdPointer) {
40104018 return ptr_type->data.pointer.is_const;
40114019 } else if (ptr_type->id == ZigTypeIdFn) {
src/analyze.hpp+1
......@@ -53,6 +53,7 @@ Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
5353void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node);
5454bool type_is_codegen_pointer(ZigType *type);
5555
56ZigType *get_src_ptr_type(ZigType *type);
5657ZigType *get_codegen_ptr_type(ZigType *type);
5758uint32_t get_ptr_align(CodeGen *g, ZigType *type);
5859bool get_ptr_const(ZigType *type);
src/ir.cpp+10-5
......@@ -155,7 +155,7 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
155155static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val);
156156
157157static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
158 assert(get_codegen_ptr_type(const_val->type) != nullptr);
158 assert(get_src_ptr_type(const_val->type) != nullptr);
159159 assert(const_val->special == ConstValSpecialStatic);
160160 ConstExprValue *result;
161161 switch (const_val->data.x_ptr.special) {
......@@ -20161,12 +20161,15 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
2016120161 if (type_is_invalid(src_type))
2016220162 return ira->codegen->builtin_types.entry_invalid;
2016320163
20164 if (get_codegen_ptr_type(src_type) == nullptr) {
20164 // We have a check for zero bits later so we use get_src_ptr_type to
20165 // validate src_type and dest_type.
20166
20167 if (get_src_ptr_type(src_type) == nullptr) {
2016520168 ir_add_error(ira, ptr, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));
2016620169 return ira->codegen->builtin_types.entry_invalid;
2016720170 }
2016820171
20169 if (get_codegen_ptr_type(dest_type) == nullptr) {
20172 if (get_src_ptr_type(dest_type) == nullptr) {
2017020173 ir_add_error(ira, dest_type_value,
2017120174 buf_sprintf("expected pointer, found '%s'", buf_ptr(&dest_type->name)));
2017220175 return ira->codegen->builtin_types.entry_invalid;
......@@ -20465,7 +20468,8 @@ static ZigType *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstructionI
2046520468 if (type_is_invalid(dest_type))
2046620469 return ira->codegen->builtin_types.entry_invalid;
2046720470
20468 if (get_codegen_ptr_type(dest_type) == nullptr) {
20471 // We explicitly check for the size, so we can use get_src_ptr_type
20472 if (get_src_ptr_type(dest_type) == nullptr) {
2046920473 ir_add_error(ira, dest_type_value, buf_sprintf("expected pointer, found '%s'", buf_ptr(&dest_type->name)));
2047020474 return ira->codegen->builtin_types.entry_invalid;
2047120475 }
......@@ -20571,7 +20575,8 @@ static ZigType *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstructionP
2057120575
2057220576 ZigType *usize = ira->codegen->builtin_types.entry_usize;
2057320577
20574 if (get_codegen_ptr_type(target->value.type) == nullptr) {
20578 // We check size explicitly so we can use get_src_ptr_type here.
20579 if (get_src_ptr_type(target->value.type) == nullptr) {
2057520580 ir_add_error(ira, target,
2057620581 buf_sprintf("expected pointer, found '%s'", buf_ptr(&target->value.type->name)));
2057720582 return ira->codegen->builtin_types.entry_invalid;
test/cases/null.zig+6
......@@ -154,3 +154,9 @@ test "optional types" {
154154const StructWithOptionalType = struct {
155155 t: ?type,
156156};
157
158test "optional pointer to 0 bit type null value at runtime" {
159 const EmptyStruct = struct {};
160 var x: ?*EmptyStruct = null;
161 assert(x == null);
162}