authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-27 17:11:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-27 17:13:34-04:00
log009e90f446ec7a81d602906fda9b17a6af1859f5
treecebcac299214f9db88acd0015bafe26b7d4be657
parent2f2215c9f46f0a75ce65803af84984b4a5f3741c
signaturelock-open Commit is signed but in an unrecognized format.

fix @typeInfo unable to distinguish compile error vs no-payload

closes #1421 closes #1426

2 files changed, 82 insertions(+), 68 deletions(-)

src/ir.cpp+65-68
......@@ -14444,8 +14444,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1444414444 ConstExprValue *payload_val = union_val->data.x_union.payload;
1444514445
1444614446 TypeTableEntry *field_type = field->type_entry;
14447 if (field_type->id == TypeTableEntryIdVoid)
14448 {
14447 if (field_type->id == TypeTableEntryIdVoid) {
1444914448 assert(payload_val == nullptr);
1445014449 payload_val = create_const_vals(1);
1445114450 payload_val->special = ConstValSpecialStatic;
......@@ -16797,12 +16796,11 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
1679716796 return var->value->data.x_type;
1679816797}
1679916798
16800static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope)
16801{
16799static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope) {
1680216800 Error err;
1680316801 TypeTableEntry *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
1680416802 if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))
16805 return false;
16803 return err;
1680616804
1680716805 ensure_field_index(type_info_definition_type, "name", 0);
1680816806 ensure_field_index(type_info_definition_type, "is_pub", 1);
......@@ -16810,38 +16808,33 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1681016808
1681116809 TypeTableEntry *type_info_definition_data_type = ir_type_info_get_type(ira, "Data", type_info_definition_type);
1681216810 if ((err = ensure_complete_type(ira->codegen, type_info_definition_data_type)))
16813 return false;
16811 return err;
1681416812
1681516813 TypeTableEntry *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_type);
1681616814 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_type)))
16817 return false;
16815 return err;
1681816816
1681916817 TypeTableEntry *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_type);
1682016818 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_inline_type)))
16821 return false;
16819 return err;
1682216820
1682316821 // Loop through our definitions once to figure out how many definitions we will generate info for.
1682416822 auto decl_it = decls_scope->decl_table.entry_iterator();
1682516823 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
1682616824 int definition_count = 0;
1682716825
16828 while ((curr_entry = decl_it.next()) != nullptr)
16829 {
16826 while ((curr_entry = decl_it.next()) != nullptr) {
1683016827 // If the definition is unresolved, force it to be resolved again.
16831 if (curr_entry->value->resolution == TldResolutionUnresolved)
16832 {
16828 if (curr_entry->value->resolution == TldResolutionUnresolved) {
1683316829 resolve_top_level_decl(ira->codegen, curr_entry->value, false, curr_entry->value->source_node);
16834 if (curr_entry->value->resolution != TldResolutionOk)
16835 {
16836 return false;
16830 if (curr_entry->value->resolution != TldResolutionOk) {
16831 return ErrorSemanticAnalyzeFail;
1683716832 }
1683816833 }
1683916834
1684016835 // Skip comptime blocks and test functions.
16841 if (curr_entry->value->id != TldIdCompTime)
16842 {
16843 if (curr_entry->value->id == TldIdFn)
16844 {
16836 if (curr_entry->value->id != TldIdCompTime) {
16837 if (curr_entry->value->id == TldIdFn) {
1684516838 FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
1684616839 if (fn_entry->is_test)
1684716840 continue;
......@@ -16863,13 +16856,11 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1686316856 decl_it = decls_scope->decl_table.entry_iterator();
1686416857 curr_entry = nullptr;
1686516858 int definition_index = 0;
16866 while ((curr_entry = decl_it.next()) != nullptr)
16867 {
16859 while ((curr_entry = decl_it.next()) != nullptr) {
1686816860 // Skip comptime blocks and test functions.
16869 if (curr_entry->value->id == TldIdCompTime)
16861 if (curr_entry->value->id == TldIdCompTime) {
1687016862 continue;
16871 else if (curr_entry->value->id == TldIdFn)
16872 {
16863 } else if (curr_entry->value->id == TldIdFn) {
1687316864 FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
1687416865 if (fn_entry->is_test)
1687516866 continue;
......@@ -16892,13 +16883,12 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1689216883 inner_fields[2].data.x_union.parent.data.p_struct.struct_val = definition_val;
1689316884 inner_fields[2].data.x_union.parent.data.p_struct.field_index = 1;
1689416885
16895 switch (curr_entry->value->id)
16896 {
16886 switch (curr_entry->value->id) {
1689716887 case TldIdVar:
1689816888 {
1689916889 VariableTableEntry *var = ((TldVar *)curr_entry->value)->var;
1690016890 if ((err = ensure_complete_type(ira->codegen, var->value->type)))
16901 return false;
16891 return ErrorSemanticAnalyzeFail;
1690216892
1690316893 if (var->value->type->id == TypeTableEntryIdMetaType)
1690416894 {
......@@ -17029,7 +17019,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1702917019 {
1703017020 TypeTableEntry *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
1703117021 if ((err = ensure_complete_type(ira->codegen, type_entry)))
17032 return false;
17022 return ErrorSemanticAnalyzeFail;
1703317023
1703417024 // This is a type.
1703517025 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);
......@@ -17051,7 +17041,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1705117041 }
1705217042
1705317043 assert(definition_index == definition_count);
17054 return true;
17044 return ErrorNone;
1705517045}
1705617046
1705717047static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, TypeTableEntry *ptr_type_entry) {
......@@ -17109,30 +17099,31 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, TypeTableEntry
1710917099 return result;
1711017100};
1711117101
17112static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry) {
17113 Error err;
17114 assert(type_entry != nullptr);
17115 assert(!type_is_invalid(type_entry));
17102static void make_enum_field_val(IrAnalyze *ira, ConstExprValue *enum_field_val, TypeEnumField *enum_field,
17103 TypeTableEntry *type_info_enum_field_type)
17104{
17105 enum_field_val->special = ConstValSpecialStatic;
17106 enum_field_val->type = type_info_enum_field_type;
1711617107
17117 if ((err = ensure_complete_type(ira->codegen, type_entry)))
17118 return nullptr;
17108 ConstExprValue *inner_fields = create_const_vals(2);
17109 inner_fields[1].special = ConstValSpecialStatic;
17110 inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
1711917111
17120 const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field,
17121 TypeTableEntry *type_info_enum_field_type) {
17122 enum_field_val->special = ConstValSpecialStatic;
17123 enum_field_val->type = type_info_enum_field_type;
17112 ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
17113 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
1712417114
17125 ConstExprValue *inner_fields = create_const_vals(2);
17126 inner_fields[1].special = ConstValSpecialStatic;
17127 inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
17115 bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
1712817116
17129 ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
17130 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
17117 enum_field_val->data.x_struct.fields = inner_fields;
17118}
1713117119
17132 bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
17120static Error ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry, ConstExprValue **out) {
17121 Error err;
17122 assert(type_entry != nullptr);
17123 assert(!type_is_invalid(type_entry));
1713317124
17134 enum_field_val->data.x_struct.fields = inner_fields;
17135 };
17125 if ((err = ensure_complete_type(ira->codegen, type_entry)))
17126 return err;
1713617127
1713717128 if (type_entry == ira->codegen->builtin_types.entry_global_error_set) {
1713817129 zig_panic("TODO implement @typeInfo for global error set");
......@@ -17155,13 +17146,16 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1715517146 case TypeTableEntryIdBlock:
1715617147 case TypeTableEntryIdArgTuple:
1715717148 case TypeTableEntryIdOpaque:
17158 return nullptr;
17149 *out = nullptr;
17150 return ErrorNone;
1715917151 default:
1716017152 {
1716117153 // Lookup an available value in our cache.
1716217154 auto entry = ira->codegen->type_info_cache.maybe_get(type_entry);
17163 if (entry != nullptr)
17164 return entry->value;
17155 if (entry != nullptr) {
17156 *out = entry->value;
17157 return ErrorNone;
17158 }
1716517159
1716617160 // Fallthrough if we don't find one.
1716717161 }
......@@ -17312,15 +17306,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1731217306 {
1731317307 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
1731417308 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
17315 make_enum_field_val(enum_field_val, enum_field, type_info_enum_field_type);
17309 make_enum_field_val(ira, enum_field_val, enum_field, type_info_enum_field_type);
1731617310 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
1731717311 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
1731817312 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
1731917313 }
1732017314 // defs: []TypeInfo.Definition
1732117315 ensure_field_index(result->type, "defs", 3);
17322 if (!ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope))
17323 return nullptr;
17316 if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope)))
17317 return err;
1732417318
1732517319 break;
1732617320 }
......@@ -17346,8 +17340,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1734617340 error_array->data.x_array.s_none.elements = create_const_vals(error_count);
1734717341
1734817342 init_const_slice(ira->codegen, &fields[0], error_array, 0, error_count, false);
17349 for (uint32_t error_index = 0; error_index < error_count; error_index++)
17350 {
17343 for (uint32_t error_index = 0; error_index < error_count; error_index++) {
1735117344 ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];
1735217345 ConstExprValue *error_val = &error_array->data.x_array.s_none.elements[error_index];
1735317346
......@@ -17425,9 +17418,9 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1742517418 tag_type->type = ira->codegen->builtin_types.entry_type;
1742617419 tag_type->data.x_type = type_entry->data.unionation.tag_type;
1742717420 fields[1].data.x_optional = tag_type;
17428 }
17429 else
17421 } else {
1743017422 fields[1].data.x_optional = nullptr;
17423 }
1743117424 // fields: []TypeInfo.UnionField
1743217425 ensure_field_index(result->type, "fields", 2);
1743317426
......@@ -17460,7 +17453,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1746017453 inner_fields[1].data.x_optional = nullptr;
1746117454 } else {
1746217455 inner_fields[1].data.x_optional = create_const_vals(1);
17463 make_enum_field_val(inner_fields[1].data.x_optional, union_field->enum_field, type_info_enum_field_type);
17456 make_enum_field_val(ira, inner_fields[1].data.x_optional, union_field->enum_field, type_info_enum_field_type);
1746417457 }
1746517458
1746617459 inner_fields[2].special = ConstValSpecialStatic;
......@@ -17477,8 +17470,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1747717470 }
1747817471 // defs: []TypeInfo.Definition
1747917472 ensure_field_index(result->type, "defs", 3);
17480 if (!ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope))
17481 return nullptr;
17473 if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope)))
17474 return err;
1748217475
1748317476 break;
1748417477 }
......@@ -17551,8 +17544,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1755117544 }
1755217545 // defs: []TypeInfo.Definition
1755317546 ensure_field_index(result->type, "defs", 2);
17554 if (!ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope))
17555 return nullptr;
17547 if ((err = ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope)))
17548 return err;
1755617549
1755717550 break;
1755817551 }
......@@ -17665,7 +17658,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1766517658 {
1766617659 TypeTableEntry *fn_type = type_entry->data.bound_fn.fn_type;
1766717660 assert(fn_type->id == TypeTableEntryIdFn);
17668 result = ir_make_type_info_value(ira, fn_type);
17661 if ((err = ir_make_type_info_value(ira, fn_type, &result)))
17662 return err;
1766917663
1767017664 break;
1767117665 }
......@@ -17673,12 +17667,14 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1767317667
1767417668 assert(result != nullptr);
1767517669 ira->codegen->type_info_cache.put(type_entry, result);
17676 return result;
17670 *out = result;
17671 return ErrorNone;
1767717672}
1767817673
1767917674static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1768017675 IrInstructionTypeInfo *instruction)
1768117676{
17677 Error err;
1768217678 IrInstruction *type_value = instruction->type_value->other;
1768317679 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
1768417680 if (type_is_invalid(type_entry))
......@@ -17686,15 +17682,16 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1768617682
1768717683 TypeTableEntry *result_type = ir_type_info_get_type(ira, nullptr, nullptr);
1768817684
17685 ConstExprValue *payload;
17686 if ((err = ir_make_type_info_value(ira, type_entry, &payload)))
17687 return ira->codegen->builtin_types.entry_invalid;
17688
1768917689 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1769017690 out_val->type = result_type;
1769117691 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry));
17692
17693 ConstExprValue *payload = ir_make_type_info_value(ira, type_entry);
1769417692 out_val->data.x_union.payload = payload;
1769517693
17696 if (payload != nullptr)
17697 {
17694 if (payload != nullptr) {
1769817695 assert(payload->type->id == TypeTableEntryIdStruct);
1769917696 payload->data.x_struct.parent.id = ConstParentIdUnion;
1770017697 payload->data.x_struct.parent.data.p_union.union_val = out_val;
test/compile_errors.zig+17
......@@ -1,6 +1,23 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "@typeInfo causing depend on itself compile error",
6 \\const start = struct {
7 \\ fn crash() bug() {
8 \\ return bug;
9 \\ }
10 \\};
11 \\fn bug() void {
12 \\ _ = @typeInfo(start).Struct;
13 \\}
14 \\export fn entry() void {
15 \\ var boom = start.crash();
16 \\}
17 ,
18 ".tmp_source.zig:2:5: error: 'crash' depends on itself",
19 );
20
421 cases.add(
522 "@handle() called outside of function definition",
623 \\var handle_undef: promise = undefined;