Browse Source

Fix unions with zero variants

gingerBill 6 years ago
parent
commit
d99ffe604f
3 changed files with 11 additions and 7 deletions
  1. 5 0
      core/fmt/fmt.odin
  2. 0 5
      src/check_stmt.cpp
  3. 6 2
      src/ir.cpp

+ 5 - 0
core/fmt/fmt.odin

@@ -1306,6 +1306,11 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
 		}
 
 	case runtime.Type_Info_Union:
+		if type_info.size == 0 {
+			strings.write_string(fi.buf, "nil");
+			return;
+		}
+
 		tag_ptr := uintptr(v.data) + info.tag_offset;
 		tag_any := any{rawptr(tag_ptr), info.tag_type.id};
 

+ 0 - 5
src/check_stmt.cpp

@@ -1710,11 +1710,6 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
 					error(vd->type, "Invalid use of a polymorphic type '%s' in variable declaration", str);
 					gb_string_free(str);
 					init_type = t_invalid;
-				} else if (is_type_empty_union(init_type)) {
-					gbString str = type_to_string(init_type);
-					error(vd->type, "An empty union '%s' cannot be instantiated in variable declaration", str);
-					gb_string_free(str);
-					init_type = t_invalid;
 				}
 			}
 

+ 6 - 2
src/ir.cpp

@@ -4008,8 +4008,12 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
 		irValue *len = ir_map_len(proc, x);
 		return ir_emit_comp(proc, op_kind, len, v_zero);
 	} else if (is_type_union(t)) {
-		irValue *tag = ir_emit_union_tag_value(proc, x);
-		return ir_emit_comp(proc, op_kind, tag, v_zero);
+		if (type_size_of(t) == 0) {
+			return ir_emit_comp(proc, op_kind, v_zero, v_zero);
+		} else {
+			irValue *tag = ir_emit_union_tag_value(proc, x);
+			return ir_emit_comp(proc, op_kind, tag, v_zero);
+		}
 	} else if (is_type_typeid(t)) {
 		irValue *invalid_typeid = ir_value_constant(t_typeid, exact_value_i64(0));
 		return ir_emit_comp(proc, op_kind, x, invalid_typeid);