authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-21 19:20:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-21 19:27:51-04:00
logefdbede7abf91d5fe2836d95987a83e95e9fcf8e
tree79c60d1fbcb793ad98a6efa6feab03fed118f7a2
parent81c441f8855d4c58f0b2ff86d3d007cf0bf395d3
signaturelock-open Commit is signed but in an unrecognized format.

breaking: remove field alignment kludge

This breaks behavior tests as well as compile error notes for generic function calls. However it introduces better circular dependency compile errors. The next step is to add Lazy Values to fix the regressions.

5 files changed, 201 insertions(+), 312 deletions(-)

src/all_types.hpp+10-19
......@@ -69,9 +69,9 @@ struct IrExecutable {
6969 IrExecutable *source_exec;
7070 IrAnalyze *analysis;
7171 Scope *begin_scope;
72 ErrorMsg *first_err_trace_msg;
7273 ZigList<Tld *> tld_list;
7374
74 bool invalid;
7575 bool is_inline;
7676 bool is_generic_instantiation;
7777 bool need_err_code_spill;
......@@ -1129,11 +1129,10 @@ struct ZigTypeStruct {
11291129 ResolveStatus resolve_status;
11301130
11311131 bool is_slice;
1132 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1133 bool reported_infinite_err;
11341132 // whether any of the fields require comptime
11351133 // known after ResolveStatusZeroBitsKnown
11361134 bool requires_comptime;
1135 bool resolve_loop_flag;
11371136};
11381137
11391138struct ZigTypeOptional {
......@@ -1155,26 +1154,20 @@ struct ZigTypeErrorSet {
11551154
11561155struct ZigTypeEnum {
11571156 AstNode *decl_node;
1158 ContainerLayout layout;
1159 uint32_t src_field_count;
11601157 TypeEnumField *fields;
1161 bool is_invalid; // true if any fields are invalid
11621158 ZigType *tag_int_type;
11631159
11641160 ScopeDecls *decls_scope;
11651161
1166 // set this flag temporarily to detect infinite loops
1167 bool embedded_in_current;
1168 bool reported_infinite_err;
1169 // whether we've finished resolving it
1170 bool complete;
1171
1172 bool zero_bits_loop_flag;
1173 bool zero_bits_known;
1174
11751162 LLVMValueRef name_function;
11761163
11771164 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;
1165 uint32_t src_field_count;
1166
1167 ContainerLayout layout;
1168 ResolveStatus resolve_status;
1169
1170 bool resolve_loop_flag;
11781171};
11791172
11801173uint32_t type_ptr_hash(const ZigType *ptr);
......@@ -1199,11 +1192,10 @@ struct ZigTypeUnion {
11991192 ResolveStatus resolve_status;
12001193
12011194 bool have_explicit_tag_type;
1202 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1203 bool reported_infinite_err;
12041195 // whether any of the fields require comptime
12051196 // the value is not valid until zero_bits_known == true
12061197 bool requires_comptime;
1198 bool resolve_loop_flag;
12071199};
12081200
12091201struct FnGenParamInfo {
......@@ -1715,6 +1707,7 @@ struct CodeGen {
17151707 //////////////////////////// Runtime State
17161708 LLVMModuleRef module;
17171709 ZigList<ErrorMsg*> errors;
1710 ErrorMsg *trace_err;
17181711 LLVMBuilderRef builder;
17191712 ZigLLVMDIBuilder *dbuilder;
17201713 ZigLLVMDICompileUnit *compile_unit;
......@@ -1767,7 +1760,6 @@ struct CodeGen {
17671760 ZigList<Tld *> resolve_queue;
17681761 size_t resolve_queue_index;
17691762 ZigList<TimeEvent> timing_events;
1770 ZigList<AstNode *> tld_ref_source_node_stack;
17711763 ZigList<ZigFn *> inline_fns;
17721764 ZigList<ZigFn *> test_fns;
17731765 ZigList<ErrorTableEntry *> errors_by_index;
......@@ -1852,7 +1844,6 @@ struct CodeGen {
18521844 ZigFn *main_fn;
18531845 ZigFn *panic_fn;
18541846 TldFn *panic_tld_fn;
1855 AstNode *root_export_decl;
18561847
18571848 WantPIC want_pic;
18581849 WantStackCheck want_stack_check;
src/analyze.cpp+118-225
......@@ -20,7 +20,7 @@
2020
2121static const size_t default_backward_branch_quota = 1000;
2222
23static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
23static Error ATTRIBUTE_MUST_USE resolve_struct_type(CodeGen *g, ZigType *struct_type);
2424
2525static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
2626static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
......@@ -271,6 +271,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
271271 return type_entry->data.structure.resolve_status >= status;
272272 case ZigTypeIdUnion:
273273 return type_entry->data.unionation.resolve_status >= status;
274 case ZigTypeIdEnum:
275 return type_entry->data.enumeration.resolve_status >= status;
274276 case ZigTypeIdFnFrame:
275277 switch (status) {
276278 case ResolveStatusInvalid:
......@@ -285,23 +287,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
285287 case ResolveStatusLLVMFull:
286288 return type_entry->llvm_type != nullptr;
287289 }
288 case ZigTypeIdEnum:
289 switch (status) {
290 case ResolveStatusUnstarted:
291 return true;
292 case ResolveStatusInvalid:
293 zig_unreachable();
294 case ResolveStatusZeroBitsKnown:
295 return type_entry->data.enumeration.zero_bits_known;
296 case ResolveStatusAlignmentKnown:
297 return type_entry->data.enumeration.zero_bits_known;
298 case ResolveStatusSizeKnown:
299 return type_entry->data.enumeration.complete;
300 case ResolveStatusLLVMFwdDecl:
301 case ResolveStatusLLVMFull:
302 return type_entry->llvm_di_type != nullptr;
303 }
304 zig_unreachable();
305290 case ZigTypeIdOpaque:
306291 return status < ResolveStatusSizeKnown;
307292 case ZigTypeIdMetaType:
......@@ -865,7 +850,7 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
865850 return table_entry->value;
866851 }
867852 if (fn_type_id->return_type != nullptr) {
868 if ((err = ensure_complete_type(g, fn_type_id->return_type)))
853 if ((err = type_resolve(g, fn_type_id->return_type, ResolveStatusSizeKnown)))
869854 return g->builtin_types.entry_invalid;
870855 assert(fn_type_id->return_type->id != ZigTypeIdOpaque);
871856 } else {
......@@ -1404,7 +1389,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
14041389 add_node_error(g, proto_node,
14051390 buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
14061391 return g->builtin_types.entry_invalid;
1407 //return get_generic_fn_type(g, &fn_type_id);
14081392 }
14091393
14101394 ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
......@@ -1490,7 +1474,7 @@ bool type_is_invalid(ZigType *type_entry) {
14901474 case ZigTypeIdUnion:
14911475 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
14921476 case ZigTypeIdEnum:
1493 return type_entry->data.enumeration.is_invalid;
1477 return type_entry->data.enumeration.resolve_status == ResolveStatusInvalid;
14941478 default:
14951479 return false;
14961480 }
......@@ -1602,9 +1586,8 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16021586 if (struct_type->data.structure.resolve_loop_flag) {
16031587 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
16041588 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1605 ErrorMsg *msg = add_node_error(g, decl_node,
1606 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
1607 emit_error_notes_for_ref_stack(g, msg);
1589 g->trace_err = add_node_error(g, decl_node,
1590 buf_sprintf("struct '%s' depends on its own size", buf_ptr(&struct_type->name)));
16081591 }
16091592 return ErrorSemanticAnalyzeFail;
16101593 }
......@@ -1728,14 +1711,13 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17281711 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
17291712 return ErrorNone;
17301713
1714 AstNode *decl_node = union_type->data.structure.decl_node;
1715
17311716 if (union_type->data.unionation.resolve_loop_flag) {
1732 if (!union_type->data.unionation.reported_infinite_err) {
1733 AstNode *decl_node = union_type->data.unionation.decl_node;
1734 union_type->data.unionation.reported_infinite_err = true;
1717 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
17351718 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1736 ErrorMsg *msg = add_node_error(g, decl_node,
1737 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
1738 emit_error_notes_for_ref_stack(g, msg);
1719 g->trace_err = add_node_error(g, decl_node,
1720 buf_sprintf("union '%s' depends on its own alignment", buf_ptr(&union_type->name)));
17391721 }
17401722 return ErrorSemanticAnalyzeFail;
17411723 }
......@@ -1752,13 +1734,12 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17521734 if (field->gen_index == UINT32_MAX)
17531735 continue;
17541736
1737 src_assert(field->type_entry != nullptr, decl_node);
1738
17551739 size_t this_field_align;
17561740 if (packed) {
17571741 // TODO: https://github.com/ziglang/zig/issues/1512
17581742 this_field_align = 1;
1759 // This is the same hack as resolve_struct_alignment. See the comment there.
1760 } else if (field->type_entry == nullptr) {
1761 this_field_align = g->builtin_types.entry_usize->abi_align;
17621743 } else {
17631744 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
17641745 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
......@@ -1839,12 +1820,10 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18391820 size_t union_size_in_bits = 0;
18401821
18411822 if (union_type->data.unionation.resolve_loop_flag) {
1842 if (!union_type->data.unionation.reported_infinite_err) {
1843 union_type->data.unionation.reported_infinite_err = true;
1823 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
18441824 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1845 ErrorMsg *msg = add_node_error(g, decl_node,
1846 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
1847 emit_error_notes_for_ref_stack(g, msg);
1825 g->trace_err = add_node_error(g, decl_node,
1826 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
18481827 }
18491828 return ErrorSemanticAnalyzeFail;
18501829 }
......@@ -1925,24 +1904,25 @@ static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) {
19251904static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19261905 assert(enum_type->id == ZigTypeIdEnum);
19271906
1928 if (enum_type->data.enumeration.is_invalid)
1907 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
19291908 return ErrorSemanticAnalyzeFail;
1930
1931 if (enum_type->data.enumeration.zero_bits_known)
1909 if (enum_type->data.enumeration.resolve_status >= ResolveStatusZeroBitsKnown)
19321910 return ErrorNone;
19331911
1934 if (enum_type->data.enumeration.zero_bits_loop_flag) {
1935 ErrorMsg *msg = add_node_error(g, enum_type->data.enumeration.decl_node,
1936 buf_sprintf("'%s' depends on itself", buf_ptr(&enum_type->name)));
1937 emit_error_notes_for_ref_stack(g, msg);
1938 enum_type->data.enumeration.is_invalid = true;
1912 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1913 assert(decl_node->type == NodeTypeContainerDecl);
1914
1915 if (enum_type->data.enumeration.resolve_loop_flag) {
1916 if (enum_type->data.enumeration.resolve_status != ResolveStatusInvalid) {
1917 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1918 g->trace_err = add_node_error(g, decl_node,
1919 buf_sprintf("circular dependency: whether enum '%s' has non-zero size",
1920 buf_ptr(&enum_type->name)));
1921 }
19391922 return ErrorSemanticAnalyzeFail;
19401923 }
19411924
1942 enum_type->data.enumeration.zero_bits_loop_flag = true;
1943
1944 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1945 assert(decl_node->type == NodeTypeContainerDecl);
1925 enum_type->data.enumeration.resolve_loop_flag = true;
19461926
19471927 assert(!enum_type->data.enumeration.fields);
19481928 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
......@@ -1951,9 +1931,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19511931
19521932 enum_type->data.enumeration.src_field_count = field_count;
19531933 enum_type->data.enumeration.fields = nullptr;
1954 enum_type->data.enumeration.is_invalid = true;
1955 enum_type->data.enumeration.zero_bits_loop_flag = false;
1956 enum_type->data.enumeration.zero_bits_known = true;
1934 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19571935 return ErrorSemanticAnalyzeFail;
19581936 }
19591937
......@@ -1982,14 +1960,14 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19821960 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
19831961 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
19841962 if (type_is_invalid(wanted_tag_int_type)) {
1985 enum_type->data.enumeration.is_invalid = true;
1963 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19861964 } else if (wanted_tag_int_type->id != ZigTypeIdInt) {
1987 enum_type->data.enumeration.is_invalid = true;
1965 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19881966 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
19891967 buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));
19901968 } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern &&
19911969 !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) {
1992 enum_type->data.enumeration.is_invalid = true;
1970 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19931971 ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr,
19941972 buf_sprintf("'%s' is not a valid tag type for an extern enum",
19951973 buf_ptr(&wanted_tag_int_type->name)));
......@@ -2029,7 +2007,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20292007 ErrorMsg *msg = add_node_error(g, field_node,
20302008 buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name)));
20312009 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2032 enum_type->data.enumeration.is_invalid = true;
2010 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20332011 continue;
20342012 }
20352013
......@@ -2039,7 +2017,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20392017 // A user-specified value is available
20402018 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
20412019 if (type_is_invalid(result->type)) {
2042 enum_type->data.enumeration.is_invalid = true;
2020 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20432021 continue;
20442022 }
20452023
......@@ -2060,7 +2038,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20602038 if (!bigint_fits_in_bits(&type_enum_field->value,
20612039 tag_int_type->size_in_bits,
20622040 tag_int_type->data.integral.is_signed)) {
2063 enum_type->data.enumeration.is_invalid = true;
2041 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20642042
20652043 Buf *val_buf = buf_alloc();
20662044 bigint_append_buf(val_buf, &type_enum_field->value, 10);
......@@ -2075,7 +2053,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20752053 // Make sure the value is unique
20762054 auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node);
20772055 if (entry != nullptr) {
2078 enum_type->data.enumeration.is_invalid = true;
2056 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20792057
20802058 Buf *val_buf = buf_alloc();
20812059 bigint_append_buf(val_buf, &type_enum_field->value, 10);
......@@ -2089,13 +2067,12 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20892067 last_enum_field = type_enum_field;
20902068 }
20912069
2092 enum_type->data.enumeration.zero_bits_loop_flag = false;
2093 enum_type->data.enumeration.zero_bits_known = true;
2094 enum_type->data.enumeration.complete = true;
2095
2096 if (enum_type->data.enumeration.is_invalid)
2070 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
20972071 return ErrorSemanticAnalyzeFail;
20982072
2073 enum_type->data.enumeration.resolve_loop_flag = false;
2074 enum_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
2075
20992076 return ErrorNone;
21002077}
21012078
......@@ -2113,12 +2090,13 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
21132090 assert(decl_node->type == NodeTypeContainerDecl);
21142091
21152092 if (struct_type->data.structure.resolve_loop_flag) {
2116 // TODO This is a problem. I believe it can be solved with lazy values.
2117 struct_type->size_in_bits = SIZE_MAX;
2118 struct_type->abi_size = SIZE_MAX;
2119 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2120 struct_type->data.structure.resolve_loop_flag = false;
2121 return ErrorNone;
2093 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2095 g->trace_err = add_node_error(g, decl_node,
2096 buf_sprintf("circular dependency: whether struct '%s' has non-zero size",
2097 buf_ptr(&struct_type->name)));
2098 }
2099 return ErrorSemanticAnalyzeFail;
21222100 }
21232101
21242102 struct_type->data.structure.resolve_loop_flag = true;
......@@ -2237,9 +2215,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
22372215 if (struct_type->data.structure.resolve_loop_flag) {
22382216 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
22392217 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2240 ErrorMsg *msg = add_node_error(g, decl_node,
2241 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
2242 emit_error_notes_for_ref_stack(g, msg);
2218 g->trace_err = add_node_error(g, decl_node,
2219 buf_sprintf("struct '%s' depends on its own alignment", buf_ptr(&struct_type->name)));
22432220 }
22442221 return ErrorSemanticAnalyzeFail;
22452222 }
......@@ -2255,20 +2232,12 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
22552232 if (field->gen_index == SIZE_MAX)
22562233 continue;
22572234
2235 src_assert(field->type_entry != nullptr, decl_node);
2236
22582237 size_t this_field_align;
22592238 if (packed) {
22602239 // TODO: https://github.com/ziglang/zig/issues/1512
22612240 this_field_align = 1;
2262 // TODO If we have no type_entry for the field, we've already failed to
2263 // compile the program correctly. This stage1 compiler needs a deeper
2264 // reworking to make this correct, or we can ignore the problem
2265 // and make sure it is fixed in stage2. This workaround is for when
2266 // there is a false positive of a dependency loop, of alignment depending
2267 // on itself. When this false positive happens we assume a pointer-aligned
2268 // field, which is usually fine but could be incorrectly over-aligned or
2269 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2270 } else if (field->type_entry == nullptr) {
2271 this_field_align = g->builtin_types.entry_usize->abi_align;
22722241 } else {
22732242 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
22742243 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
......@@ -2304,23 +2273,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
23042273 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
23052274 return ErrorNone;
23062275
2276 AstNode *decl_node = union_type->data.unionation.decl_node;
2277 assert(decl_node->type == NodeTypeContainerDecl);
2278
23072279 if (union_type->data.unionation.resolve_loop_flag) {
2308 // If we get here it's due to recursion. From this we conclude that the struct is
2309 // not zero bits.
2310 // TODO actually it could still be zero bits. Here we should continue analyzing
2311 // the union from the next field index.
2312 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2313 union_type->data.unionation.resolve_loop_flag = false;
2314 union_type->abi_size = SIZE_MAX;
2315 union_type->size_in_bits = SIZE_MAX;
2316 return ErrorNone;
2280 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
2281 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2282 g->trace_err = add_node_error(g, decl_node,
2283 buf_sprintf("circular dependency: whether union '%s' has non-zero size",
2284 buf_ptr(&union_type->name)));
2285 }
2286 return ErrorSemanticAnalyzeFail;
23172287 }
23182288
23192289 union_type->data.unionation.resolve_loop_flag = true;
23202290
2321 AstNode *decl_node = union_type->data.unionation.decl_node;
2322 assert(decl_node->type == NodeTypeContainerDecl);
2323
23242291 assert(union_type->data.unionation.fields == nullptr);
23252292 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
23262293 if (field_count == 0) {
......@@ -2380,14 +2347,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
23802347 tag_type->size_in_bits = tag_int_type->size_in_bits;
23812348
23822349 tag_type->data.enumeration.tag_int_type = tag_int_type;
2383 tag_type->data.enumeration.zero_bits_known = true;
2350 tag_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
23842351 tag_type->data.enumeration.decl_node = decl_node;
23852352 tag_type->data.enumeration.layout = ContainerLayoutAuto;
23862353 tag_type->data.enumeration.src_field_count = field_count;
23872354 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
23882355 tag_type->data.enumeration.fields_by_name.init(field_count);
23892356 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;
2390 tag_type->data.enumeration.complete = true;
23912357 } else if (enum_type_node != nullptr) {
23922358 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
23932359 if (type_is_invalid(enum_type)) {
......@@ -3185,9 +3151,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
31853151 ZigType *explicit_type = nullptr;
31863152 if (var_decl->type) {
31873153 if (tld_var->analyzing_type) {
3188 ErrorMsg *msg = add_node_error(g, var_decl->type,
3154 g->trace_err = add_node_error(g, var_decl->type,
31893155 buf_sprintf("type of '%s' depends on itself", buf_ptr(tld_var->base.name)));
3190 emit_error_notes_for_ref_stack(g, msg);
31913156 explicit_type = g->builtin_types.entry_invalid;
31923157 } else {
31933158 tld_var->analyzing_type = true;
......@@ -3386,7 +3351,6 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
33863351
33873352 assert(tld->resolution != TldResolutionResolving);
33883353 tld->resolution = TldResolutionResolving;
3389 g->tld_ref_source_node_stack.append(source_node);
33903354
33913355 switch (tld->id) {
33923356 case TldIdVar:
......@@ -3424,7 +3388,10 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
34243388 }
34253389
34263390 tld->resolution = TldResolutionOk;
3427 g->tld_ref_source_node_stack.pop();
3391
3392 if (g->trace_err != nullptr && source_node != nullptr) {
3393 g->trace_err = add_error_note(g, g->trace_err, source_node, buf_create_from_str("referenced here"));
3394 }
34283395}
34293396
34303397Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name) {
......@@ -3550,7 +3517,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag)
35503517}
35513518
35523519TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) {
3553 assert(enum_type->data.enumeration.zero_bits_known);
3520 assert(type_is_resolved(enum_type, ResolveStatusZeroBitsKnown));
35543521 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
35553522 TypeEnumField *field = &enum_type->data.enumeration.fields[i];
35563523 if (bigint_cmp(&field->value, tag) == CmpEQ) {
......@@ -3619,43 +3586,6 @@ ZigType *container_ref_type(ZigType *type_entry) {
36193586 type_entry->data.pointer.child_type : type_entry;
36203587}
36213588
3622Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
3623 switch (type_entry->id) {
3624 case ZigTypeIdStruct:
3625 return resolve_struct_type(g, type_entry);
3626 case ZigTypeIdEnum:
3627 return resolve_enum_zero_bits(g, type_entry);
3628 case ZigTypeIdUnion:
3629 return resolve_union_type(g, type_entry);
3630 case ZigTypeIdPointer:
3631 case ZigTypeIdMetaType:
3632 case ZigTypeIdVoid:
3633 case ZigTypeIdBool:
3634 case ZigTypeIdUnreachable:
3635 case ZigTypeIdInt:
3636 case ZigTypeIdFloat:
3637 case ZigTypeIdArray:
3638 case ZigTypeIdComptimeFloat:
3639 case ZigTypeIdComptimeInt:
3640 case ZigTypeIdEnumLiteral:
3641 case ZigTypeIdUndefined:
3642 case ZigTypeIdNull:
3643 case ZigTypeIdOptional:
3644 case ZigTypeIdErrorUnion:
3645 case ZigTypeIdErrorSet:
3646 case ZigTypeIdFn:
3647 case ZigTypeIdBoundFn:
3648 case ZigTypeIdInvalid:
3649 case ZigTypeIdArgTuple:
3650 case ZigTypeIdOpaque:
3651 case ZigTypeIdVector:
3652 case ZigTypeIdFnFrame:
3653 case ZigTypeIdAnyFrame:
3654 zig_unreachable();
3655 }
3656 zig_unreachable();
3657}
3658
36593589ZigType *get_src_ptr_type(ZigType *type) {
36603590 if (type->id == ZigTypeIdPointer) return type;
36613591 if (type->id == ZigTypeIdFn) return type;
......@@ -3906,7 +3836,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
39063836 &fn->analyzed_executable, fn_type_id->return_type, return_type_node);
39073837 fn->src_implicit_return_type = block_return_type;
39083838
3909 if (type_is_invalid(block_return_type) || fn->analyzed_executable.invalid) {
3839 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
39103840 assert(g->errors.length > 0);
39113841 fn->anal_state = FnAnalStateInvalid;
39123842 return;
......@@ -3990,7 +3920,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
39903920 assert(!fn_type->data.fn.is_generic);
39913921
39923922 ir_gen_fn(g, fn_table_entry);
3993 if (fn_table_entry->ir_executable.invalid) {
3923 if (fn_table_entry->ir_executable.first_err_trace_msg != nullptr) {
39943924 fn_table_entry->anal_state = FnAnalStateInvalid;
39953925 return;
39963926 }
......@@ -4128,12 +4058,14 @@ void semantic_analyze(CodeGen *g) {
41284058 {
41294059 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
41304060 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
4061 g->trace_err = nullptr;
41314062 AstNode *source_node = nullptr;
41324063 resolve_top_level_decl(g, tld, source_node);
41334064 }
41344065
41354066 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
41364067 ZigFn *fn_entry = g->fn_defs.at(g->fn_defs_index);
4068 g->trace_err = nullptr;
41374069 analyze_fn_body(g, fn_entry);
41384070 }
41394071 }
......@@ -4145,6 +4077,7 @@ void semantic_analyze(CodeGen *g) {
41454077 // second pass over functions for detecting async
41464078 for (g->fn_defs_index = 0; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
41474079 ZigFn *fn = g->fn_defs.at(g->fn_defs_index);
4080 g->trace_err = nullptr;
41484081 analyze_fn_async(g, fn, true);
41494082 if (fn_is_async(fn) && fn->non_async_node != nullptr) {
41504083 ErrorMsg *msg = add_node_error(g, fn->proto_node,
......@@ -5143,34 +5076,6 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
51435076}
51445077
51455078
5146void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
5147 Error err;
5148 ZigType *wanted_type = const_val->type;
5149 if (wanted_type->id == ZigTypeIdArray) {
5150 const_val->special = ConstValSpecialStatic;
5151 const_val->data.x_array.special = ConstArraySpecialUndef;
5152 } else if (wanted_type->id == ZigTypeIdStruct) {
5153 if ((err = ensure_complete_type(g, wanted_type))) {
5154 return;
5155 }
5156
5157 const_val->special = ConstValSpecialStatic;
5158 size_t field_count = wanted_type->data.structure.src_field_count;
5159 const_val->data.x_struct.fields = create_const_vals(field_count);
5160 for (size_t i = 0; i < field_count; i += 1) {
5161 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
5162 field_val->type = wanted_type->data.structure.fields[i].type_entry;
5163 assert(field_val->type);
5164 init_const_undefined(g, field_val);
5165 field_val->parent.id = ConstParentIdStruct;
5166 field_val->parent.data.p_struct.struct_val = const_val;
5167 field_val->parent.data.p_struct.field_index = i;
5168 }
5169 } else {
5170 const_val->special = ConstValSpecialUndef;
5171 }
5172}
5173
51745079ConstExprValue *create_const_vals(size_t count) {
51755080 ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count);
51765081 ConstExprValue *vals = allocate<ConstExprValue>(count);
......@@ -5180,10 +5085,6 @@ ConstExprValue *create_const_vals(size_t count) {
51805085 return vals;
51815086}
51825087
5183Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {
5184 return type_resolve(g, type_entry, ResolveStatusSizeKnown);
5185}
5186
51875088static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
51885089 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)
51895090 return orig_fn_type;
......@@ -5197,27 +5098,6 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
51975098 return fn_type;
51985099}
51995100
5200static void emit_error_notes_for_type_loop(CodeGen *g, ErrorMsg *msg, ZigType *stop_type,
5201 ZigType *ty, AstNode *src_node)
5202{
5203 ErrorMsg *note = add_error_note(g, msg, src_node,
5204 buf_sprintf("when analyzing type '%s' here", buf_ptr(&ty->name)));
5205 if (ty == stop_type)
5206 return;
5207 switch (ty->id) {
5208 case ZigTypeIdFnFrame: {
5209 ty->data.frame.reported_loop_err = true;
5210 ZigType *depending_type = ty->data.frame.resolve_loop_type;
5211 if (depending_type == nullptr)
5212 return;
5213 emit_error_notes_for_type_loop(g, note, stop_type,
5214 depending_type, ty->data.frame.resolve_loop_src_node);
5215 }
5216 default:
5217 return;
5218 }
5219}
5220
52215101static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52225102 Error err;
52235103
......@@ -5230,13 +5110,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52305110 if (frame_type->data.frame.resolve_loop_type != nullptr) {
52315111 if (!frame_type->data.frame.reported_loop_err) {
52325112 frame_type->data.frame.reported_loop_err = true;
5233 ErrorMsg *msg = add_node_error(g, fn->proto_node,
5113 g->trace_err = add_node_error(g, fn->proto_node,
52345114 buf_sprintf("'%s' depends on itself", buf_ptr(&frame_type->name)));
5235 emit_error_notes_for_type_loop(g, msg,
5236 frame_type,
5237 frame_type->data.frame.resolve_loop_type,
5238 frame_type->data.frame.resolve_loop_src_node);
5239 emit_error_notes_for_ref_stack(g, msg);
52405115 }
52415116 return ErrorSemanticAnalyzeFail;
52425117 }
......@@ -5252,11 +5127,9 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52525127 return ErrorSemanticAnalyzeFail;
52535128 break;
52545129 case FnAnalStateProbing: {
5255 ErrorMsg *msg = add_node_error(g, fn->proto_node,
5130 g->trace_err = add_node_error(g, fn->proto_node,
52565131 buf_sprintf("cannot resolve '%s': function not fully analyzed yet",
52575132 buf_ptr(&frame_type->name)));
5258 ir_add_analysis_trace(fn->ir_executable.analysis, msg,
5259 buf_sprintf("depends on its own frame here"));
52605133 return ErrorSemanticAnalyzeFail;
52615134 }
52625135 }
......@@ -5327,10 +5200,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
53275200 if (callee->anal_state == FnAnalStateProbing) {
53285201 ErrorMsg *msg = add_node_error(g, fn->proto_node,
53295202 buf_sprintf("unable to determine async function frame of '%s'", buf_ptr(&fn->symbol_name)));
5330 ErrorMsg *note = add_error_note(g, msg, call->base.source_node,
5203 g->trace_err = add_error_note(g, msg, call->base.source_node,
53315204 buf_sprintf("analysis of function '%s' depends on the frame", buf_ptr(&callee->symbol_name)));
5332 ir_add_analysis_trace(callee->ir_executable.analysis, note,
5333 buf_sprintf("depends on the frame here"));
53345205 return ErrorSemanticAnalyzeFail;
53355206 }
53365207
......@@ -6229,6 +6100,40 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
62296100 zig_unreachable();
62306101}
62316102
6103static void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
6104 Error err;
6105 ZigType *wanted_type = const_val->type;
6106 if (wanted_type->id == ZigTypeIdArray) {
6107 const_val->special = ConstValSpecialStatic;
6108 const_val->data.x_array.special = ConstArraySpecialUndef;
6109 } else if (wanted_type->id == ZigTypeIdStruct) {
6110 if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) {
6111 return;
6112 }
6113
6114 const_val->special = ConstValSpecialStatic;
6115 size_t field_count = wanted_type->data.structure.src_field_count;
6116 const_val->data.x_struct.fields = create_const_vals(field_count);
6117 for (size_t i = 0; i < field_count; i += 1) {
6118 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
6119 field_val->type = wanted_type->data.structure.fields[i].type_entry;
6120 assert(field_val->type);
6121 init_const_undefined(g, field_val);
6122 field_val->parent.id = ConstParentIdStruct;
6123 field_val->parent.data.p_struct.struct_val = const_val;
6124 field_val->parent.data.p_struct.field_index = i;
6125 }
6126 } else {
6127 const_val->special = ConstValSpecialUndef;
6128 }
6129}
6130
6131void expand_undef_struct(CodeGen *g, ConstExprValue *const_val) {
6132 if (const_val->special == ConstValSpecialUndef) {
6133 init_const_undefined(g, const_val);
6134 }
6135}
6136
62326137// Canonicalize the array value as ConstArraySpecialNone
62336138void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
62346139 size_t elem_count;
......@@ -6707,19 +6612,6 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) {
67076612 return false;
67086613}
67096614
6710void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg) {
6711 size_t i = g->tld_ref_source_node_stack.length;
6712 for (;;) {
6713 if (i == 0)
6714 break;
6715 i -= 1;
6716 AstNode *source_node = g->tld_ref_source_node_stack.at(i);
6717 if (source_node) {
6718 msg = add_error_note(g, msg, source_node, buf_sprintf("referenced here"));
6719 }
6720 }
6721}
6722
67236615Buf *type_bare_name(ZigType *type_entry) {
67246616 if (is_slice(type_entry)) {
67256617 return &type_entry->name;
......@@ -7122,10 +7014,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
71227014 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
71237015}
71247016
7125static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
7126 assert(!enum_type->data.enumeration.is_invalid);
7127 assert(enum_type->data.enumeration.complete);
7128 if (enum_type->llvm_di_type != nullptr) return;
7017static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatus wanted_resolve_status) {
7018 assert(enum_type->data.enumeration.resolve_status >= ResolveStatusSizeKnown);
7019 if (enum_type->data.enumeration.resolve_status >= wanted_resolve_status) return;
71297020
71307021 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
71317022 ZigType *import = get_scope_import(scope);
......@@ -7146,6 +7037,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
71467037 debug_align_in_bits,
71477038 ZigLLVM_DIFlags_Zero,
71487039 nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
7040 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
71497041 return;
71507042 }
71517043
......@@ -7178,6 +7070,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
71787070 get_llvm_di_type(g, tag_int_type), "");
71797071
71807072 enum_type->llvm_di_type = tag_di_type;
7073 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
71817074}
71827075
71837076static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
......@@ -7909,7 +7802,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
79097802 else
79107803 return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr);
79117804 case ZigTypeIdEnum:
7912 return resolve_llvm_types_enum(g, type);
7805 return resolve_llvm_types_enum(g, type, wanted_resolve_status);
79137806 case ZigTypeIdUnion:
79147807 return resolve_llvm_types_union(g, type, wanted_resolve_status);
79157808 case ZigTypeIdPointer:
src/analyze.hpp+1-5
......@@ -14,7 +14,6 @@ void semantic_analyze(CodeGen *g);
1414ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg);
1515ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);
1616ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg);
17void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg);
1817ZigType *new_type_table_entry(ZigTypeId id);
1918ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn);
2019ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);
......@@ -71,7 +70,6 @@ bool type_is_complete(ZigType *type_entry);
7170bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
7271bool type_is_invalid(ZigType *type_entry);
7372bool type_is_global_error_set(ZigType *err_set_type);
74Error resolve_container_type(CodeGen *g, ZigType *type_entry);
7573ScopeDecls *get_container_scope(ZigType *type_entry);
7674TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
7775TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);
......@@ -95,7 +93,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
9593ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
9694void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);
9795AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
98Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);
9996Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);
10097void complete_enum(CodeGen *g, ZigType *enum_type);
10198bool ir_get_var_is_comptime(ZigVar *var);
......@@ -169,12 +166,11 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t
169166void init_const_arg_tuple(CodeGen *g, ConstExprValue *const_val, size_t arg_index_start, size_t arg_index_end);
170167ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_index_end);
171168
172void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
173
174169ConstExprValue *create_const_vals(size_t count);
175170
176171ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
177172void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
173void expand_undef_struct(CodeGen *g, ConstExprValue *const_val);
178174void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
179175
180176const char *type_id_name(ZigTypeId id);
src/ir.cpp+72-61
......@@ -234,6 +234,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
234234 }
235235 case ConstPtrSpecialBaseStruct: {
236236 ConstExprValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val;
237 expand_undef_struct(g, struct_val);
237238 result = &struct_val->data.x_struct.fields[const_val->data.x_ptr.data.base_struct.field_index];
238239 break;
239240 }
......@@ -8111,7 +8112,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
81118112 ir_build_reset_result(irb, scope, node, result_loc);
81128113 }
81138114 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc);
8114 irb->exec->invalid = irb->exec->invalid || (result == irb->codegen->invalid_instruction);
8115 if (result == irb->codegen->invalid_instruction) {
8116 src_assert(irb->exec->first_err_trace_msg != nullptr, node);
8117 }
81158118 return result;
81168119}
81178120
......@@ -8119,21 +8122,20 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {
81198122 return ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
81208123}
81218124
8122static void invalidate_exec(IrExecutable *exec) {
8123 if (exec->invalid)
8125static void invalidate_exec(IrExecutable *exec, ErrorMsg *msg) {
8126 if (exec->first_err_trace_msg != nullptr)
81248127 return;
81258128
8126 exec->invalid = true;
8129 exec->first_err_trace_msg = msg;
81278130
81288131 for (size_t i = 0; i < exec->tld_list.length; i += 1) {
81298132 exec->tld_list.items[i]->resolution = TldResolutionInvalid;
81308133 }
81318134
81328135 if (exec->source_exec != nullptr)
8133 invalidate_exec(exec->source_exec);
8136 invalidate_exec(exec->source_exec, msg);
81348137}
81358138
8136
81378139bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {
81388140 assert(node->owner);
81398141
......@@ -8151,8 +8153,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
81518153
81528154 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
81538155 assert(result);
8154 if (irb->exec->invalid)
8156 if (irb->exec->first_err_trace_msg != nullptr) {
8157 codegen->trace_err = irb->exec->first_err_trace_msg;
81558158 return false;
8159 }
81568160
81578161 if (!instr_is_unreachable(result)) {
81588162 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->source_node, result));
......@@ -8174,25 +8178,9 @@ bool ir_gen_fn(CodeGen *codegen, ZigFn *fn_entry) {
81748178 return ir_gen(codegen, body_node, fn_entry->child_scope, ir_executable);
81758179}
81768180
8177static void ir_add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg *err_msg, int limit) {
8178 if (!exec || !exec->source_node || limit < 0) return;
8179 add_error_note(codegen, err_msg, exec->source_node, buf_sprintf("called from here"));
8180
8181 ir_add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
8182}
8183
8184void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text) {
8185 IrInstruction *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
8186 add_error_note(ira->codegen, err_msg, old_instruction->source_node, text);
8187 ir_add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);
8188}
8189
81908181static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
8191 invalidate_exec(exec);
81928182 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
8193 if (exec->parent_exec) {
8194 ir_add_call_stack_errors(codegen, exec, err_msg, 10);
8195 }
8183 invalidate_exec(exec, err_msg);
81968184 return err_msg;
81978185}
81988186
......@@ -10634,7 +10622,7 @@ static void ir_finish_bb(IrAnalyze *ira) {
1063410622
1063510623static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
1063610624 ira->old_bb_index = SIZE_MAX;
10637 ira->new_irb.exec->invalid = true;
10625 assert(ira->new_irb.exec->first_err_trace_msg != nullptr);
1063810626 return ira->codegen->unreach_instruction;
1063910627}
1064010628
......@@ -10761,8 +10749,11 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1076110749 ir_executable->begin_scope = scope;
1076210750 ir_gen(codegen, node, scope, ir_executable);
1076310751
10764 if (ir_executable->invalid)
10752 if (ir_executable->first_err_trace_msg != nullptr) {
10753 codegen->trace_err = add_error_note(codegen, ir_executable->first_err_trace_msg,
10754 source_node, buf_create_from_str("called from here"));
1076510755 return &codegen->invalid_instruction->value;
10756 }
1076610757
1076710758 if (codegen->verbose_ir) {
1076810759 fprintf(stderr, "\nSource: ");
......@@ -10783,8 +10774,9 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1078310774 analyzed_executable->backward_branch_quota = backward_branch_quota;
1078410775 analyzed_executable->begin_scope = scope;
1078510776 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node);
10786 if (type_is_invalid(result_type))
10777 if (type_is_invalid(result_type)) {
1078710778 return &codegen->invalid_instruction->value;
10779 }
1078810780
1078910781 if (codegen->verbose_ir) {
1079010782 fprintf(stderr, "{ // (analyzed)\n");
......@@ -11322,7 +11314,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc
1132211314 IrInstruction *target, ZigType *wanted_type)
1132311315{
1132411316 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
11325 init_const_undefined(ira->codegen, &result->value);
11317 result->value.special = ConstValSpecialUndef;
1132611318 return result;
1132711319}
1132811320
......@@ -11461,7 +11453,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
1146111453
1146211454 ZigType *actual_type = target->value.type;
1146311455
11464 if ((err = ensure_complete_type(ira->codegen, wanted_type)))
11456 if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown)))
1146511457 return ira->codegen->invalid_instruction;
1146611458
1146711459 if (actual_type != wanted_type->data.enumeration.tag_int_type) {
......@@ -12719,7 +12711,9 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
1271912711 if (type_is_invalid(operand->value.type))
1272012712 return ir_unreach_error(ira);
1272112713
12722 if (!instr_is_comptime(operand) && handle_is_ptr(ira->explicit_return_type)) {
12714 if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr &&
12715 handle_is_ptr(ira->explicit_return_type))
12716 {
1272312717 // result location mechanism took care of it.
1272412718 IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
1272512719 instruction->base.source_node, nullptr);
......@@ -15513,8 +15507,9 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1551315507 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);
1551415508 }
1551515509
15516 if (type_is_invalid(result->type))
15510 if (type_is_invalid(result->type)) {
1551715511 return ira->codegen->invalid_instruction;
15512 }
1551815513 }
1551915514
1552015515 IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);
......@@ -16727,7 +16722,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1672716722 return ira->codegen->invalid_instruction;
1672816723
1672916724 bool safety_check_on = elem_ptr_instruction->safety_check_on;
16730 if ((err = ensure_complete_type(ira->codegen, return_type->data.pointer.child_type)))
16725 if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown)))
1673116726 return ira->codegen->invalid_instruction;
1673216727
1673316728 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
......@@ -17113,7 +17108,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1711317108 Error err;
1711417109
1711517110 ZigType *bare_type = container_ref_type(container_type);
17116 if ((err = ensure_complete_type(ira->codegen, bare_type)))
17111 if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusSizeKnown)))
1711717112 return ira->codegen->invalid_instruction;
1711817113
1711917114 assert(container_ptr->value.type->id == ZigTypeIdPointer);
......@@ -17243,15 +17238,15 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1724317238}
1724417239
1724517240static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
17246 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
17247 emit_error_notes_for_ref_stack(ira->codegen, msg);
17241 ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
1724817242 return ira->codegen->invalid_instruction;
1724917243}
1725017244
1725117245static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
1725217246 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
17253 if (tld->resolution == TldResolutionInvalid)
17247 if (tld->resolution == TldResolutionInvalid) {
1725417248 return ira->codegen->invalid_instruction;
17249 }
1725517250
1725617251 switch (tld->id) {
1725717252 case TldIdContainer:
......@@ -17396,7 +17391,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1739617391 return ira->codegen->invalid_instruction;
1739717392 } else if (is_container(child_type)) {
1739817393 if (child_type->id == ZigTypeIdEnum) {
17399 if ((err = ensure_complete_type(ira->codegen, child_type)))
17394 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1740017395 return ira->codegen->invalid_instruction;
1740117396
1740217397 TypeEnumField *field = find_enum_type_field(child_type, field_name);
......@@ -17425,7 +17420,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1742517420 (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
1742617421 child_type->data.unionation.decl_node->data.container_decl.auto_enum))
1742717422 {
17428 if ((err = ensure_complete_type(ira->codegen, child_type)))
17423 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1742917424 return ira->codegen->invalid_instruction;
1743017425 TypeUnionField *field = find_union_type_field(child_type, field_name);
1743117426 if (field) {
......@@ -18008,7 +18003,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1800818003 case ZigTypeIdFnFrame:
1800918004 case ZigTypeIdAnyFrame:
1801018005 {
18011 if ((err = ensure_complete_type(ira->codegen, child_type)))
18006 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1801218007 return ira->codegen->invalid_instruction;
1801318008 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
1801418009 return ir_const_type(ira, &array_type_instruction->base, result_type);
......@@ -18024,7 +18019,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
1802418019 IrInstruction *type_value = size_of_instruction->type_value->child;
1802518020 ZigType *type_entry = ir_resolve_type(ira, type_value);
1802618021
18027 if ((err = ensure_complete_type(ira->codegen, type_entry)))
18022 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1802818023 return ira->codegen->invalid_instruction;
1802918024
1803018025 switch (type_entry->id) {
......@@ -18529,7 +18524,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1852918524 if (pointee_val->special == ConstValSpecialRuntime)
1853018525 pointee_val = nullptr;
1853118526 }
18532 if ((err = ensure_complete_type(ira->codegen, target_type)))
18527 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusSizeKnown)))
1853318528 return ira->codegen->invalid_instruction;
1853418529
1853518530 switch (target_type->id) {
......@@ -19276,8 +19271,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
1927619271 if (!msg_buf)
1927719272 return ira->codegen->invalid_instruction;
1927819273
19279 ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf);
19280 emit_error_notes_for_ref_stack(ira->codegen, msg);
19274 ir_add_error(ira, &instruction->base, msg_buf);
1928119275
1928219276 return ira->codegen->invalid_instruction;
1928319277}
......@@ -19391,7 +19385,7 @@ static IrInstruction *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1939119385 return ira->codegen->invalid_instruction;
1939219386 }
1939319387
19394 if ((err = ensure_complete_type(ira->codegen, container_type)))
19388 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
1939519389 return ira->codegen->invalid_instruction;
1939619390
1939719391 TypeStructField *field = find_struct_type_field(container_type, field_name);
......@@ -19470,7 +19464,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
1947019464 return nullptr;
1947119465
1947219466 Error err;
19473 if ((err = ensure_complete_type(ira->codegen, container_type)))
19467 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
1947419468 return nullptr;
1947519469
1947619470 Buf *field_name = ir_resolve_str(ira, field_name_value);
......@@ -19574,7 +19568,7 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1957419568
1957519569 ZigVar *var = tld->var;
1957619570
19577 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))
19571 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
1957819572 return ira->codegen->builtin_types.entry_invalid;
1957919573
1958019574 assert(var->const_value->type->id == ZigTypeIdMetaType);
......@@ -19594,15 +19588,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1959419588 ensure_field_index(type_info_declaration_type, "data", 2);
1959519589
1959619590 ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type);
19597 if ((err = ensure_complete_type(ira->codegen, type_info_declaration_data_type)))
19591 if ((err = type_resolve(ira->codegen, type_info_declaration_data_type, ResolveStatusSizeKnown)))
1959819592 return err;
1959919593
1960019594 ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type);
19601 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_type)))
19595 if ((err = type_resolve(ira->codegen, type_info_fn_decl_type, ResolveStatusSizeKnown)))
1960219596 return err;
1960319597
1960419598 ZigType *type_info_fn_decl_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_decl_type);
19605 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_inline_type)))
19599 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))
1960619600 return err;
1960719601
1960819602 // Loop through our declarations once to figure out how many declarations we will generate info for.
......@@ -19673,7 +19667,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1967319667 case TldIdVar:
1967419668 {
1967519669 ZigVar *var = ((TldVar *)curr_entry->value)->var;
19676 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))
19670 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
1967719671 return ErrorSemanticAnalyzeFail;
1967819672
1967919673 if (var->const_value->type->id == ZigTypeIdMetaType) {
......@@ -19799,7 +19793,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1979919793 case TldIdContainer:
1980019794 {
1980119795 ZigType *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
19802 if ((err = ensure_complete_type(ira->codegen, type_entry)))
19796 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1980319797 return ErrorSemanticAnalyzeFail;
1980419798
1980519799 // This is a type.
......@@ -19855,7 +19849,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1985519849 return nullptr;
1985619850
1985719851 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
19858 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_type));
19852 assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown));
1985919853
1986019854 ConstExprValue *result = create_const_vals(1);
1986119855 result->special = ConstValSpecialStatic;
......@@ -19867,7 +19861,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1986719861 // size: Size
1986819862 ensure_field_index(result->type, "size", 0);
1986919863 ZigType *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type);
19870 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_size_type));
19864 assertNoError(type_resolve(ira->codegen, type_info_pointer_size_type, ResolveStatusSizeKnown));
1987119865 fields[0].special = ConstValSpecialStatic;
1987219866 fields[0].type = type_info_pointer_size_type;
1987319867 bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);
......@@ -22021,7 +22015,7 @@ static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInst
2202122015 return ira->codegen->invalid_instruction;
2202222016 ZigType *container_type = ir_resolve_type(ira, container);
2202322017
22024 if ((err = ensure_complete_type(ira->codegen, container_type)))
22018 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2202522019 return ira->codegen->invalid_instruction;
2202622020
2202722021 uint64_t result;
......@@ -22057,7 +22051,7 @@ static IrInstruction *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstr
2205722051 if (type_is_invalid(container_type))
2205822052 return ira->codegen->invalid_instruction;
2205922053
22060 if ((err = ensure_complete_type(ira->codegen, container_type)))
22054 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2206122055 return ira->codegen->invalid_instruction;
2206222056
2206322057
......@@ -22100,7 +22094,7 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr
2210022094 if (type_is_invalid(container_type))
2210122095 return ira->codegen->invalid_instruction;
2210222096
22103 if ((err = ensure_complete_type(ira->codegen, container_type)))
22097 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2210422098 return ira->codegen->invalid_instruction;
2210522099
2210622100 uint64_t member_index;
......@@ -23309,8 +23303,10 @@ static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExp
2330923303}
2331023304
2331123305static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) {
23312 if (val->special == ConstValSpecialUndef)
23306 if (val->special == ConstValSpecialUndef) {
23307 expand_undef_struct(codegen, val);
2331323308 val->special = ConstValSpecialStatic;
23309 }
2331423310 assert(val->special == ConstValSpecialStatic);
2331523311 switch (val->type->id) {
2331623312 case ZigTypeIdInvalid:
......@@ -23746,8 +23742,9 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2374623742 IrInstructionDeclRef *instruction)
2374723743{
2374823744 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);
23749 if (type_is_invalid(ref_instruction->value.type))
23745 if (type_is_invalid(ref_instruction->value.type)) {
2375023746 return ira->codegen->invalid_instruction;
23747 }
2375123748
2375223749 if (instruction->lval == LValPtr) {
2375323750 return ref_instruction;
......@@ -23954,7 +23951,7 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct
2395423951 return ira->codegen->invalid_instruction;
2395523952
2395623953 if (enum_type->id == ZigTypeIdEnum) {
23957 if ((err = ensure_complete_type(ira->codegen, enum_type)))
23954 if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown)))
2395823955 return ira->codegen->invalid_instruction;
2395923956
2396023957 return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type);
......@@ -25100,7 +25097,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2510025097ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec,
2510125098 ZigType *expected_type, AstNode *expected_type_source_node)
2510225099{
25103 assert(!old_exec->invalid);
25100 assert(old_exec->first_err_trace_msg == nullptr);
2510425101 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2510525102
2510625103 IrAnalyze *ira = allocate<IrAnalyze>(1);
......@@ -25147,6 +25144,15 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2514725144 old_instruction->child = new_instruction;
2514825145
2514925146 if (type_is_invalid(new_instruction->value.type)) {
25147 if (new_exec->first_err_trace_msg != nullptr) {
25148 ira->codegen->trace_err = new_exec->first_err_trace_msg;
25149 } else {
25150 new_exec->first_err_trace_msg = ira->codegen->trace_err;
25151 }
25152 if (new_exec->first_err_trace_msg != nullptr) {
25153 new_exec->first_err_trace_msg = add_error_note(ira->codegen, new_exec->first_err_trace_msg,
25154 old_instruction->source_node, buf_create_from_str("referenced here"));
25155 }
2515025156 return ira->codegen->builtin_types.entry_invalid;
2515125157 }
2515225158
......@@ -25158,7 +25164,12 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2515825164 ira->instruction_index += 1;
2515925165 }
2516025166
25161 if (new_exec->invalid) {
25167 if (new_exec->first_err_trace_msg != nullptr) {
25168 codegen->trace_err = new_exec->first_err_trace_msg;
25169 if (codegen->trace_err != nullptr) {
25170 codegen->trace_err = add_error_note(codegen, codegen->trace_err,
25171 new_exec->source_node, buf_create_from_str("referenced here"));
25172 }
2516225173 return ira->codegen->builtin_types.entry_invalid;
2516325174 } else if (ira->src_implicit_return_type_list.length == 0) {
2516425175 return codegen->builtin_types.entry_unreachable;
src/ir.hpp-2
......@@ -28,6 +28,4 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal
2828 AstNode *source_node);
2929const char *float_op_to_name(BuiltinFnId op, bool llvm_name);
3030
31void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text);
32
3331#endif