Skip to content

Commit

Permalink
Enabled optional types of zero bit types with no LLVM DI type. (#1110)
Browse files Browse the repository at this point in the history
* Zero bit optional types do not need a LLVM DI type
  • Loading branch information
alexnask authored and andrewrk committed Jun 14, 2018
1 parent fc87f6e commit 4ec09ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/analyze.cpp
Expand Up @@ -522,7 +522,6 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {

TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdOptional);
assert(child_type->type_ref || child_type->zero_bits);
assert(child_type->di_type);
entry->is_copyable = type_is_copyable(g, child_type);

buf_resize(&entry->name, 0);
Expand All @@ -532,12 +531,14 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
entry->type_ref = LLVMInt1Type();
entry->di_type = g->builtin_types.entry_bool->di_type;
} else if (type_is_codegen_pointer(child_type)) {
assert(child_type->di_type);
// this is an optimization but also is necessary for calling C
// functions where all pointers are maybe pointers
// function types are technically pointers
entry->type_ref = child_type->type_ref;
entry->di_type = child_type->di_type;
} else {
assert(child_type->di_type);
// create a struct with a boolean whether this is the null value
LLVMTypeRef elem_types[] = {
child_type->type_ref,
Expand Down
11 changes: 11 additions & 0 deletions test/cases/null.zig
Expand Up @@ -143,3 +143,14 @@ test "null with default unwrap" {
const x: i32 = null orelse 1;
assert(x == 1);
}

test "optional types" {
comptime {
const opt_type_struct = StructWithOptionalType { .t=u8, };
assert(opt_type_struct.t != null and opt_type_struct.t.? == u8);
}
}

const StructWithOptionalType = struct {
t: ?type,
};

0 comments on commit 4ec09ac

Please sign in to comment.