From 8eedec4f938f89875e36750a33b69de921b453f1 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 8 Mar 2019 10:37:12 +0100 Subject: [PATCH 1/7] fixed #1600 --- src/ir.cpp | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index a8771285f6..40cd1f36da 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15506,13 +15506,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ ConstExprValue *payload_val = union_val->data.x_union.payload; ZigType *field_type = field->type_entry; - if (field_type->id == ZigTypeIdVoid) { - assert(payload_val == nullptr); - payload_val = create_const_vals(1); - payload_val->special = ConstValSpecialStatic; - payload_val->type = field_type; - } - ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type, is_const, is_volatile, PtrLenSingle, 0, 0, 0); @@ -18052,19 +18045,10 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr case ZigTypeIdNull: case ZigTypeIdArgTuple: case ZigTypeIdOpaque: - *out = nullptr; - return ErrorNone; - default: - { - // Lookup an available value in our cache. - auto entry = ira->codegen->type_info_cache.maybe_get(type_entry); - if (entry != nullptr) { - *out = entry->value; - return ErrorNone; - } - - // Fallthrough if we don't find one. - } + result = create_const_vals(1); + result->special = ConstValSpecialStatic; + result->type = ira->codegen->builtin_types.entry_void; + break; case ZigTypeIdInt: { result = create_const_vals(1); @@ -18636,7 +18620,6 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira, out_val->data.x_union.payload = payload; if (payload != nullptr) { - assert(payload->type->id == ZigTypeIdStruct); payload->parent.id = ConstParentIdUnion; payload->parent.data.p_union.union_val = out_val; } From 29accac74c64bb91c3a36e19db8602a77e19d85e Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 8 Mar 2019 10:40:11 +0100 Subject: [PATCH 2/7] removed wild tab --- src/ir.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 40cd1f36da..968f49fb5c 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -18048,7 +18048,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr result = create_const_vals(1); result->special = ConstValSpecialStatic; result->type = ira->codegen->builtin_types.entry_void; - break; + break; case ZigTypeIdInt: { result = create_const_vals(1); From 5a52613caf940b264bc0ed011e503ceec769200a Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 8 Mar 2019 10:58:29 +0100 Subject: [PATCH 3/7] use cached const_void_val --- src/ir.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 968f49fb5c..462c07959f 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -18045,9 +18045,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr case ZigTypeIdNull: case ZigTypeIdArgTuple: case ZigTypeIdOpaque: - result = create_const_vals(1); - result->special = ConstValSpecialStatic; - result->type = ira->codegen->builtin_types.entry_void; + result = &ira->codegen->const_void_val; break; case ZigTypeIdInt: { From 2d7f0ca387898fa6fb7da7946e4ecff45058b99c Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 8 Mar 2019 12:52:21 +0100 Subject: [PATCH 4/7] fixed enum to union code --- src/ir.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ir.cpp b/src/ir.cpp index 462c07959f..486a8ec4f3 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -10576,6 +10576,9 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so result->value.special = ConstValSpecialStatic; result->value.type = wanted_type; bigint_init_bigint(&result->value.data.x_union.tag, &val->data.x_enum_tag); + result->value.data.x_union.payload = create_const_vals(1); + result->value.data.x_union.payload->special = ConstValSpecialStatic; + result->value.data.x_union.payload->type = union_field->type_entry; return result; } From 357813b1930579af2ba3b5004131ac9f3f80ba57 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 8 Mar 2019 13:42:55 +0100 Subject: [PATCH 5/7] added tests --- test/stage1/behavior/type_info.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index 12a7c118b7..3eb91d534f 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -299,3 +299,17 @@ test "type info: optional field unwrapping" { _ = field.offset orelse 0; } + +test "type info: pass to function" { + _ = passTypeInfo(@typeInfo(void)); + _ = comptime passTypeInfo(@typeInfo(void)); +} + +fn passTypeInfo(comptime info: TypeInfo) type { + return void; +} + +test "type info: TypeId -> TypeInfo impl cast" { + _ = passTypeInfo(TypeId.Void); + _ = comptime passTypeInfo(TypeId.Void); +} From bcbb2650c5a7dca900861cc23f60374d29d50ec4 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Fri, 8 Mar 2019 17:50:42 +0100 Subject: [PATCH 6/7] check for type_has_one_possible_value and added correct caching to TypeInfo --- src/ir.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 486a8ec4f3..e8306913f9 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -10560,18 +10560,25 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so assert(union_field != nullptr); if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown))) return ira->codegen->invalid_instruction; - if (type_has_bits(union_field->type_entry)) { - AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at( - union_field->enum_field->decl_index); - ErrorMsg *msg = ir_add_error(ira, source_instr, - buf_sprintf("cast to union '%s' must initialize '%s' field '%s'", - buf_ptr(&wanted_type->name), - buf_ptr(&union_field->type_entry->name), - buf_ptr(union_field->name))); - add_error_note(ira->codegen, msg, field_node, - buf_sprintf("field '%s' declared here", buf_ptr(union_field->name))); - return ira->codegen->invalid_instruction; + + switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) { + case OnePossibleValueNo: + case OnePossibleValueInvalid: { + AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at( + union_field->enum_field->decl_index); + ErrorMsg *msg = ir_add_error(ira, source_instr, + buf_sprintf("cast to union '%s' must initialize '%s' field '%s'", + buf_ptr(&wanted_type->name), + buf_ptr(&union_field->type_entry->name), + buf_ptr(union_field->name))); + add_error_note(ira->codegen, msg, field_node, + buf_sprintf("field '%s' declared here", buf_ptr(union_field->name))); + return ira->codegen->invalid_instruction; + } + case OnePossibleValueYes: + break; } + IrInstruction *result = ir_const(ira, source_instr, wanted_type); result->value.special = ConstValSpecialStatic; result->value.type = wanted_type; @@ -18034,6 +18041,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown))) return err; + auto entry = ira->codegen->type_info_cache.maybe_get(type_entry); + if (entry != nullptr) { + *out = entry->value; + return ErrorNone; + } + ConstExprValue *result = nullptr; switch (type_entry->id) { case ZigTypeIdInvalid: From 1d5e349a52de9f4d056d50a1e41b89e7b0184a05 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 11 Mar 2019 11:09:15 -0400 Subject: [PATCH 7/7] ir: fix handling of OnePossibleValueInvalid this is the poison value which means a compile error has already been reported --- src/ir.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index e8306913f9..7d5a5305b1 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -10562,8 +10562,9 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so return ira->codegen->invalid_instruction; switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) { - case OnePossibleValueNo: - case OnePossibleValueInvalid: { + case OnePossibleValueInvalid: + return ira->codegen->invalid_instruction; + case OnePossibleValueNo: { AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at( union_field->enum_field->decl_index); ErrorMsg *msg = ir_add_error(ira, source_instr,