authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-06-14 17:57:28+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-14 10:57:28-04:00
log4ec09ac243afa0b784669e618ec09e9e444a0275
treea42c343a2b0d5b884f3ac377ba392a8cd9f99c7f
parentfc87f6e417d206a88b581b77d3a5494ae4c978dd

Enabled optional types of zero bit types with no LLVM DI type. (#1110)

* Zero bit optional types do not need a LLVM DI type

2 files changed, 13 insertions(+), 1 deletions(-)

src/analyze.cpp+2-1
......@@ -522,7 +522,6 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
522522
523523 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdOptional);
524524 assert(child_type->type_ref || child_type->zero_bits);
525 assert(child_type->di_type);
526525 entry->is_copyable = type_is_copyable(g, child_type);
527526
528527 buf_resize(&entry->name, 0);
......@@ -532,12 +531,14 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
532531 entry->type_ref = LLVMInt1Type();
533532 entry->di_type = g->builtin_types.entry_bool->di_type;
534533 } else if (type_is_codegen_pointer(child_type)) {
534 assert(child_type->di_type);
535535 // this is an optimization but also is necessary for calling C
536536 // functions where all pointers are maybe pointers
537537 // function types are technically pointers
538538 entry->type_ref = child_type->type_ref;
539539 entry->di_type = child_type->di_type;
540540 } else {
541 assert(child_type->di_type);
541542 // create a struct with a boolean whether this is the null value
542543 LLVMTypeRef elem_types[] = {
543544 child_type->type_ref,
test/cases/null.zig+11
......@@ -143,3 +143,14 @@ test "null with default unwrap" {
143143 const x: i32 = null orelse 1;
144144 assert(x == 1);
145145}
146
147test "optional types" {
148 comptime {
149 const opt_type_struct = StructWithOptionalType { .t=u8, };
150 assert(opt_type_struct.t != null and opt_type_struct.t.? == u8);
151 }
152}
153
154const StructWithOptionalType = struct {
155 t: ?type,
156};