From 4ab9edeb53559f6ab6ef39a5ad9b45564d22aa8e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 9 Jul 2019 10:28:13 +0100 Subject: union #no_nil --- src/types.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index 875519f2d..dc7ecb946 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -131,6 +131,7 @@ struct TypeUnion { i64 variant_block_size; i64 custom_align; i64 tag_size; + bool no_nil; bool is_polymorphic; bool is_poly_specialized; @@ -1468,7 +1469,7 @@ bool type_has_nil(Type *t) { case Type_Map: return true; case Type_Union: - return true; + return !t->Union.no_nil; case Type_Struct: return false; case Type_Opaque: @@ -1625,7 +1626,8 @@ bool are_types_identical(Type *x, Type *y) { case Type_Union: if (y->kind == Type_Union) { if (x->Union.variants.count == y->Union.variants.count && - x->Union.custom_align == y->Union.custom_align) { + x->Union.custom_align == y->Union.custom_align && + x->Union.no_nil == y->Union.no_nil) { // NOTE(bill): zeroth variant is nullptr for_array(i, x->Union.variants) { if (!are_types_identical(x->Union.variants[i], y->Union.variants[i])) { @@ -1778,7 +1780,11 @@ i64 union_variant_index(Type *u, Type *v) { for_array(i, u->Union.variants) { Type *vt = u->Union.variants[i]; if (are_types_identical(v, vt)) { - return cast(i64)(i+1); + if (u->Union.no_nil) { + return cast(i64)(i+0); + } else { + return cast(i64)(i+1); + } } } return 0; -- cgit v1.2.3