authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-27 13:02:31-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-27 13:02:31-04:00
log35a374efe034ae5ed9aafc06bbc581044e7325de
tree41f0fdd7555532dab92e38fb8afc58aa96ccdf61
parent326b7b794b01a1194df3785e497460bada756c29
parentd9ed55f017bdd23054cd1d7f50ac1da517b5a5e4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3115 from ziglang/fix-field-alignment-kludge

fix field alignment kludge by implementing lazy values

11 files changed, 1796 insertions(+), 1061 deletions(-)

src/all_types.hpp+106-31
......@@ -47,6 +47,19 @@ struct ResultLocPeer;
4747struct ResultLocPeerParent;
4848struct ResultLocBitCast;
4949
50enum PtrLen {
51 PtrLenUnknown,
52 PtrLenSingle,
53 PtrLenC,
54};
55
56enum UndefAllowed {
57 UndefOk,
58 UndefBad,
59 LazyOkNoUndef,
60 LazyOk,
61};
62
5063enum X64CABIClass {
5164 X64CABIClass_Unknown,
5265 X64CABIClass_MEMORY,
......@@ -69,9 +82,9 @@ struct IrExecutable {
6982 IrExecutable *source_exec;
7083 IrAnalyze *analysis;
7184 Scope *begin_scope;
85 ErrorMsg *first_err_trace_msg;
7286 ZigList<Tld *> tld_list;
7387
74 bool invalid;
7588 bool is_inline;
7689 bool is_generic_instantiation;
7790 bool need_err_code_spill;
......@@ -255,6 +268,7 @@ enum ConstValSpecial {
255268 ConstValSpecialRuntime,
256269 ConstValSpecialStatic,
257270 ConstValSpecialUndef,
271 ConstValSpecialLazy,
258272};
259273
260274enum RuntimeHintErrorUnion {
......@@ -291,6 +305,73 @@ struct ConstGlobalRefs {
291305 uint32_t align;
292306};
293307
308enum LazyValueId {
309 LazyValueIdInvalid,
310 LazyValueIdAlignOf,
311 LazyValueIdPtrType,
312 LazyValueIdOptType,
313 LazyValueIdSliceType,
314 LazyValueIdFnType,
315};
316
317struct LazyValue {
318 LazyValueId id;
319};
320
321struct LazyValueAlignOf {
322 LazyValue base;
323
324 IrAnalyze *ira;
325 IrInstruction *target_type;
326};
327
328struct LazyValueSliceType {
329 LazyValue base;
330
331 IrAnalyze *ira;
332 ZigType *elem_type;
333 IrInstruction *align_inst; // can be null
334
335 bool is_const;
336 bool is_volatile;
337 bool is_allowzero;
338};
339
340struct LazyValuePtrType {
341 LazyValue base;
342
343 IrAnalyze *ira;
344 IrInstruction *elem_type;
345 IrInstruction *align_inst; // can be null
346
347 PtrLen ptr_len;
348 uint32_t bit_offset_in_host;
349
350 uint32_t host_int_bytes;
351 bool is_const;
352 bool is_volatile;
353 bool is_allowzero;
354};
355
356struct LazyValueOptType {
357 LazyValue base;
358
359 IrAnalyze *ira;
360 IrInstruction *payload_type;
361};
362
363struct LazyValueFnType {
364 LazyValue base;
365
366 IrAnalyze *ira;
367 AstNode *proto_node;
368 IrInstruction **param_types;
369 IrInstruction *align_inst; // can be null
370 IrInstruction *return_type;
371
372 bool is_generic;
373};
374
294375struct ConstExprValue {
295376 ZigType *type;
296377 ConstValSpecial special;
......@@ -318,6 +399,7 @@ struct ConstExprValue {
318399 ConstPtrValue x_ptr;
319400 ConstArgTuple x_arg_tuple;
320401 Buf *x_enum_literal;
402 LazyValue *x_lazy;
321403
322404 // populated if special == ConstValSpecialRuntime
323405 RuntimeHintErrorUnion rh_error_union;
......@@ -364,6 +446,7 @@ enum TldResolution {
364446 TldResolutionUnresolved,
365447 TldResolutionResolving,
366448 TldResolutionInvalid,
449 TldResolutionOkLazy,
367450 TldResolutionOk,
368451};
369452
......@@ -420,10 +503,12 @@ struct TypeEnumField {
420503
421504struct TypeUnionField {
422505 Buf *name;
506 ZigType *type_entry; // available after ResolveStatusSizeKnown
507 ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown
423508 TypeEnumField *enum_field;
424 ZigType *type_entry;
425509 AstNode *decl_node;
426510 uint32_t gen_index;
511 uint32_t align;
427512};
428513
429514enum NodeType {
......@@ -946,6 +1031,7 @@ struct AstNodeEnumLiteral {
9461031
9471032struct AstNode {
9481033 enum NodeType type;
1034 bool already_traced_this_node;
9491035 size_t line;
9501036 size_t column;
9511037 ZigType *owner;
......@@ -1041,12 +1127,6 @@ struct FnTypeId {
10411127uint32_t fn_type_id_hash(FnTypeId*);
10421128bool fn_type_id_eql(FnTypeId *a, FnTypeId *b);
10431129
1044enum PtrLen {
1045 PtrLenUnknown,
1046 PtrLenSingle,
1047 PtrLenC,
1048};
1049
10501130struct ZigTypePointer {
10511131 ZigType *child_type;
10521132 ZigType *slice_parent;
......@@ -1057,6 +1137,7 @@ struct ZigTypePointer {
10571137 bool is_const;
10581138 bool is_volatile;
10591139 bool allow_zero;
1140 bool resolve_loop_flag_zero_bits;
10601141};
10611142
10621143struct ZigTypeInt {
......@@ -1075,7 +1156,8 @@ struct ZigTypeArray {
10751156
10761157struct TypeStructField {
10771158 Buf *name;
1078 ZigType *type_entry;
1159 ZigType *type_entry; // available after ResolveStatusSizeKnown
1160 ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown
10791161 size_t src_index;
10801162 size_t gen_index;
10811163 size_t offset; // byte offset from beginning of struct
......@@ -1131,11 +1213,11 @@ struct ZigTypeStruct {
11311213 ResolveStatus resolve_status;
11321214
11331215 bool is_slice;
1134 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1135 bool reported_infinite_err;
11361216 // whether any of the fields require comptime
11371217 // known after ResolveStatusZeroBitsKnown
11381218 bool requires_comptime;
1219 bool resolve_loop_flag_zero_bits;
1220 bool resolve_loop_flag_other;
11391221};
11401222
11411223struct ZigTypeOptional {
......@@ -1157,26 +1239,20 @@ struct ZigTypeErrorSet {
11571239
11581240struct ZigTypeEnum {
11591241 AstNode *decl_node;
1160 ContainerLayout layout;
1161 uint32_t src_field_count;
11621242 TypeEnumField *fields;
1163 bool is_invalid; // true if any fields are invalid
11641243 ZigType *tag_int_type;
11651244
11661245 ScopeDecls *decls_scope;
11671246
1168 // set this flag temporarily to detect infinite loops
1169 bool embedded_in_current;
1170 bool reported_infinite_err;
1171 // whether we've finished resolving it
1172 bool complete;
1173
1174 bool zero_bits_loop_flag;
1175 bool zero_bits_known;
1176
11771247 LLVMValueRef name_function;
11781248
11791249 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;
1250 uint32_t src_field_count;
1251
1252 ContainerLayout layout;
1253 ResolveStatus resolve_status;
1254
1255 bool resolve_loop_flag;
11801256};
11811257
11821258uint32_t type_ptr_hash(const ZigType *ptr);
......@@ -1189,7 +1265,7 @@ struct ZigTypeUnion {
11891265 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
11901266 ZigType *tag_type; // always an enum or null
11911267 LLVMTypeRef union_llvm_type;
1192 ZigType *most_aligned_union_member;
1268 TypeUnionField *most_aligned_union_member;
11931269 size_t gen_union_index;
11941270 size_t gen_tag_index;
11951271 size_t union_abi_size;
......@@ -1201,11 +1277,11 @@ struct ZigTypeUnion {
12011277 ResolveStatus resolve_status;
12021278
12031279 bool have_explicit_tag_type;
1204 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1205 bool reported_infinite_err;
12061280 // whether any of the fields require comptime
12071281 // the value is not valid until zero_bits_known == true
12081282 bool requires_comptime;
1283 bool resolve_loop_flag_zero_bits;
1284 bool resolve_loop_flag_other;
12091285};
12101286
12111287struct FnGenParamInfo {
......@@ -1717,6 +1793,7 @@ struct CodeGen {
17171793 //////////////////////////// Runtime State
17181794 LLVMModuleRef module;
17191795 ZigList<ErrorMsg*> errors;
1796 ErrorMsg *trace_err;
17201797 LLVMBuilderRef builder;
17211798 ZigLLVMDIBuilder *dbuilder;
17221799 ZigLLVMDICompileUnit *compile_unit;
......@@ -1769,7 +1846,6 @@ struct CodeGen {
17691846 ZigList<Tld *> resolve_queue;
17701847 size_t resolve_queue_index;
17711848 ZigList<TimeEvent> timing_events;
1772 ZigList<AstNode *> tld_ref_source_node_stack;
17731849 ZigList<ZigFn *> inline_fns;
17741850 ZigList<ZigFn *> test_fns;
17751851 ZigList<ErrorTableEntry *> errors_by_index;
......@@ -1854,7 +1930,6 @@ struct CodeGen {
18541930 ZigFn *main_fn;
18551931 ZigFn *panic_fn;
18561932 TldFn *panic_tld_fn;
1857 AstNode *root_export_decl;
18581933
18591934 WantPIC want_pic;
18601935 WantStackCheck want_stack_check;
......@@ -1942,7 +2017,7 @@ struct CodeGen {
19422017 Buf *zig_lib_dir;
19432018 Buf *zig_std_dir;
19442019 Buf *dynamic_linker_path;
1945 Buf *version_script_path;
2020 Buf *version_script_path;
19462021
19472022 const char **llvm_argv;
19482023 size_t llvm_argv_len;
......@@ -3659,13 +3734,13 @@ enum ResultLocId {
36593734 ResultLocIdBitCast,
36603735};
36613736
3662// Additions to this struct may need to be handled in
3737// Additions to this struct may need to be handled in
36633738// ir_reset_result
36643739struct ResultLoc {
36653740 ResultLocId id;
36663741 bool written;
36673742 bool allow_write_through_const;
3668 IrInstruction *resolved_loc; // result ptr
3743 IrInstruction *resolved_loc; // result ptr
36693744 IrInstruction *source_instruction;
36703745 IrInstruction *gen_instruction; // value to store to the result loc
36713746 ZigType *implicit_elem_type;
src/analyze.cpp+698-455
......@@ -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);
......@@ -59,13 +59,15 @@ ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg) {
5959 root_struct->source_code, root_struct->line_offsets, msg);
6060
6161 g->errors.append(err);
62 g->trace_err = err;
6263 return err;
6364}
6465
65ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg) {
66ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
6667 Token fake_token;
6768 fake_token.start_line = node->line;
6869 fake_token.start_column = node->column;
70 node->already_traced_this_node = true;
6971 return add_token_error(g, node->owner, &fake_token, msg);
7072}
7173
......@@ -271,6 +273,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
271273 return type_entry->data.structure.resolve_status >= status;
272274 case ZigTypeIdUnion:
273275 return type_entry->data.unionation.resolve_status >= status;
276 case ZigTypeIdEnum:
277 return type_entry->data.enumeration.resolve_status >= status;
274278 case ZigTypeIdFnFrame:
275279 switch (status) {
276280 case ResolveStatusInvalid:
......@@ -285,32 +289,28 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
285289 case ResolveStatusLLVMFull:
286290 return type_entry->llvm_type != nullptr;
287291 }
288 case ZigTypeIdEnum:
292 case ZigTypeIdOpaque:
293 return status < ResolveStatusSizeKnown;
294 case ZigTypeIdPointer:
289295 switch (status) {
290 case ResolveStatusUnstarted:
291 return true;
292296 case ResolveStatusInvalid:
293297 zig_unreachable();
298 case ResolveStatusUnstarted:
299 return true;
294300 case ResolveStatusZeroBitsKnown:
295 return type_entry->data.enumeration.zero_bits_known;
296301 case ResolveStatusAlignmentKnown:
297 return type_entry->data.enumeration.zero_bits_known;
298302 case ResolveStatusSizeKnown:
299 return type_entry->data.enumeration.complete;
303 return type_entry->abi_size != SIZE_MAX;
300304 case ResolveStatusLLVMFwdDecl:
301305 case ResolveStatusLLVMFull:
302 return type_entry->llvm_di_type != nullptr;
306 return type_entry->llvm_type != nullptr;
303307 }
304 zig_unreachable();
305 case ZigTypeIdOpaque:
306 return status < ResolveStatusSizeKnown;
307308 case ZigTypeIdMetaType:
308309 case ZigTypeIdVoid:
309310 case ZigTypeIdBool:
310311 case ZigTypeIdUnreachable:
311312 case ZigTypeIdInt:
312313 case ZigTypeIdFloat:
313 case ZigTypeIdPointer:
314314 case ZigTypeIdArray:
315315 case ZigTypeIdComptimeFloat:
316316 case ZigTypeIdComptimeInt:
......@@ -460,8 +460,6 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
460460 }
461461 }
462462
463 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));
464
465463 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
466464
467465 const char *star_str = ptr_len_to_star_str(ptr_len);
......@@ -491,17 +489,21 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
491489 buf_ptr(&child_type->name));
492490 }
493491
494 assert(child_type->id != ZigTypeIdInvalid);
495
496 if (type_has_bits(child_type)) {
497 entry->abi_size = g->builtin_types.entry_usize->abi_size;
498 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
499 entry->abi_align = g->builtin_types.entry_usize->abi_align;
492 if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) {
493 if (type_has_bits(child_type)) {
494 entry->abi_size = g->builtin_types.entry_usize->abi_size;
495 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
496 entry->abi_align = g->builtin_types.entry_usize->abi_align;
497 } else {
498 assert(byte_alignment == 0);
499 entry->abi_size = 0;
500 entry->size_in_bits = 0;
501 entry->abi_align = 0;
502 }
500503 } else {
501 assert(byte_alignment == 0);
502 entry->abi_size = 0;
503 entry->size_in_bits = 0;
504 entry->abi_align = 0;
504 entry->abi_size = SIZE_MAX;
505 entry->size_in_bits = SIZE_MAX;
506 entry->abi_align = UINT32_MAX;
505507 }
506508
507509 entry->data.pointer.ptr_len = ptr_len;
......@@ -865,7 +867,7 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
865867 return table_entry->value;
866868 }
867869 if (fn_type_id->return_type != nullptr) {
868 if ((err = ensure_complete_type(g, fn_type_id->return_type)))
870 if ((err = type_resolve(g, fn_type_id->return_type, ResolveStatusSizeKnown)))
869871 return g->builtin_types.entry_invalid;
870872 assert(fn_type_id->return_type->id != ZigTypeIdOpaque);
871873 } else {
......@@ -960,31 +962,209 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
960962 return entry;
961963}
962964
963ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name) {
965ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
966 Buf *type_name, UndefAllowed undef)
967{
964968 size_t backward_branch_count = 0;
965969 size_t backward_branch_quota = default_backward_branch_quota;
966970 return ir_eval_const_value(g, scope, node, type_entry,
967971 &backward_branch_count, &backward_branch_quota,
968 nullptr, nullptr, node, type_name, nullptr, nullptr);
972 nullptr, nullptr, node, type_name, nullptr, nullptr, undef);
969973}
970974
971ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
972 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
973 if (type_is_invalid(result->type))
974 return g->builtin_types.entry_invalid;
975static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, ZigType *parent_type,
976 ConstExprValue *parent_type_val, bool *is_zero_bits)
977{
978 Error err;
979 if (type_val->special != ConstValSpecialLazy) {
980 assert(type_val->special == ConstValSpecialStatic);
981 if ((type_val->data.x_type->id == ZigTypeIdStruct &&
982 type_val->data.x_type->data.structure.resolve_loop_flag_zero_bits) ||
983 (type_val->data.x_type->id == ZigTypeIdUnion &&
984 type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits) ||
985 type_val->data.x_type->id == ZigTypeIdPointer)
986 {
987 // Does a struct/union which contains a pointer field to itself have bits? Yes.
988 *is_zero_bits = false;
989 return ErrorNone;
990 }
991 if ((err = type_resolve(g, type_val->data.x_type, ResolveStatusZeroBitsKnown)))
992 return err;
993 *is_zero_bits = (type_val->data.x_type->abi_size == 0);
994 return ErrorNone;
995 }
996 switch (type_val->data.x_lazy->id) {
997 case LazyValueIdInvalid:
998 case LazyValueIdAlignOf:
999 zig_unreachable();
1000 case LazyValueIdPtrType: {
1001 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
9751002
976 assert(result->special != ConstValSpecialRuntime);
977 // Reject undefined as valid `type` type even though the specification
978 // allows it to be casted to anything.
979 // See also ir_resolve_type()
980 if (result->special == ConstValSpecialUndef) {
981 add_node_error(g, node,
982 buf_sprintf("expected type 'type', found '%s'",
983 buf_ptr(&g->builtin_types.entry_undef->name)));
984 return g->builtin_types.entry_invalid;
1003 if (parent_type_val == &lazy_ptr_type->elem_type->value) {
1004 // Does a struct which contains a pointer field to itself have bits? Yes.
1005 *is_zero_bits = false;
1006 return ErrorNone;
1007 } else {
1008 if (parent_type_val == nullptr) {
1009 parent_type_val = type_val;
1010 }
1011 return type_val_resolve_zero_bits(g, &lazy_ptr_type->elem_type->value, parent_type,
1012 parent_type_val, is_zero_bits);
1013 }
1014 }
1015 case LazyValueIdOptType:
1016 case LazyValueIdSliceType:
1017 *is_zero_bits = false;
1018 return ErrorNone;
1019 case LazyValueIdFnType: {
1020 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
1021 *is_zero_bits = lazy_fn_type->is_generic;
1022 return ErrorNone;
1023 }
1024 }
1025 zig_unreachable();
1026}
1027
1028Error type_val_resolve_is_opaque_type(CodeGen *g, ConstExprValue *type_val, bool *is_opaque_type) {
1029 if (type_val->special != ConstValSpecialLazy) {
1030 assert(type_val->special == ConstValSpecialStatic);
1031 *is_opaque_type = (type_val->data.x_type->id == ZigTypeIdOpaque);
1032 return ErrorNone;
1033 }
1034 switch (type_val->data.x_lazy->id) {
1035 case LazyValueIdInvalid:
1036 case LazyValueIdAlignOf:
1037 zig_unreachable();
1038 case LazyValueIdSliceType:
1039 case LazyValueIdPtrType:
1040 case LazyValueIdFnType:
1041 case LazyValueIdOptType:
1042 *is_opaque_type = false;
1043 return ErrorNone;
9851044 }
1045 zig_unreachable();
1046}
9861047
987 assert(result->data.x_type != nullptr);
1048static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue *type_val) {
1049 if (type_val->special != ConstValSpecialLazy) {
1050 return type_requires_comptime(g, type_val->data.x_type);
1051 }
1052 switch (type_val->data.x_lazy->id) {
1053 case LazyValueIdInvalid:
1054 case LazyValueIdAlignOf:
1055 zig_unreachable();
1056 case LazyValueIdSliceType: {
1057 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(type_val->data.x_lazy);
1058 if (type_is_invalid(lazy_slice_type->elem_type))
1059 return ReqCompTimeInvalid;
1060 return type_requires_comptime(g, lazy_slice_type->elem_type);
1061 }
1062 case LazyValueIdPtrType: {
1063 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
1064 return type_val_resolve_requires_comptime(g, &lazy_ptr_type->elem_type->value);
1065 }
1066 case LazyValueIdOptType: {
1067 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);
1068 return type_val_resolve_requires_comptime(g, &lazy_opt_type->payload_type->value);
1069 }
1070 case LazyValueIdFnType: {
1071 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
1072 if (lazy_fn_type->is_generic)
1073 return ReqCompTimeYes;
1074 switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->return_type->value)) {
1075 case ReqCompTimeInvalid:
1076 return ReqCompTimeInvalid;
1077 case ReqCompTimeYes:
1078 return ReqCompTimeYes;
1079 case ReqCompTimeNo:
1080 break;
1081 }
1082 size_t param_count = lazy_fn_type->proto_node->data.fn_proto.params.length;
1083 for (size_t i = 0; i < param_count; i += 1) {
1084 AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i);
1085 bool param_is_var_args = param_node->data.param_decl.is_var_args;
1086 if (param_is_var_args) break;
1087 switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->param_types[i]->value)) {
1088 case ReqCompTimeInvalid:
1089 return ReqCompTimeInvalid;
1090 case ReqCompTimeYes:
1091 return ReqCompTimeYes;
1092 case ReqCompTimeNo:
1093 break;
1094 }
1095 }
1096 return ReqCompTimeNo;
1097 }
1098 }
1099 zig_unreachable();
1100}
1101
1102Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t *abi_align) {
1103 Error err;
1104 if (type_val->special != ConstValSpecialLazy) {
1105 assert(type_val->special == ConstValSpecialStatic);
1106 ZigType *ty = type_val->data.x_type;
1107 if (ty->id == ZigTypeIdPointer) {
1108 *abi_align = g->builtin_types.entry_usize->abi_align;
1109 return ErrorNone;
1110 }
1111 if ((err = type_resolve(g, ty, ResolveStatusAlignmentKnown)))
1112 return err;
1113 *abi_align = ty->abi_align;
1114 return ErrorNone;
1115 }
1116 switch (type_val->data.x_lazy->id) {
1117 case LazyValueIdInvalid:
1118 case LazyValueIdAlignOf:
1119 zig_unreachable();
1120 case LazyValueIdSliceType:
1121 case LazyValueIdPtrType:
1122 case LazyValueIdFnType:
1123 *abi_align = g->builtin_types.entry_usize->abi_align;
1124 return ErrorNone;
1125 case LazyValueIdOptType: {
1126 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);
1127 return type_val_resolve_abi_align(g, &lazy_opt_type->payload_type->value, abi_align);
1128 }
1129 }
1130 zig_unreachable();
1131}
1132
1133static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, ConstExprValue *type_val) {
1134 if (type_val->special != ConstValSpecialLazy) {
1135 return type_has_one_possible_value(g, type_val->data.x_type);
1136 }
1137 switch (type_val->data.x_lazy->id) {
1138 case LazyValueIdInvalid:
1139 case LazyValueIdAlignOf:
1140 zig_unreachable();
1141 case LazyValueIdSliceType: // it has the len field
1142 case LazyValueIdOptType: // it has the optional bit
1143 case LazyValueIdFnType:
1144 return OnePossibleValueNo;
1145 case LazyValueIdPtrType: {
1146 Error err;
1147 bool zero_bits;
1148 if ((err = type_val_resolve_zero_bits(g, type_val, nullptr, nullptr, &zero_bits))) {
1149 return OnePossibleValueInvalid;
1150 }
1151 if (zero_bits) {
1152 return OnePossibleValueYes;
1153 } else {
1154 return OnePossibleValueNo;
1155 }
1156 }
1157 }
1158 zig_unreachable();
1159}
1160
1161ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
1162 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type,
1163 nullptr, UndefBad);
1164 if (type_is_invalid(result->type))
1165 return g->builtin_types.entry_invalid;
1166 src_assert(result->special == ConstValSpecialStatic, node);
1167 src_assert(result->data.x_type != nullptr, node);
9881168 return result->data.x_type;
9891169}
9901170
......@@ -1032,7 +1212,8 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
10321212}
10331213
10341214static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_t *result) {
1035 ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g), nullptr);
1215 ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g),
1216 nullptr, UndefBad);
10361217 if (type_is_invalid(align_result->type))
10371218 return false;
10381219
......@@ -1054,7 +1235,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
10541235 ZigType *ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
10551236 PtrLenUnknown, 0, 0, 0, false);
10561237 ZigType *str_type = get_slice_type(g, ptr_type);
1057 ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr);
1238 ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr, UndefBad);
10581239 if (type_is_invalid(result_val->type))
10591240 return false;
10601241
......@@ -1404,7 +1585,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
14041585 add_node_error(g, proto_node,
14051586 buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
14061587 return g->builtin_types.entry_invalid;
1407 //return get_generic_fn_type(g, &fn_type_id);
14081588 }
14091589
14101590 ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
......@@ -1423,14 +1603,17 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
14231603 }
14241604
14251605 if (!calling_convention_allows_zig_types(fn_type_id.cc) &&
1426 fn_type_id.return_type->id != ZigTypeIdVoid &&
1427 !type_allowed_in_extern(g, fn_type_id.return_type))
1606 fn_type_id.return_type->id != ZigTypeIdVoid)
14281607 {
1429 add_node_error(g, fn_proto->return_type,
1430 buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
1431 buf_ptr(&fn_type_id.return_type->name),
1432 calling_convention_name(fn_type_id.cc)));
1433 return g->builtin_types.entry_invalid;
1608 if ((err = type_resolve(g, fn_type_id.return_type, ResolveStatusSizeKnown)))
1609 return g->builtin_types.entry_invalid;
1610 if (!type_allowed_in_extern(g, fn_type_id.return_type)) {
1611 add_node_error(g, fn_proto->return_type,
1612 buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
1613 buf_ptr(&fn_type_id.return_type->name),
1614 calling_convention_name(fn_type_id.cc)));
1615 return g->builtin_types.entry_invalid;
1616 }
14341617 }
14351618
14361619 switch (fn_type_id.return_type->id) {
......@@ -1490,7 +1673,7 @@ bool type_is_invalid(ZigType *type_entry) {
14901673 case ZigTypeIdUnion:
14911674 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
14921675 case ZigTypeIdEnum:
1493 return type_entry->data.enumeration.is_invalid;
1676 return type_entry->data.enumeration.resolve_status == ResolveStatusInvalid;
14941677 default:
14951678 return false;
14961679 }
......@@ -1599,12 +1782,11 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
15991782
16001783 AstNode *decl_node = struct_type->data.structure.decl_node;
16011784
1602 if (struct_type->data.structure.resolve_loop_flag) {
1785 if (struct_type->data.structure.resolve_loop_flag_other) {
16031786 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
16041787 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);
1788 add_node_error(g, decl_node,
1789 buf_sprintf("struct '%s' depends on itself", buf_ptr(&struct_type->name)));
16081790 }
16091791 return ErrorSemanticAnalyzeFail;
16101792 }
......@@ -1615,18 +1797,42 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16151797 size_t field_count = struct_type->data.structure.src_field_count;
16161798
16171799 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
1618 struct_type->data.structure.resolve_loop_flag = true;
1800 struct_type->data.structure.resolve_loop_flag_other = true;
16191801
16201802 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
16211803
1622 // Resolve sizes of all the field types. Done before the offset loop because the offset
1623 // loop has to look ahead.
1804 // Resolve types for fields and then resolve sizes of all the field types.
1805 // This is done before the offset loop because the offset loop has to look ahead.
16241806 for (size_t i = 0; i < field_count; i += 1) {
1807 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
16251808 TypeStructField *field = &struct_type->data.structure.fields[i];
1809
1810 if ((err = ir_resolve_lazy(g, field_source_node, field->type_val))) {
1811 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1812 return err;
1813 }
1814 field->type_entry = field->type_val->data.x_type;
1815
16261816 if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) {
16271817 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1818 return err;
1819 }
1820
1821 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
1822 !type_allowed_in_extern(g, field->type_entry))
1823 {
1824 add_node_error(g, field_source_node,
1825 buf_sprintf("extern structs cannot contain fields of type '%s'",
1826 buf_ptr(&field->type_entry->name)));
1827 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
16281828 return ErrorSemanticAnalyzeFail;
1829 } else if (packed) {
1830 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field->type_entry, field_source_node))) {
1831 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1832 return err;
1833 }
16291834 }
1835
16301836 }
16311837
16321838 size_t packed_bits_offset = 0;
......@@ -1690,9 +1896,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16901896 break;
16911897 }
16921898 }
1693 size_t next_abi_align = (next_src_field_index == field_count) ?
1694 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1695 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align);
1899 size_t next_align = (next_src_field_index == field_count) ?
1900 abi_align : struct_type->data.structure.fields[next_src_field_index].align;
1901 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_align);
16961902 size_in_bits = next_offset * 8;
16971903 }
16981904 }
......@@ -1708,7 +1914,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
17081914 struct_type->size_in_bits = size_in_bits;
17091915 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
17101916 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
1711 struct_type->data.structure.resolve_loop_flag = false;
1917 struct_type->data.structure.resolve_loop_flag_other = false;
17121918 struct_type->data.structure.host_int_bytes = host_int_bytes;
17131919
17141920 return ErrorNone;
......@@ -1728,22 +1934,21 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17281934 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
17291935 return ErrorNone;
17301936
1731 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;
1937 AstNode *decl_node = union_type->data.structure.decl_node;
1938
1939 if (union_type->data.unionation.resolve_loop_flag_other) {
1940 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
17351941 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);
1942 add_node_error(g, decl_node,
1943 buf_sprintf("union '%s' depends on itself", buf_ptr(&union_type->name)));
17391944 }
17401945 return ErrorSemanticAnalyzeFail;
17411946 }
17421947
17431948 // set temporary flag
1744 union_type->data.unionation.resolve_loop_flag = true;
1949 union_type->data.unionation.resolve_loop_flag_other = true;
17451950
1746 ZigType *most_aligned_union_member = nullptr;
1951 TypeUnionField *most_aligned_union_member = nullptr;
17471952 uint32_t field_count = union_type->data.unionation.src_field_count;
17481953 bool packed = union_type->data.unionation.layout == ContainerLayoutPacked;
17491954
......@@ -1752,34 +1957,40 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17521957 if (field->gen_index == UINT32_MAX)
17531958 continue;
17541959
1755 size_t this_field_align;
1756 if (packed) {
1757 // TODO: https://github.com/ziglang/zig/issues/1512
1758 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;
1762 } else {
1960 AstNode *align_expr = field->decl_node->data.struct_field.align_expr;
1961 if (align_expr != nullptr) {
1962 if (!analyze_const_align(g, &union_type->data.unionation.decls_scope->base, align_expr,
1963 &field->align))
1964 {
1965 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1966 return err;
1967 }
1968 add_node_error(g, field->decl_node,
1969 buf_create_from_str("TODO implement field alignment syntax for unions. https://github.com/ziglang/zig/issues/3125"));
1970 } else if (packed) {
1971 field->align = 1;
1972 } else if (field->type_entry != nullptr) {
17631973 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
17641974 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1765 return ErrorSemanticAnalyzeFail;
1975 return err;
1976 }
1977 field->align = field->type_entry->abi_align;
1978 } else {
1979 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
1980 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1981 return err;
17661982 }
1767
17681983 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
17691984 return ErrorSemanticAnalyzeFail;
1770
1771 this_field_align = field->type_entry->abi_align;
17721985 }
17731986
1774 if (most_aligned_union_member == nullptr ||
1775 this_field_align > most_aligned_union_member->abi_align)
1776 {
1777 most_aligned_union_member = field->type_entry;
1987 if (most_aligned_union_member == nullptr || field->align > most_aligned_union_member->align) {
1988 most_aligned_union_member = field;
17781989 }
17791990 }
17801991
17811992 // unset temporary flag
1782 union_type->data.unionation.resolve_loop_flag = false;
1993 union_type->data.unionation.resolve_loop_flag_other = false;
17831994 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
17841995 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
17851996
......@@ -1793,18 +2004,18 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17932004 union_type->abi_align = tag_type->abi_align;
17942005 union_type->data.unionation.gen_tag_index = SIZE_MAX;
17952006 union_type->data.unionation.gen_union_index = SIZE_MAX;
1796 } else if (tag_type->abi_align > most_aligned_union_member->abi_align) {
2007 } else if (tag_type->abi_align > most_aligned_union_member->align) {
17972008 union_type->abi_align = tag_type->abi_align;
17982009 union_type->data.unionation.gen_tag_index = 0;
17992010 union_type->data.unionation.gen_union_index = 1;
18002011 } else {
1801 union_type->abi_align = most_aligned_union_member->abi_align;
2012 union_type->abi_align = most_aligned_union_member->align;
18022013 union_type->data.unionation.gen_union_index = 0;
18032014 union_type->data.unionation.gen_tag_index = 1;
18042015 }
18052016 } else {
18062017 assert(most_aligned_union_member != nullptr);
1807 union_type->abi_align = most_aligned_union_member->abi_align;
2018 union_type->abi_align = most_aligned_union_member->align;
18082019 union_type->data.unionation.gen_union_index = SIZE_MAX;
18092020 union_type->data.unionation.gen_tag_index = SIZE_MAX;
18102021 }
......@@ -1812,6 +2023,17 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
18122023 return ErrorNone;
18132024}
18142025
2026ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field) {
2027 Error err;
2028 if (union_field->type_entry == nullptr) {
2029 if ((err = ir_resolve_lazy(g, union_field->decl_node, union_field->type_val))) {
2030 return nullptr;
2031 }
2032 union_field->type_entry = union_field->type_val->data.x_type;
2033 }
2034 return union_field->type_entry;
2035}
2036
18152037static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18162038 assert(union_type->id == ZigTypeIdUnion);
18172039
......@@ -1831,30 +2053,32 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18312053 assert(decl_node->type == NodeTypeContainerDecl);
18322054
18332055 uint32_t field_count = union_type->data.unionation.src_field_count;
1834 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
2056 TypeUnionField *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
18352057
18362058 assert(union_type->data.unionation.fields);
18372059
18382060 size_t union_abi_size = 0;
18392061 size_t union_size_in_bits = 0;
18402062
1841 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;
2063 if (union_type->data.unionation.resolve_loop_flag_other) {
2064 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
18442065 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);
2066 add_node_error(g, decl_node,
2067 buf_sprintf("union '%s' depends on itself", buf_ptr(&union_type->name)));
18482068 }
18492069 return ErrorSemanticAnalyzeFail;
18502070 }
18512071
18522072 // set temporary flag
1853 union_type->data.unionation.resolve_loop_flag = true;
2073 union_type->data.unionation.resolve_loop_flag_other = true;
18542074
18552075 for (uint32_t i = 0; i < field_count; i += 1) {
18562076 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
1857 ZigType *field_type = union_field->type_entry;
2077 ZigType *field_type = resolve_union_field_type(g, union_field);
2078 if (field_type == nullptr) {
2079 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2080 return ErrorSemanticAnalyzeFail;
2081 }
18582082
18592083 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
18602084 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
......@@ -1874,11 +2098,11 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18742098 // The union itself for now has to be treated as being independently aligned.
18752099 // See https://github.com/ziglang/zig/issues/2166.
18762100 if (most_aligned_union_member != nullptr) {
1877 union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align);
2101 union_abi_size = align_forward(union_abi_size, most_aligned_union_member->align);
18782102 }
18792103
18802104 // unset temporary flag
1881 union_type->data.unionation.resolve_loop_flag = false;
2105 union_type->data.unionation.resolve_loop_flag_other = false;
18822106 union_type->data.unionation.resolve_status = ResolveStatusSizeKnown;
18832107 union_type->data.unionation.union_abi_size = union_abi_size;
18842108
......@@ -1897,7 +2121,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18972121 field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size;
18982122 field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align;
18992123 field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size;
1900 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align;
2124 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->align;
19012125 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]);
19022126 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align);
19032127 union_type->size_in_bits = union_type->abi_size * 8;
......@@ -1925,24 +2149,25 @@ static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) {
19252149static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19262150 assert(enum_type->id == ZigTypeIdEnum);
19272151
1928 if (enum_type->data.enumeration.is_invalid)
2152 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
19292153 return ErrorSemanticAnalyzeFail;
1930
1931 if (enum_type->data.enumeration.zero_bits_known)
2154 if (enum_type->data.enumeration.resolve_status >= ResolveStatusZeroBitsKnown)
19322155 return ErrorNone;
19332156
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;
2157 AstNode *decl_node = enum_type->data.enumeration.decl_node;
2158 assert(decl_node->type == NodeTypeContainerDecl);
2159
2160 if (enum_type->data.enumeration.resolve_loop_flag) {
2161 if (enum_type->data.enumeration.resolve_status != ResolveStatusInvalid) {
2162 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
2163 add_node_error(g, decl_node,
2164 buf_sprintf("enum '%s' depends on itself",
2165 buf_ptr(&enum_type->name)));
2166 }
19392167 return ErrorSemanticAnalyzeFail;
19402168 }
19412169
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);
2170 enum_type->data.enumeration.resolve_loop_flag = true;
19462171
19472172 assert(!enum_type->data.enumeration.fields);
19482173 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
......@@ -1951,9 +2176,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19512176
19522177 enum_type->data.enumeration.src_field_count = field_count;
19532178 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;
2179 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19572180 return ErrorSemanticAnalyzeFail;
19582181 }
19592182
......@@ -1982,14 +2205,14 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19822205 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
19832206 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
19842207 if (type_is_invalid(wanted_tag_int_type)) {
1985 enum_type->data.enumeration.is_invalid = true;
2208 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19862209 } else if (wanted_tag_int_type->id != ZigTypeIdInt) {
1987 enum_type->data.enumeration.is_invalid = true;
2210 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19882211 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
19892212 buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));
19902213 } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern &&
19912214 !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) {
1992 enum_type->data.enumeration.is_invalid = true;
2215 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
19932216 ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr,
19942217 buf_sprintf("'%s' is not a valid tag type for an extern enum",
19952218 buf_ptr(&wanted_tag_int_type->name)));
......@@ -2022,6 +2245,11 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20222245 buf_sprintf("structs and unions, not enums, support field types"));
20232246 add_error_note(g, msg, decl_node,
20242247 buf_sprintf("consider 'union(enum)' here"));
2248 } else if (field_node->data.struct_field.align_expr != nullptr) {
2249 ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.align_expr,
2250 buf_sprintf("structs and unions, not enums, support field alignment"));
2251 add_error_note(g, msg, decl_node,
2252 buf_sprintf("consider 'union(enum)' here"));
20252253 }
20262254
20272255 auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);
......@@ -2029,7 +2257,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20292257 ErrorMsg *msg = add_node_error(g, field_node,
20302258 buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name)));
20312259 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2032 enum_type->data.enumeration.is_invalid = true;
2260 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20332261 continue;
20342262 }
20352263
......@@ -2037,9 +2265,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20372265
20382266 if (tag_value != nullptr) {
20392267 // A user-specified value is available
2040 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
2268 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,
2269 nullptr, UndefBad);
20412270 if (type_is_invalid(result->type)) {
2042 enum_type->data.enumeration.is_invalid = true;
2271 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20432272 continue;
20442273 }
20452274
......@@ -2060,7 +2289,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20602289 if (!bigint_fits_in_bits(&type_enum_field->value,
20612290 tag_int_type->size_in_bits,
20622291 tag_int_type->data.integral.is_signed)) {
2063 enum_type->data.enumeration.is_invalid = true;
2292 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20642293
20652294 Buf *val_buf = buf_alloc();
20662295 bigint_append_buf(val_buf, &type_enum_field->value, 10);
......@@ -2075,7 +2304,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20752304 // Make sure the value is unique
20762305 auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node);
20772306 if (entry != nullptr) {
2078 enum_type->data.enumeration.is_invalid = true;
2307 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20792308
20802309 Buf *val_buf = buf_alloc();
20812310 bigint_append_buf(val_buf, &type_enum_field->value, 10);
......@@ -2089,13 +2318,12 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20892318 last_enum_field = type_enum_field;
20902319 }
20912320
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)
2321 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
20972322 return ErrorSemanticAnalyzeFail;
20982323
2324 enum_type->data.enumeration.resolve_loop_flag = false;
2325 enum_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
2326
20992327 return ErrorNone;
21002328}
21012329
......@@ -2112,16 +2340,17 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
21122340 AstNode *decl_node = struct_type->data.structure.decl_node;
21132341 assert(decl_node->type == NodeTypeContainerDecl);
21142342
2115 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;
2343 if (struct_type->data.structure.resolve_loop_flag_zero_bits) {
2344 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2345 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2346 add_node_error(g, decl_node,
2347 buf_sprintf("struct '%s' depends on itself",
2348 buf_ptr(&struct_type->name)));
2349 }
2350 return ErrorSemanticAnalyzeFail;
21222351 }
21232352
2124 struct_type->data.structure.resolve_loop_flag = true;
2353 struct_type->data.structure.resolve_loop_flag_zero_bits = true;
21252354
21262355 assert(!struct_type->data.structure.fields);
21272356 size_t field_count = decl_node->data.container_decl.fields.length;
......@@ -2153,58 +2382,60 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
21532382 return ErrorSemanticAnalyzeFail;
21542383 }
21552384
2156 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2157 type_struct_field->type_entry = field_type;
2158 if (type_is_invalid(field_type)) {
2385 ConstExprValue *field_type_val = analyze_const_value(g, scope,
2386 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef);
2387 if (type_is_invalid(field_type_val->type)) {
21592388 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
21602389 return ErrorSemanticAnalyzeFail;
21612390 }
2391 assert(field_type_val->special != ConstValSpecialRuntime);
2392 type_struct_field->type_val = field_type_val;
21622393 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
21632394 return ErrorSemanticAnalyzeFail;
21642395
2165 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2166 !type_allowed_in_extern(g, field_type))
2167 {
2168 add_node_error(g, field_node,
2169 buf_sprintf("extern structs cannot contain fields of type '%s'",
2170 buf_ptr(&field_type->name)));
2396 bool field_is_opaque_type;
2397 if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) {
21712398 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
21722399 return ErrorSemanticAnalyzeFail;
2173 } else if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2174 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) {
2175 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2176 return ErrorSemanticAnalyzeFail;
2177 }
21782400 }
2179
2180 type_struct_field->src_index = i;
2181 type_struct_field->gen_index = SIZE_MAX;
2182
2183 if (field_type->id == ZigTypeIdOpaque) {
2401 if (field_is_opaque_type) {
21842402 add_node_error(g, field_node->data.struct_field.type,
21852403 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
21862404 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
21872405 return ErrorSemanticAnalyzeFail;
21882406 }
2189 switch (type_requires_comptime(g, field_type)) {
2407
2408 type_struct_field->src_index = i;
2409 type_struct_field->gen_index = SIZE_MAX;
2410
2411 switch (type_val_resolve_requires_comptime(g, field_type_val)) {
21902412 case ReqCompTimeYes:
21912413 struct_type->data.structure.requires_comptime = true;
21922414 break;
21932415 case ReqCompTimeInvalid:
2416 if (g->trace_err != nullptr) {
2417 g->trace_err = add_error_note(g, g->trace_err, field_node,
2418 buf_create_from_str("while checking this field"));
2419 }
21942420 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
21952421 return ErrorSemanticAnalyzeFail;
21962422 case ReqCompTimeNo:
21972423 break;
21982424 }
21992425
2200 if (!type_has_bits(field_type))
2426 bool field_is_zero_bits;
2427 if ((err = type_val_resolve_zero_bits(g, field_type_val, struct_type, nullptr, &field_is_zero_bits))) {
2428 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2429 return ErrorSemanticAnalyzeFail;
2430 }
2431 if (field_is_zero_bits)
22012432 continue;
22022433
22032434 type_struct_field->gen_index = gen_field_index;
22042435 gen_field_index += 1;
22052436 }
22062437
2207 struct_type->data.structure.resolve_loop_flag = false;
2438 struct_type->data.structure.resolve_loop_flag_zero_bits = false;
22082439 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
22092440 if (gen_field_index != 0) {
22102441 struct_type->abi_size = SIZE_MAX;
......@@ -2234,17 +2465,16 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
22342465
22352466 AstNode *decl_node = struct_type->data.structure.decl_node;
22362467
2237 if (struct_type->data.structure.resolve_loop_flag) {
2468 if (struct_type->data.structure.resolve_loop_flag_other) {
22382469 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
22392470 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);
2471 add_node_error(g, decl_node,
2472 buf_sprintf("struct '%s' depends on itself", buf_ptr(&struct_type->name)));
22432473 }
22442474 return ErrorSemanticAnalyzeFail;
22452475 }
22462476
2247 struct_type->data.structure.resolve_loop_flag = true;
2477 struct_type->data.structure.resolve_loop_flag_other = true;
22482478 assert(decl_node->type == NodeTypeContainerDecl);
22492479
22502480 size_t field_count = struct_type->data.structure.src_field_count;
......@@ -2255,47 +2485,31 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
22552485 if (field->gen_index == SIZE_MAX)
22562486 continue;
22572487
2258 uint32_t this_field_align;
2259
2260 // TODO: Sets the field alignment in the type, but doesn't do anything
2261 // to actually make that happen yet.
22622488 AstNode *align_expr = field->decl_node->data.struct_field.align_expr;
22632489 if (align_expr != nullptr) {
2264 if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr, &this_field_align)) {
2265 field->type_entry = g->builtin_types.entry_invalid;
2266 } else {
2267 field->align = this_field_align;
2490 if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr,
2491 &field->align))
2492 {
2493 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2494 return err;
22682495 }
22692496 } else if (packed) {
2270 // TODO: https://github.com/ziglang/zig/issues/1512
2271 // TODO: Validate requested alignment is possible, given packed,
2272 // and given other field alignments.
2273 this_field_align = 1;
2274 // TODO If we have no type_entry for the field, we've already failed to
2275 // compile the program correctly. This stage1 compiler needs a deeper
2276 // reworking to make this correct, or we can ignore the problem
2277 // and make sure it is fixed in stage2. This workaround is for when
2278 // there is a false positive of a dependency loop, of alignment depending
2279 // on itself. When this false positive happens we assume a pointer-aligned
2280 // field, which is usually fine but could be incorrectly over-aligned or
2281 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2282 } else if (field->type_entry == nullptr) {
2283 this_field_align = g->builtin_types.entry_usize->abi_align;
2497 field->align = 1;
22842498 } else {
2285 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2499 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
22862500 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2287 return ErrorSemanticAnalyzeFail;
2501 return err;
22882502 }
2289 this_field_align = field->type_entry->abi_align;
2503 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2504 return ErrorSemanticAnalyzeFail;
22902505 }
22912506
2292 // TODO: https://github.com/ziglang/zig/issues/1512
2293 if (this_field_align > struct_type->abi_align) {
2294 struct_type->abi_align = this_field_align;
2507 if (field->align > struct_type->abi_align) {
2508 struct_type->abi_align = field->align;
22952509 }
22962510 }
22972511
2298 struct_type->data.structure.resolve_loop_flag = false;
2512 struct_type->data.structure.resolve_loop_flag_other = false;
22992513
23002514 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
23012515 return ErrorSemanticAnalyzeFail;
......@@ -2316,23 +2530,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
23162530 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
23172531 return ErrorNone;
23182532
2319 if (union_type->data.unionation.resolve_loop_flag) {
2320 // If we get here it's due to recursion. From this we conclude that the struct is
2321 // not zero bits.
2322 // TODO actually it could still be zero bits. Here we should continue analyzing
2323 // the union from the next field index.
2324 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2325 union_type->data.unionation.resolve_loop_flag = false;
2326 union_type->abi_size = SIZE_MAX;
2327 union_type->size_in_bits = SIZE_MAX;
2328 return ErrorNone;
2329 }
2330
2331 union_type->data.unionation.resolve_loop_flag = true;
2332
23332533 AstNode *decl_node = union_type->data.unionation.decl_node;
23342534 assert(decl_node->type == NodeTypeContainerDecl);
23352535
2536 if (union_type->data.unionation.resolve_loop_flag_zero_bits) {
2537 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
2538 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2539 add_node_error(g, decl_node,
2540 buf_sprintf("union '%s' depends on itself",
2541 buf_ptr(&union_type->name)));
2542 }
2543 return ErrorSemanticAnalyzeFail;
2544 }
2545
2546 union_type->data.unionation.resolve_loop_flag_zero_bits = true;
2547
23362548 assert(union_type->data.unionation.fields == nullptr);
23372549 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
23382550 if (field_count == 0) {
......@@ -2392,14 +2604,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
23922604 tag_type->size_in_bits = tag_int_type->size_in_bits;
23932605
23942606 tag_type->data.enumeration.tag_int_type = tag_int_type;
2395 tag_type->data.enumeration.zero_bits_known = true;
2607 tag_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
23962608 tag_type->data.enumeration.decl_node = decl_node;
23972609 tag_type->data.enumeration.layout = ContainerLayoutAuto;
23982610 tag_type->data.enumeration.src_field_count = field_count;
23992611 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
24002612 tag_type->data.enumeration.fields_by_name.init(field_count);
24012613 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;
2402 tag_type->data.enumeration.complete = true;
24032614 } else if (enum_type_node != nullptr) {
24042615 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
24052616 if (type_is_invalid(enum_type)) {
......@@ -2441,49 +2652,68 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
24412652 return ErrorSemanticAnalyzeFail;
24422653 }
24432654
2444 ZigType *field_type;
2655 bool field_is_zero_bits;
24452656 if (field_node->data.struct_field.type == nullptr) {
2446 if (decl_node->data.container_decl.auto_enum || decl_node->data.container_decl.init_arg_expr != nullptr) {
2447 field_type = g->builtin_types.entry_void;
2657 if (decl_node->data.container_decl.auto_enum ||
2658 decl_node->data.container_decl.init_arg_expr != nullptr)
2659 {
2660 union_field->type_entry = g->builtin_types.entry_void;
2661 field_is_zero_bits = true;
24482662 } else {
24492663 add_node_error(g, field_node, buf_sprintf("union field missing type"));
24502664 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
24512665 return ErrorSemanticAnalyzeFail;
24522666 }
24532667 } else {
2454 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2455 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2668 ConstExprValue *field_type_val = analyze_const_value(g, scope,
2669 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef);
2670 if (type_is_invalid(field_type_val->type)) {
24562671 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
24572672 return ErrorSemanticAnalyzeFail;
24582673 }
2674 assert(field_type_val->special != ConstValSpecialRuntime);
2675 union_field->type_val = field_type_val;
24592676 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
24602677 return ErrorSemanticAnalyzeFail;
2461 }
2462 union_field->type_entry = field_type;
24632678
2464 if (field_type->id == ZigTypeIdOpaque) {
2465 add_node_error(g, field_node->data.struct_field.type,
2466 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));
2467 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2468 return ErrorSemanticAnalyzeFail;
2469 }
2679 bool field_is_opaque_type;
2680 if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) {
2681 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2682 return ErrorSemanticAnalyzeFail;
2683 }
2684 if (field_is_opaque_type) {
2685 add_node_error(g, field_node->data.struct_field.type,
2686 buf_create_from_str(
2687 "opaque types have unknown size and therefore cannot be directly embedded in unions"));
2688 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2689 return ErrorSemanticAnalyzeFail;
2690 }
24702691
2471 switch (type_requires_comptime(g, field_type)) {
2472 case ReqCompTimeInvalid:
2692 switch (type_val_resolve_requires_comptime(g, field_type_val)) {
2693 case ReqCompTimeInvalid:
2694 if (g->trace_err != nullptr) {
2695 g->trace_err = add_error_note(g, g->trace_err, field_node,
2696 buf_create_from_str("while checking this field"));
2697 }
2698 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2699 return ErrorSemanticAnalyzeFail;
2700 case ReqCompTimeYes:
2701 union_type->data.unionation.requires_comptime = true;
2702 break;
2703 case ReqCompTimeNo:
2704 break;
2705 }
2706
2707 if ((err = type_val_resolve_zero_bits(g, field_type_val, union_type, nullptr, &field_is_zero_bits))) {
24732708 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
24742709 return ErrorSemanticAnalyzeFail;
2475 case ReqCompTimeYes:
2476 union_type->data.unionation.requires_comptime = true;
2477 break;
2478 case ReqCompTimeNo:
2479 break;
2710 }
24802711 }
24812712
24822713 if (field_node->data.struct_field.value != nullptr && !decl_node->data.container_decl.auto_enum) {
24832714 ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value,
2484 buf_sprintf("non-enum union field assignment"));
2485 add_error_note(g, msg, decl_node,
2486 buf_sprintf("consider 'union(enum)' here"));
2715 buf_create_from_str("untagged union field assignment"));
2716 add_error_note(g, msg, decl_node, buf_create_from_str("consider 'union(enum)' here"));
24872717 }
24882718
24892719 if (create_enum_type) {
......@@ -2501,7 +2731,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
25012731 // In a second pass we will fill in the unspecified ones.
25022732 if (tag_value != nullptr) {
25032733 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
2504 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
2734 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,
2735 nullptr, UndefBad);
25052736 if (type_is_invalid(result->type)) {
25062737 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
25072738 return ErrorSemanticAnalyzeFail;
......@@ -2542,7 +2773,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
25422773 }
25432774 assert(union_field->enum_field != nullptr);
25442775
2545 if (!type_has_bits(field_type))
2776 if (field_is_zero_bits)
25462777 continue;
25472778
25482779 union_field->gen_index = gen_field_index;
......@@ -2619,7 +2850,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
26192850 return ErrorSemanticAnalyzeFail;
26202851 }
26212852
2622 union_type->data.unionation.resolve_loop_flag = false;
2853 union_type->data.unionation.resolve_loop_flag_zero_bits = false;
26232854
26242855 union_type->data.unionation.gen_field_count = gen_field_index;
26252856 bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag);
......@@ -2676,6 +2907,8 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node) {
26762907 fn_entry->body_node = (proto_node->data.fn_proto.fn_def_node == nullptr) ? nullptr :
26772908 proto_node->data.fn_proto.fn_def_node->data.fn_def.body;
26782909
2910 fn_entry->analyzed_executable.source_node = fn_entry->body_node;
2911
26792912 return fn_entry;
26802913}
26812914
......@@ -2702,7 +2935,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) {
27022935 fake_decl->data.symbol_expr.symbol = tld_fn->base.name;
27032936
27042937 // call this for the side effects of casting to panic_fn_type
2705 analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr);
2938 analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr, UndefBad);
27062939}
27072940
27082941ZigType *get_test_fn_type(CodeGen *g) {
......@@ -2848,7 +3081,8 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
28483081static void resolve_decl_comptime(CodeGen *g, TldCompTime *tld_comptime) {
28493082 assert(tld_comptime->base.source_node->type == NodeTypeCompTime);
28503083 AstNode *expr_node = tld_comptime->base.source_node->data.comptime_expr.expr;
2851 analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void, nullptr);
3084 analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void,
3085 nullptr, UndefBad);
28523086}
28533087
28543088static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
......@@ -2943,7 +3177,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
29433177
29443178void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
29453179 Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name);
2946 resolve_top_level_decl(g, tld, tld->source_node);
3180 resolve_top_level_decl(g, tld, tld->source_node, false);
29473181 assert(tld->id == TldIdVar);
29483182 TldVar *tld_var = (TldVar *)tld;
29493183 tld_var->var->const_value = value;
......@@ -3185,7 +3419,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
31853419 return variable_entry;
31863420}
31873421
3188static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3422static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
31893423 AstNode *source_node = tld_var->base.source_node;
31903424 AstNodeVariableDeclaration *var_decl = &source_node->data.variable_declaration;
31913425
......@@ -3197,9 +3431,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
31973431 ZigType *explicit_type = nullptr;
31983432 if (var_decl->type) {
31993433 if (tld_var->analyzing_type) {
3200 ErrorMsg *msg = add_node_error(g, var_decl->type,
3434 add_node_error(g, var_decl->type,
32013435 buf_sprintf("type of '%s' depends on itself", buf_ptr(tld_var->base.name)));
3202 emit_error_notes_for_ref_stack(g, msg);
32033436 explicit_type = g->builtin_types.entry_invalid;
32043437 } else {
32053438 tld_var->analyzing_type = true;
......@@ -3217,7 +3450,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32173450 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
32183451 implicit_type = explicit_type;
32193452 } else if (var_decl->expr) {
3220 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
3453 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type,
3454 var_decl->symbol, allow_lazy ? LazyOk : UndefOk);
32213455 assert(init_value);
32223456 implicit_type = init_value->type;
32233457
......@@ -3372,7 +3606,8 @@ static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, Sco
33723606 using_namespace->base.resolution = TldResolutionResolving;
33733607 assert(using_namespace->base.source_node->type == NodeTypeUsingNamespace);
33743608 ConstExprValue *result = analyze_const_value(g, &dest_decls_scope->base,
3375 using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type, nullptr);
3609 using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type,
3610 nullptr, UndefBad);
33763611 using_namespace->using_namespace_value = result;
33773612
33783613 if (type_is_invalid(result->type)) {
......@@ -3392,51 +3627,61 @@ static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, Sco
33923627 }
33933628}
33943629
3395void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3396 if (tld->resolution != TldResolutionUnresolved)
3630void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy) {
3631 bool want_resolve_lazy = tld->resolution == TldResolutionOkLazy && !allow_lazy;
3632 if (tld->resolution != TldResolutionUnresolved && !want_resolve_lazy)
33973633 return;
33983634
3399 assert(tld->resolution != TldResolutionResolving);
34003635 tld->resolution = TldResolutionResolving;
3401 g->tld_ref_source_node_stack.append(source_node);
34023636
34033637 switch (tld->id) {
3404 case TldIdVar:
3405 {
3406 TldVar *tld_var = (TldVar *)tld;
3407 resolve_decl_var(g, tld_var);
3408 break;
3409 }
3410 case TldIdFn:
3411 {
3412 TldFn *tld_fn = (TldFn *)tld;
3413 resolve_decl_fn(g, tld_fn);
3414 break;
3415 }
3416 case TldIdContainer:
3417 {
3418 TldContainer *tld_container = (TldContainer *)tld;
3419 resolve_decl_container(g, tld_container);
3420 break;
3421 }
3422 case TldIdCompTime:
3423 {
3424 TldCompTime *tld_comptime = (TldCompTime *)tld;
3425 resolve_decl_comptime(g, tld_comptime);
3426 break;
3638 case TldIdVar: {
3639 TldVar *tld_var = (TldVar *)tld;
3640 if (want_resolve_lazy) {
3641 ir_resolve_lazy(g, source_node, tld_var->var->const_value);
3642 } else {
3643 resolve_decl_var(g, tld_var, allow_lazy);
34273644 }
3645 tld->resolution = allow_lazy ? TldResolutionOkLazy : TldResolutionOk;
3646 break;
3647 }
3648 case TldIdFn: {
3649 TldFn *tld_fn = (TldFn *)tld;
3650 resolve_decl_fn(g, tld_fn);
3651
3652 tld->resolution = TldResolutionOk;
3653 break;
3654 }
3655 case TldIdContainer: {
3656 TldContainer *tld_container = (TldContainer *)tld;
3657 resolve_decl_container(g, tld_container);
3658
3659 tld->resolution = TldResolutionOk;
3660 break;
3661 }
3662 case TldIdCompTime: {
3663 TldCompTime *tld_comptime = (TldCompTime *)tld;
3664 resolve_decl_comptime(g, tld_comptime);
3665
3666 tld->resolution = TldResolutionOk;
3667 break;
3668 }
34283669 case TldIdUsingNamespace: {
34293670 TldUsingNamespace *tld_using_namespace = (TldUsingNamespace *)tld;
34303671 assert(tld_using_namespace->base.parent_scope->id == ScopeIdDecls);
34313672 ScopeDecls *dest_decls_scope = (ScopeDecls *)tld_using_namespace->base.parent_scope;
34323673 preview_use_decl(g, tld_using_namespace, dest_decls_scope);
34333674 resolve_use_decl(g, tld_using_namespace, dest_decls_scope);
3675
3676 tld->resolution = TldResolutionOk;
34343677 break;
34353678 }
34363679 }
34373680
3438 tld->resolution = TldResolutionOk;
3439 g->tld_ref_source_node_stack.pop();
3681 if (g->trace_err != nullptr && source_node != nullptr && !source_node->already_traced_this_node) {
3682 g->trace_err = add_error_note(g, g->trace_err, source_node, buf_create_from_str("referenced here"));
3683 source_node->already_traced_this_node = true;
3684 }
34403685}
34413686
34423687Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name) {
......@@ -3562,7 +3807,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag)
35623807}
35633808
35643809TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) {
3565 assert(enum_type->data.enumeration.zero_bits_known);
3810 assert(type_is_resolved(enum_type, ResolveStatusZeroBitsKnown));
35663811 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
35673812 TypeEnumField *field = &enum_type->data.enumeration.fields[i];
35683813 if (bigint_cmp(&field->value, tag) == CmpEQ) {
......@@ -3631,43 +3876,6 @@ ZigType *container_ref_type(ZigType *type_entry) {
36313876 type_entry->data.pointer.child_type : type_entry;
36323877}
36333878
3634Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
3635 switch (type_entry->id) {
3636 case ZigTypeIdStruct:
3637 return resolve_struct_type(g, type_entry);
3638 case ZigTypeIdEnum:
3639 return resolve_enum_zero_bits(g, type_entry);
3640 case ZigTypeIdUnion:
3641 return resolve_union_type(g, type_entry);
3642 case ZigTypeIdPointer:
3643 case ZigTypeIdMetaType:
3644 case ZigTypeIdVoid:
3645 case ZigTypeIdBool:
3646 case ZigTypeIdUnreachable:
3647 case ZigTypeIdInt:
3648 case ZigTypeIdFloat:
3649 case ZigTypeIdArray:
3650 case ZigTypeIdComptimeFloat:
3651 case ZigTypeIdComptimeInt:
3652 case ZigTypeIdEnumLiteral:
3653 case ZigTypeIdUndefined:
3654 case ZigTypeIdNull:
3655 case ZigTypeIdOptional:
3656 case ZigTypeIdErrorUnion:
3657 case ZigTypeIdErrorSet:
3658 case ZigTypeIdFn:
3659 case ZigTypeIdBoundFn:
3660 case ZigTypeIdInvalid:
3661 case ZigTypeIdArgTuple:
3662 case ZigTypeIdOpaque:
3663 case ZigTypeIdVector:
3664 case ZigTypeIdFnFrame:
3665 case ZigTypeIdAnyFrame:
3666 zig_unreachable();
3667 }
3668 zig_unreachable();
3669}
3670
36713879ZigType *get_src_ptr_type(ZigType *type) {
36723880 if (type->id == ZigTypeIdPointer) return type;
36733881 if (type->id == ZigTypeIdFn) return type;
......@@ -3803,6 +4011,13 @@ static void resolve_async_fn_frame(CodeGen *g, ZigFn *fn) {
38034011 ZigType *frame_type = get_fn_frame_type(g, fn);
38044012 Error err;
38054013 if ((err = type_resolve(g, frame_type, ResolveStatusSizeKnown))) {
4014 if (g->trace_err != nullptr && frame_type->data.frame.resolve_loop_src_node != nullptr &&
4015 !frame_type->data.frame.reported_loop_err)
4016 {
4017 frame_type->data.frame.reported_loop_err = true;
4018 g->trace_err = add_error_note(g, g->trace_err, frame_type->data.frame.resolve_loop_src_node,
4019 buf_sprintf("when analyzing type '%s' here", buf_ptr(&frame_type->name)));
4020 }
38064021 fn->anal_state = FnAnalStateInvalid;
38074022 return;
38084023 }
......@@ -3918,7 +4133,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
39184133 &fn->analyzed_executable, fn_type_id->return_type, return_type_node);
39194134 fn->src_implicit_return_type = block_return_type;
39204135
3921 if (type_is_invalid(block_return_type) || fn->analyzed_executable.invalid) {
4136 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
39224137 assert(g->errors.length > 0);
39234138 fn->anal_state = FnAnalStateInvalid;
39244139 return;
......@@ -4002,7 +4217,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
40024217 assert(!fn_type->data.fn.is_generic);
40034218
40044219 ir_gen_fn(g, fn_table_entry);
4005 if (fn_table_entry->ir_executable.invalid) {
4220 if (fn_table_entry->ir_executable.first_err_trace_msg != nullptr) {
40064221 fn_table_entry->anal_state = FnAnalStateInvalid;
40074222 return;
40084223 }
......@@ -4140,12 +4355,14 @@ void semantic_analyze(CodeGen *g) {
41404355 {
41414356 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
41424357 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
4358 g->trace_err = nullptr;
41434359 AstNode *source_node = nullptr;
4144 resolve_top_level_decl(g, tld, source_node);
4360 resolve_top_level_decl(g, tld, source_node, false);
41454361 }
41464362
41474363 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
41484364 ZigFn *fn_entry = g->fn_defs.at(g->fn_defs_index);
4365 g->trace_err = nullptr;
41494366 analyze_fn_body(g, fn_entry);
41504367 }
41514368 }
......@@ -4157,6 +4374,7 @@ void semantic_analyze(CodeGen *g) {
41574374 // second pass over functions for detecting async
41584375 for (g->fn_defs_index = 0; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
41594376 ZigFn *fn = g->fn_defs.at(g->fn_defs_index);
4377 g->trace_err = nullptr;
41604378 analyze_fn_async(g, fn, true);
41614379 if (fn_is_async(fn) && fn->non_async_node != nullptr) {
41624380 ErrorMsg *msg = add_node_error(g, fn->proto_node,
......@@ -4802,7 +5020,10 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
48025020 case ZigTypeIdStruct:
48035021 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
48045022 TypeStructField *field = &type_entry->data.structure.fields[i];
4805 switch (type_has_one_possible_value(g, field->type_entry)) {
5023 OnePossibleValue opv = (field->type_entry != nullptr) ?
5024 type_has_one_possible_value(g, field->type_entry) :
5025 type_val_resolve_has_one_possible_value(g, field->type_val);
5026 switch (opv) {
48065027 case OnePossibleValueInvalid:
48075028 return OnePossibleValueInvalid;
48085029 case OnePossibleValueNo:
......@@ -4828,18 +5049,19 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
48285049 case ZigTypeIdUnion:
48295050 if (type_entry->data.unionation.src_field_count > 1)
48305051 return OnePossibleValueNo;
4831 return type_has_one_possible_value(g, type_entry->data.unionation.fields[0].type_entry);
5052 TypeUnionField *only_field = &type_entry->data.unionation.fields[0];
5053 if (only_field->type_entry != nullptr) {
5054 return type_has_one_possible_value(g, only_field->type_entry);
5055 }
5056 return type_val_resolve_has_one_possible_value(g, only_field->type_val);
48325057 }
48335058 zig_unreachable();
48345059}
48355060
4836ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
5061ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty) {
48375062 Error err;
4838 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
4839 return ReqCompTimeInvalid;
4840 switch (type_entry->id) {
5063 switch (ty->id) {
48415064 case ZigTypeIdInvalid:
4842 case ZigTypeIdOpaque:
48435065 zig_unreachable();
48445066 case ZigTypeIdComptimeFloat:
48455067 case ZigTypeIdComptimeInt:
......@@ -4851,23 +5073,36 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
48515073 case ZigTypeIdArgTuple:
48525074 return ReqCompTimeYes;
48535075 case ZigTypeIdArray:
4854 return type_requires_comptime(g, type_entry->data.array.child_type);
5076 return type_requires_comptime(g, ty->data.array.child_type);
48555077 case ZigTypeIdStruct:
4856 return type_entry->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
5078 if (ty->data.structure.resolve_loop_flag_zero_bits) {
5079 // Does a struct which contains a pointer field to itself require comptime? No.
5080 return ReqCompTimeNo;
5081 }
5082 if ((err = type_resolve(g, ty, ResolveStatusZeroBitsKnown)))
5083 return ReqCompTimeInvalid;
5084 return ty->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
48575085 case ZigTypeIdUnion:
4858 return type_entry->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
5086 if (ty->data.unionation.resolve_loop_flag_zero_bits) {
5087 // Does a union which contains a pointer field to itself require comptime? No.
5088 return ReqCompTimeNo;
5089 }
5090 if ((err = type_resolve(g, ty, ResolveStatusZeroBitsKnown)))
5091 return ReqCompTimeInvalid;
5092 return ty->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
48595093 case ZigTypeIdOptional:
4860 return type_requires_comptime(g, type_entry->data.maybe.child_type);
5094 return type_requires_comptime(g, ty->data.maybe.child_type);
48615095 case ZigTypeIdErrorUnion:
4862 return type_requires_comptime(g, type_entry->data.error_union.payload_type);
5096 return type_requires_comptime(g, ty->data.error_union.payload_type);
48635097 case ZigTypeIdPointer:
4864 if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) {
5098 if (ty->data.pointer.child_type->id == ZigTypeIdOpaque) {
48655099 return ReqCompTimeNo;
48665100 } else {
4867 return type_requires_comptime(g, type_entry->data.pointer.child_type);
5101 return type_requires_comptime(g, ty->data.pointer.child_type);
48685102 }
48695103 case ZigTypeIdFn:
4870 return type_entry->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo;
5104 return ty->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo;
5105 case ZigTypeIdOpaque:
48715106 case ZigTypeIdEnum:
48725107 case ZigTypeIdErrorSet:
48735108 case ZigTypeIdBool:
......@@ -5155,34 +5390,6 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
51555390}
51565391
51575392
5158void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
5159 Error err;
5160 ZigType *wanted_type = const_val->type;
5161 if (wanted_type->id == ZigTypeIdArray) {
5162 const_val->special = ConstValSpecialStatic;
5163 const_val->data.x_array.special = ConstArraySpecialUndef;
5164 } else if (wanted_type->id == ZigTypeIdStruct) {
5165 if ((err = ensure_complete_type(g, wanted_type))) {
5166 return;
5167 }
5168
5169 const_val->special = ConstValSpecialStatic;
5170 size_t field_count = wanted_type->data.structure.src_field_count;
5171 const_val->data.x_struct.fields = create_const_vals(field_count);
5172 for (size_t i = 0; i < field_count; i += 1) {
5173 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
5174 field_val->type = wanted_type->data.structure.fields[i].type_entry;
5175 assert(field_val->type);
5176 init_const_undefined(g, field_val);
5177 field_val->parent.id = ConstParentIdStruct;
5178 field_val->parent.data.p_struct.struct_val = const_val;
5179 field_val->parent.data.p_struct.field_index = i;
5180 }
5181 } else {
5182 const_val->special = ConstValSpecialUndef;
5183 }
5184}
5185
51865393ConstExprValue *create_const_vals(size_t count) {
51875394 ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count);
51885395 ConstExprValue *vals = allocate<ConstExprValue>(count);
......@@ -5192,10 +5399,6 @@ ConstExprValue *create_const_vals(size_t count) {
51925399 return vals;
51935400}
51945401
5195Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {
5196 return type_resolve(g, type_entry, ResolveStatusSizeKnown);
5197}
5198
51995402static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
52005403 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)
52015404 return orig_fn_type;
......@@ -5209,27 +5412,6 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
52095412 return fn_type;
52105413}
52115414
5212static void emit_error_notes_for_type_loop(CodeGen *g, ErrorMsg *msg, ZigType *stop_type,
5213 ZigType *ty, AstNode *src_node)
5214{
5215 ErrorMsg *note = add_error_note(g, msg, src_node,
5216 buf_sprintf("when analyzing type '%s' here", buf_ptr(&ty->name)));
5217 if (ty == stop_type)
5218 return;
5219 switch (ty->id) {
5220 case ZigTypeIdFnFrame: {
5221 ty->data.frame.reported_loop_err = true;
5222 ZigType *depending_type = ty->data.frame.resolve_loop_type;
5223 if (depending_type == nullptr)
5224 return;
5225 emit_error_notes_for_type_loop(g, note, stop_type,
5226 depending_type, ty->data.frame.resolve_loop_src_node);
5227 }
5228 default:
5229 return;
5230 }
5231}
5232
52335415static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52345416 Error err;
52355417
......@@ -5241,14 +5423,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52415423
52425424 if (frame_type->data.frame.resolve_loop_type != nullptr) {
52435425 if (!frame_type->data.frame.reported_loop_err) {
5244 frame_type->data.frame.reported_loop_err = true;
5245 ErrorMsg *msg = add_node_error(g, fn->proto_node,
5426 add_node_error(g, fn->proto_node,
52465427 buf_sprintf("'%s' depends on itself", buf_ptr(&frame_type->name)));
5247 emit_error_notes_for_type_loop(g, msg,
5248 frame_type,
5249 frame_type->data.frame.resolve_loop_type,
5250 frame_type->data.frame.resolve_loop_src_node);
5251 emit_error_notes_for_ref_stack(g, msg);
52525428 }
52535429 return ErrorSemanticAnalyzeFail;
52545430 }
......@@ -5264,11 +5440,9 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
52645440 return ErrorSemanticAnalyzeFail;
52655441 break;
52665442 case FnAnalStateProbing: {
5267 ErrorMsg *msg = add_node_error(g, fn->proto_node,
5443 add_node_error(g, fn->proto_node,
52685444 buf_sprintf("cannot resolve '%s': function not fully analyzed yet",
52695445 buf_ptr(&frame_type->name)));
5270 ir_add_analysis_trace(fn->ir_executable.analysis, msg,
5271 buf_sprintf("depends on its own frame here"));
52725446 return ErrorSemanticAnalyzeFail;
52735447 }
52745448 }
......@@ -5339,10 +5513,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
53395513 if (callee->anal_state == FnAnalStateProbing) {
53405514 ErrorMsg *msg = add_node_error(g, fn->proto_node,
53415515 buf_sprintf("unable to determine async function frame of '%s'", buf_ptr(&fn->symbol_name)));
5342 ErrorMsg *note = add_error_note(g, msg, call->base.source_node,
5516 g->trace_err = add_error_note(g, msg, call->base.source_node,
53435517 buf_sprintf("analysis of function '%s' depends on the frame", buf_ptr(&callee->symbol_name)));
5344 ir_add_analysis_trace(callee->ir_executable.analysis, note,
5345 buf_sprintf("depends on the frame here"));
53465518 return ErrorSemanticAnalyzeFail;
53475519 }
53485520
......@@ -5454,6 +5626,37 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
54545626 return ErrorNone;
54555627}
54565628
5629static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) {
5630 Error err;
5631
5632 if (ty->abi_size != SIZE_MAX)
5633 return ErrorNone;
5634
5635 if (ty->data.pointer.resolve_loop_flag_zero_bits) {
5636 ty->abi_size = g->builtin_types.entry_usize->abi_size;
5637 ty->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
5638 ty->abi_align = g->builtin_types.entry_usize->abi_align;
5639 return ErrorNone;
5640 }
5641 ty->data.pointer.resolve_loop_flag_zero_bits = true;
5642
5643 ZigType *elem_type = ty->data.pointer.child_type;
5644
5645 if ((err = type_resolve(g, elem_type, ResolveStatusZeroBitsKnown)))
5646 return err;
5647
5648 if (type_has_bits(elem_type)) {
5649 ty->abi_size = g->builtin_types.entry_usize->abi_size;
5650 ty->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
5651 ty->abi_align = g->builtin_types.entry_usize->abi_align;
5652 } else {
5653 ty->abi_size = 0;
5654 ty->size_in_bits = 0;
5655 ty->abi_align = 0;
5656 }
5657 return ErrorNone;
5658}
5659
54575660Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
54585661 if (type_is_invalid(ty))
54595662 return ErrorSemanticAnalyzeFail;
......@@ -5463,36 +5666,48 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
54635666 case ResolveStatusInvalid:
54645667 zig_unreachable();
54655668 case ResolveStatusZeroBitsKnown:
5466 if (ty->id == ZigTypeIdStruct) {
5467 return resolve_struct_zero_bits(g, ty);
5468 } else if (ty->id == ZigTypeIdEnum) {
5469 return resolve_enum_zero_bits(g, ty);
5470 } else if (ty->id == ZigTypeIdUnion) {
5471 return resolve_union_zero_bits(g, ty);
5669 switch (ty->id) {
5670 case ZigTypeIdStruct:
5671 return resolve_struct_zero_bits(g, ty);
5672 case ZigTypeIdEnum:
5673 return resolve_enum_zero_bits(g, ty);
5674 case ZigTypeIdUnion:
5675 return resolve_union_zero_bits(g, ty);
5676 case ZigTypeIdPointer:
5677 return resolve_pointer_zero_bits(g, ty);
5678 default:
5679 return ErrorNone;
54725680 }
5473 return ErrorNone;
54745681 case ResolveStatusAlignmentKnown:
5475 if (ty->id == ZigTypeIdStruct) {
5476 return resolve_struct_alignment(g, ty);
5477 } else if (ty->id == ZigTypeIdEnum) {
5478 return resolve_enum_zero_bits(g, ty);
5479 } else if (ty->id == ZigTypeIdUnion) {
5480 return resolve_union_alignment(g, ty);
5481 } else if (ty->id == ZigTypeIdFnFrame) {
5482 return resolve_async_frame(g, ty);
5682 switch (ty->id) {
5683 case ZigTypeIdStruct:
5684 return resolve_struct_alignment(g, ty);
5685 case ZigTypeIdEnum:
5686 return resolve_enum_zero_bits(g, ty);
5687 case ZigTypeIdUnion:
5688 return resolve_union_alignment(g, ty);
5689 case ZigTypeIdFnFrame:
5690 return resolve_async_frame(g, ty);
5691 case ZigTypeIdPointer:
5692 return resolve_pointer_zero_bits(g, ty);
5693 default:
5694 return ErrorNone;
54835695 }
5484 return ErrorNone;
54855696 case ResolveStatusSizeKnown:
5486 if (ty->id == ZigTypeIdStruct) {
5487 return resolve_struct_type(g, ty);
5488 } else if (ty->id == ZigTypeIdEnum) {
5489 return resolve_enum_zero_bits(g, ty);
5490 } else if (ty->id == ZigTypeIdUnion) {
5491 return resolve_union_type(g, ty);
5492 } else if (ty->id == ZigTypeIdFnFrame) {
5493 return resolve_async_frame(g, ty);
5697 switch (ty->id) {
5698 case ZigTypeIdStruct:
5699 return resolve_struct_type(g, ty);
5700 case ZigTypeIdEnum:
5701 return resolve_enum_zero_bits(g, ty);
5702 case ZigTypeIdUnion:
5703 return resolve_union_type(g, ty);
5704 case ZigTypeIdFnFrame:
5705 return resolve_async_frame(g, ty);
5706 case ZigTypeIdPointer:
5707 return resolve_pointer_zero_bits(g, ty);
5708 default:
5709 return ErrorNone;
54945710 }
5495 return ErrorNone;
54965711 case ResolveStatusLLVMFwdDecl:
54975712 case ResolveStatusLLVMFull:
54985713 resolve_llvm_types(g, ty, status);
......@@ -5867,6 +6082,9 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
58676082 case ConstValSpecialRuntime:
58686083 buf_appendf(buf, "(runtime value)");
58696084 return;
6085 case ConstValSpecialLazy:
6086 buf_appendf(buf, "(lazy value)");
6087 return;
58706088 case ConstValSpecialUndef:
58716089 buf_appendf(buf, "undefined");
58726090 return;
......@@ -6241,6 +6459,40 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
62416459 zig_unreachable();
62426460}
62436461
6462static void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
6463 Error err;
6464 ZigType *wanted_type = const_val->type;
6465 if (wanted_type->id == ZigTypeIdArray) {
6466 const_val->special = ConstValSpecialStatic;
6467 const_val->data.x_array.special = ConstArraySpecialUndef;
6468 } else if (wanted_type->id == ZigTypeIdStruct) {
6469 if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) {
6470 return;
6471 }
6472
6473 const_val->special = ConstValSpecialStatic;
6474 size_t field_count = wanted_type->data.structure.src_field_count;
6475 const_val->data.x_struct.fields = create_const_vals(field_count);
6476 for (size_t i = 0; i < field_count; i += 1) {
6477 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
6478 field_val->type = wanted_type->data.structure.fields[i].type_entry;
6479 assert(field_val->type);
6480 init_const_undefined(g, field_val);
6481 field_val->parent.id = ConstParentIdStruct;
6482 field_val->parent.data.p_struct.struct_val = const_val;
6483 field_val->parent.data.p_struct.field_index = i;
6484 }
6485 } else {
6486 const_val->special = ConstValSpecialUndef;
6487 }
6488}
6489
6490void expand_undef_struct(CodeGen *g, ConstExprValue *const_val) {
6491 if (const_val->special == ConstValSpecialUndef) {
6492 init_const_undefined(g, const_val);
6493 }
6494}
6495
62446496// Canonicalize the array value as ConstArraySpecialNone
62456497void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
62466498 size_t elem_count;
......@@ -6504,7 +6756,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
65046756
65056757ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
65066758 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
6507 resolve_top_level_decl(codegen, tld, nullptr);
6759 resolve_top_level_decl(codegen, tld, nullptr, false);
65086760 assert(tld->id == TldIdVar);
65096761 TldVar *tld_var = (TldVar *)tld;
65106762 ConstExprValue *var_value = tld_var->var->const_value;
......@@ -6719,19 +6971,6 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) {
67196971 return false;
67206972}
67216973
6722void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg) {
6723 size_t i = g->tld_ref_source_node_stack.length;
6724 for (;;) {
6725 if (i == 0)
6726 break;
6727 i -= 1;
6728 AstNode *source_node = g->tld_ref_source_node_stack.at(i);
6729 if (source_node) {
6730 msg = add_error_note(g, msg, source_node, buf_sprintf("referenced here"));
6731 }
6732 }
6733}
6734
67356974Buf *type_bare_name(ZigType *type_entry) {
67366975 if (is_slice(type_entry)) {
67376976 return &type_entry->name;
......@@ -7134,10 +7373,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
71347373 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
71357374}
71367375
7137static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
7138 assert(!enum_type->data.enumeration.is_invalid);
7139 assert(enum_type->data.enumeration.complete);
7140 if (enum_type->llvm_di_type != nullptr) return;
7376static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatus wanted_resolve_status) {
7377 assert(enum_type->data.enumeration.resolve_status >= ResolveStatusSizeKnown);
7378 if (enum_type->data.enumeration.resolve_status >= wanted_resolve_status) return;
71417379
71427380 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
71437381 ZigType *import = get_scope_import(scope);
......@@ -7158,6 +7396,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
71587396 debug_align_in_bits,
71597397 ZigLLVM_DIFlags_Zero,
71607398 nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
7399 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
71617400 return;
71627401 }
71637402
......@@ -7190,14 +7429,16 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
71907429 get_llvm_di_type(g, tag_int_type), "");
71917430
71927431 enum_type->llvm_di_type = tag_di_type;
7432 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
71937433}
71947434
71957435static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
71967436 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
71977437
7198 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
7438 TypeUnionField *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
71997439 ZigType *tag_type = union_type->data.unionation.tag_type;
7200 if (most_aligned_union_member == nullptr) {
7440 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
7441 if (gen_field_count == 0) {
72017442 union_type->llvm_type = get_llvm_type(g, tag_type);
72027443 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
72037444 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
......@@ -7221,7 +7462,6 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
72217462 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
72227463 }
72237464
7224 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
72257465 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
72267466 uint32_t field_count = union_type->data.unionation.src_field_count;
72277467 for (uint32_t i = 0; i < field_count; i += 1) {
......@@ -7248,17 +7488,17 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
72487488 if (tag_type == nullptr || !type_has_bits(tag_type)) {
72497489 assert(most_aligned_union_member != nullptr);
72507490
7251 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
7491 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size;
72527492 if (padding_bytes > 0) {
72537493 ZigType *u8_type = get_int_type(g, false, 8);
72547494 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
72557495 LLVMTypeRef union_element_types[] = {
7256 most_aligned_union_member->llvm_type,
7496 most_aligned_union_member->type_entry->llvm_type,
72577497 get_llvm_type(g, padding_array),
72587498 };
72597499 LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false);
72607500 } else {
7261 LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false);
7501 LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->type_entry->llvm_type, 1, false);
72627502 }
72637503 union_type->data.unionation.union_llvm_type = union_type->llvm_type;
72647504 union_type->data.unionation.gen_tag_index = SIZE_MAX;
......@@ -7269,7 +7509,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
72697509 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
72707510 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
72717511 union_type->data.unionation.union_abi_size * 8,
7272 most_aligned_union_member->abi_align * 8,
7512 most_aligned_union_member->align * 8,
72737513 ZigLLVM_DIFlags_Zero, union_inner_di_types,
72747514 gen_field_count, 0, "");
72757515
......@@ -7280,14 +7520,14 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
72807520 }
72817521
72827522 LLVMTypeRef union_type_ref;
7283 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
7523 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size;
72847524 if (padding_bytes == 0) {
7285 union_type_ref = get_llvm_type(g, most_aligned_union_member);
7525 union_type_ref = get_llvm_type(g, most_aligned_union_member->type_entry);
72867526 } else {
72877527 ZigType *u8_type = get_int_type(g, false, 8);
72887528 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
72897529 LLVMTypeRef union_element_types[] = {
7290 get_llvm_type(g, most_aligned_union_member),
7530 get_llvm_type(g, most_aligned_union_member->type_entry),
72917531 get_llvm_type(g, padding_array),
72927532 };
72937533 union_type_ref = LLVMStructType(union_element_types, 2, false);
......@@ -7303,7 +7543,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
73037543 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
73047544 ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion",
73057545 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
7306 most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align,
7546 most_aligned_union_member->type_entry->size_in_bits, 8*most_aligned_union_member->align,
73077547 ZigLLVM_DIFlags_Zero, union_inner_di_types, gen_field_count, 0, "");
73087548
73097549 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
......@@ -7314,8 +7554,8 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
73147554 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
73157555 ZigLLVMTypeToScope(union_type->llvm_di_type), "payload",
73167556 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
7317 most_aligned_union_member->size_in_bits,
7318 8*most_aligned_union_member->abi_align,
7557 most_aligned_union_member->type_entry->size_in_bits,
7558 8*most_aligned_union_member->align,
73197559 union_offset_in_bits,
73207560 ZigLLVM_DIFlags_Zero, union_di_type);
73217561
......@@ -7352,6 +7592,9 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
73527592static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
73537593 if (type->llvm_di_type != nullptr) return;
73547594
7595 if (resolve_pointer_zero_bits(g, type) != ErrorNone)
7596 zig_unreachable();
7597
73557598 if (!type_has_bits(type)) {
73567599 type->llvm_type = g->builtin_types.entry_void->llvm_type;
73577600 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
......@@ -7921,7 +8164,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
79218164 else
79228165 return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr);
79238166 case ZigTypeIdEnum:
7924 return resolve_llvm_types_enum(g, type);
8167 return resolve_llvm_types_enum(g, type, wanted_resolve_status);
79258168 case ZigTypeIdUnion:
79268169 return resolve_llvm_types_union(g, type, wanted_resolve_status);
79278170 case ZigTypeIdPointer:
src/analyze.hpp+8-8
......@@ -11,10 +11,9 @@
1111#include "all_types.hpp"
1212
1313void semantic_analyze(CodeGen *g);
14ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg);
14ErrorMsg *add_node_error(CodeGen *g, 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);
......@@ -59,7 +58,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Bu
5958ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
6059Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
6160Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
62void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
61void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy);
6362
6463ZigType *get_src_ptr_type(ZigType *type);
6564ZigType *get_codegen_ptr_type(ZigType *type);
......@@ -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);
......@@ -244,9 +240,13 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
244240
245241void src_assert(bool ok, AstNode *source_node);
246242bool is_container(ZigType *type_entry);
247ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name);
243ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
244 Buf *type_name, UndefAllowed undef);
248245
249246void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn);
250247bool fn_is_async(ZigFn *fn);
251248
249Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t *abi_align);
250ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field);
251
252252#endif
src/codegen.cpp+9-2
......@@ -3386,6 +3386,8 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
33863386
33873387static bool value_is_all_undef(ConstExprValue *const_val) {
33883388 switch (const_val->special) {
3389 case ConstValSpecialLazy:
3390 zig_unreachable();
33893391 case ConstValSpecialRuntime:
33903392 return false;
33913393 case ConstValSpecialUndef:
......@@ -3525,6 +3527,8 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
35253527}
35263528
35273529static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrInstructionVarPtr *instruction) {
3530 if (instruction->base.value.special != ConstValSpecialRuntime)
3531 return ir_llvm_value(g, &instruction->base);
35283532 ZigVar *var = instruction->var;
35293533 if (type_has_bits(var->var_type)) {
35303534 assert(var->value_ref);
......@@ -6041,6 +6045,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
60416045
60426046static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) {
60436047 switch (const_val->special) {
6048 case ConstValSpecialLazy:
60446049 case ConstValSpecialRuntime:
60456050 zig_unreachable();
60466051 case ConstValSpecialUndef:
......@@ -6300,6 +6305,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63006305 assert(type_has_bits(type_entry));
63016306
63026307 switch (const_val->special) {
6308 case ConstValSpecialLazy:
6309 zig_unreachable();
63036310 case ConstValSpecialRuntime:
63046311 zig_unreachable();
63056312 case ConstValSpecialUndef:
......@@ -6563,7 +6570,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
65636570 uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes;
65646571 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");
65656572 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_value->type, correctly_typed_value) ||
6566 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
6573 payload_value->type != type_entry->data.unionation.most_aligned_union_member->type_entry;
65676574
65686575 {
65696576 if (pad_bytes == 0) {
......@@ -8891,7 +8898,7 @@ static void gen_root_source(CodeGen *g) {
88918898 }
88928899 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
88938900 assert(panic_tld != nullptr);
8894 resolve_top_level_decl(g, panic_tld, nullptr);
8901 resolve_top_level_decl(g, panic_tld, nullptr, false);
88958902 }
88968903
88978904
src/ir.cpp+858-508
......@@ -152,11 +152,6 @@ struct ConstCastBadAllowsZero {
152152};
153153
154154
155enum UndefAllowed {
156 UndefOk,
157 UndefBad,
158};
159
160155static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
161156static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
162157 ResultLoc *result_loc);
......@@ -234,6 +229,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
234229 }
235230 case ConstPtrSpecialBaseStruct: {
236231 ConstExprValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val;
232 expand_undef_struct(g, struct_val);
237233 result = &struct_val->data.x_struct.fields[const_val->data.x_ptr.data.base_struct.field_index];
238234 break;
239235 }
......@@ -402,7 +398,7 @@ static void ir_ref_var(ZigVar *var) {
402398ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
403399 ConstExprValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type,
404400 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr,
405 node, nullptr, ira->new_irb.exec, nullptr);
401 node, nullptr, ira->new_irb.exec, nullptr, UndefBad);
406402
407403 if (type_is_invalid(result->type))
408404 return ira->codegen->builtin_types.entry_invalid;
......@@ -6858,7 +6854,7 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, Scope *scope, AstNode *nod
68586854 uint32_t len = asm_token.end - asm_token.start - 2;
68596855
68606856 add_node_error(irb->codegen, node,
6861 buf_sprintf("could not find '%.*s' in the inputs or outputs.",
6857 buf_sprintf("could not find '%.*s' in the inputs or outputs",
68626858 len, ptr));
68636859 return irb->codegen->invalid_instruction;
68646860 }
......@@ -8111,7 +8107,12 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
81118107 ir_build_reset_result(irb, scope, node, result_loc);
81128108 }
81138109 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc);
8114 irb->exec->invalid = irb->exec->invalid || (result == irb->codegen->invalid_instruction);
8110 if (result == irb->codegen->invalid_instruction) {
8111 if (irb->exec->first_err_trace_msg == nullptr) {
8112 irb->exec->first_err_trace_msg = irb->codegen->trace_err;
8113 }
8114 src_assert(irb->exec->first_err_trace_msg != nullptr, node);
8115 }
81158116 return result;
81168117}
81178118
......@@ -8119,21 +8120,20 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {
81198120 return ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
81208121}
81218122
8122static void invalidate_exec(IrExecutable *exec) {
8123 if (exec->invalid)
8123static void invalidate_exec(IrExecutable *exec, ErrorMsg *msg) {
8124 if (exec->first_err_trace_msg != nullptr)
81248125 return;
81258126
8126 exec->invalid = true;
8127 exec->first_err_trace_msg = msg;
81278128
81288129 for (size_t i = 0; i < exec->tld_list.length; i += 1) {
81298130 exec->tld_list.items[i]->resolution = TldResolutionInvalid;
81308131 }
81318132
81328133 if (exec->source_exec != nullptr)
8133 invalidate_exec(exec->source_exec);
8134 invalidate_exec(exec->source_exec, msg);
81348135}
81358136
8136
81378137bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {
81388138 assert(node->owner);
81398139
......@@ -8151,8 +8151,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
81518151
81528152 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
81538153 assert(result);
8154 if (irb->exec->invalid)
8154 if (irb->exec->first_err_trace_msg != nullptr) {
8155 codegen->trace_err = irb->exec->first_err_trace_msg;
81558156 return false;
8157 }
81568158
81578159 if (!instr_is_unreachable(result)) {
81588160 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->source_node, result));
......@@ -8181,15 +8183,9 @@ static void ir_add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, Error
81818183 ir_add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
81828184}
81838185
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
81908186static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
8191 invalidate_exec(exec);
81928187 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
8188 invalidate_exec(exec, err_msg);
81938189 if (exec->parent_exec) {
81948190 ir_add_call_stack_errors(codegen, exec, err_msg, 10);
81958191 }
......@@ -8223,6 +8219,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast
82238219{
82248220 Error err;
82258221 assert(ptr_val->type->id == ZigTypeIdPointer);
8222 assert(ptr_val->special == ConstValSpecialStatic);
82268223 ConstExprValue tmp = {};
82278224 tmp.special = ConstValSpecialStatic;
82288225 tmp.type = ptr_val->type->data.pointer.child_type;
......@@ -9016,7 +9013,8 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
90169013 }
90179014
90189015 ConstExprValue *const_val = ir_resolve_const(ira, instruction, UndefBad);
9019 assert(const_val != nullptr);
9016 if (const_val == nullptr)
9017 return false;
90209018
90219019 bool const_val_is_int = (const_val->type->id == ZigTypeIdInt || const_val->type->id == ZigTypeIdComptimeInt);
90229020 bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
......@@ -9376,6 +9374,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
93769374 result.id = ConstCastResultIdInvalid;
93779375 return result;
93789376 }
9377 if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) {
9378 result.id = ConstCastResultIdInvalid;
9379 return result;
9380 }
9381 if ((err = type_resolve(g, actual_type, ResolveStatusZeroBitsKnown))) {
9382 result.id = ConstCastResultIdInvalid;
9383 return result;
9384 }
93799385 bool ptr_lens_equal = actual_ptr_type->data.pointer.ptr_len == wanted_ptr_type->data.pointer.ptr_len;
93809386 if ((ptr_lens_equal || wanted_is_c_ptr || actual_is_c_ptr) &&
93819387 type_has_bits(wanted_type) == type_has_bits(actual_type) &&
......@@ -10634,7 +10640,7 @@ static void ir_finish_bb(IrAnalyze *ira) {
1063410640
1063510641static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
1063610642 ira->old_bb_index = SIZE_MAX;
10637 ira->new_irb.exec->invalid = true;
10643 assert(ira->new_irb.exec->first_err_trace_msg != nullptr);
1063810644 return ira->codegen->unreach_instruction;
1063910645}
1064010646
......@@ -10722,32 +10728,57 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
1072210728 return const_instr;
1072310729}
1072410730
10731static Error ir_resolve_const_val(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
10732 ConstExprValue *val, UndefAllowed undef_allowed)
10733{
10734 Error err;
10735 for (;;) {
10736 switch (val->special) {
10737 case ConstValSpecialStatic:
10738 return ErrorNone;
10739 case ConstValSpecialRuntime:
10740 if (!type_has_bits(val->type))
10741 return ErrorNone;
10742
10743 exec_add_error_node(codegen, exec, source_node,
10744 buf_sprintf("unable to evaluate constant expression"));
10745 return ErrorSemanticAnalyzeFail;
10746 case ConstValSpecialUndef:
10747 if (undef_allowed == UndefOk || undef_allowed == LazyOk)
10748 return ErrorNone;
10749
10750 exec_add_error_node(codegen, exec, source_node,
10751 buf_sprintf("use of undefined value here causes undefined behavior"));
10752 return ErrorSemanticAnalyzeFail;
10753 case ConstValSpecialLazy:
10754 if (undef_allowed == LazyOk || undef_allowed == LazyOkNoUndef)
10755 return ErrorNone;
10756
10757 if ((err = ir_resolve_lazy(codegen, source_node, val)))
10758 return err;
10759
10760 continue;
10761 }
10762 }
10763}
10764
1072510765static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
10726 switch (value->value.special) {
10727 case ConstValSpecialStatic:
10728 return &value->value;
10729 case ConstValSpecialRuntime:
10730 if (!type_has_bits(value->value.type)) {
10731 return &value->value;
10732 }
10733 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
10734 return nullptr;
10735 case ConstValSpecialUndef:
10736 if (undef_allowed == UndefOk) {
10737 return &value->value;
10738 } else {
10739 ir_add_error(ira, value, buf_sprintf("use of undefined value here causes undefined behavior"));
10740 return nullptr;
10741 }
10766 Error err;
10767 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, value->source_node,
10768 &value->value, undef_allowed)))
10769 {
10770 return nullptr;
1074210771 }
10743 zig_unreachable();
10772 return &value->value;
1074410773}
1074510774
1074610775ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1074710776 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
1074810777 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
10749 IrExecutable *parent_exec, AstNode *expected_type_source_node)
10778 IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed)
1075010779{
10780 Error err;
10781
1075110782 if (expected_type != nullptr && type_is_invalid(expected_type))
1075210783 return &codegen->invalid_instruction->value;
1075310784
......@@ -10761,8 +10792,10 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1076110792 ir_executable->begin_scope = scope;
1076210793 ir_gen(codegen, node, scope, ir_executable);
1076310794
10764 if (ir_executable->invalid)
10795 if (ir_executable->first_err_trace_msg != nullptr) {
10796 codegen->trace_err = ir_executable->first_err_trace_msg;
1076510797 return &codegen->invalid_instruction->value;
10798 }
1076610799
1076710800 if (codegen->verbose_ir) {
1076810801 fprintf(stderr, "\nSource: ");
......@@ -10783,8 +10816,9 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1078310816 analyzed_executable->backward_branch_quota = backward_branch_quota;
1078410817 analyzed_executable->begin_scope = scope;
1078510818 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node);
10786 if (type_is_invalid(result_type))
10819 if (type_is_invalid(result_type)) {
1078710820 return &codegen->invalid_instruction->value;
10821 }
1078810822
1078910823 if (codegen->verbose_ir) {
1079010824 fprintf(stderr, "{ // (analyzed)\n");
......@@ -10792,7 +10826,14 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1079210826 fprintf(stderr, "}\n");
1079310827 }
1079410828
10795 return ir_exec_const_result(codegen, analyzed_executable);
10829 ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable);
10830 if (type_is_invalid(result->type))
10831 return &codegen->invalid_instruction->value;
10832
10833 if ((err = ir_resolve_const_val(codegen, analyzed_executable, node, result, undef_allowed)))
10834 return &codegen->invalid_instruction->value;
10835
10836 return result;
1079610837}
1079710838
1079810839static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstruction *err_value) {
......@@ -10813,22 +10854,43 @@ static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstruction *err_valu
1081310854 return const_val->data.x_err_set;
1081410855}
1081510856
10816static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
10857static ZigType *ir_resolve_const_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
10858 ConstExprValue *val)
10859{
10860 Error err;
10861 if ((err = ir_resolve_const_val(codegen, exec, source_node, val, UndefBad)))
10862 return codegen->builtin_types.entry_invalid;
10863
10864 assert(val->data.x_type != nullptr);
10865 return val->data.x_type;
10866}
10867
10868static ConstExprValue *ir_resolve_type_lazy(IrAnalyze *ira, IrInstruction *type_value) {
1081710869 if (type_is_invalid(type_value->value.type))
10818 return ira->codegen->builtin_types.entry_invalid;
10870 return nullptr;
1081910871
1082010872 if (type_value->value.type->id != ZigTypeIdMetaType) {
1082110873 ir_add_error(ira, type_value,
1082210874 buf_sprintf("expected type 'type', found '%s'", buf_ptr(&type_value->value.type->name)));
10823 return ira->codegen->builtin_types.entry_invalid;
10875 return nullptr;
1082410876 }
1082510877
10826 ConstExprValue *const_val = ir_resolve_const(ira, type_value, UndefBad);
10827 if (!const_val)
10878 Error err;
10879 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, type_value->source_node,
10880 &type_value->value, LazyOk)))
10881 {
10882 return nullptr;
10883 }
10884
10885 return &type_value->value;
10886}
10887
10888static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
10889 ConstExprValue *val = ir_resolve_type_lazy(ira, type_value);
10890 if (val == nullptr)
1082810891 return ira->codegen->builtin_types.entry_invalid;
1082910892
10830 assert(const_val->data.x_type != nullptr);
10831 return const_val->data.x_type;
10893 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val);
1083210894}
1083310895
1083410896static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) {
......@@ -11026,14 +11088,21 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou
1102611088}
1102711089
1102811090static IrInstruction *ir_analyze_frame_ptr_to_anyframe(IrAnalyze *ira, IrInstruction *source_instr,
11029 IrInstruction *value, ZigType *wanted_type)
11091 IrInstruction *frame_ptr, ZigType *wanted_type)
1103011092{
11031 if (instr_is_comptime(value)) {
11032 zig_panic("TODO comptime frame pointer");
11093 if (instr_is_comptime(frame_ptr)) {
11094 ConstExprValue *ptr_val = ir_resolve_const(ira, frame_ptr, UndefBad);
11095 if (ptr_val == nullptr)
11096 return ira->codegen->invalid_instruction;
11097
11098 ir_assert(ptr_val->type->id == ZigTypeIdPointer, source_instr);
11099 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
11100 zig_panic("TODO comptime frame pointer");
11101 }
1103311102 }
1103411103
1103511104 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node,
11036 wanted_type, value, CastOpBitCast);
11105 wanted_type, frame_ptr, CastOpBitCast);
1103711106 result->value.type = wanted_type;
1103811107 return result;
1103911108}
......@@ -11152,6 +11221,9 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
1115211221 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type,
1115311222 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
1115411223
11224 if ((err = type_resolve(ira->codegen, ptr_type, ResolveStatusZeroBitsKnown)))
11225 return ira->codegen->invalid_instruction;
11226
1115511227 IrInstruction *result_loc;
1115611228 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) {
1115711229 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true,
......@@ -11322,7 +11394,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc
1132211394 IrInstruction *target, ZigType *wanted_type)
1132311395{
1132411396 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
11325 init_const_undefined(ira->codegen, &result->value);
11397 result->value.special = ConstValSpecialUndef;
1132611398 return result;
1132711399}
1132811400
......@@ -11345,10 +11417,13 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1134511417 return ira->codegen->invalid_instruction;
1134611418 TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag);
1134711419 assert(union_field != nullptr);
11348 if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown)))
11420 ZigType *field_type = resolve_union_field_type(ira->codegen, union_field);
11421 if (field_type == nullptr)
11422 return ira->codegen->invalid_instruction;
11423 if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown)))
1134911424 return ira->codegen->invalid_instruction;
1135011425
11351 switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) {
11426 switch (type_has_one_possible_value(ira->codegen, field_type)) {
1135211427 case OnePossibleValueInvalid:
1135311428 return ira->codegen->invalid_instruction;
1135411429 case OnePossibleValueNo: {
......@@ -11357,7 +11432,7 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1135711432 ErrorMsg *msg = ir_add_error(ira, source_instr,
1135811433 buf_sprintf("cast to union '%s' must initialize '%s' field '%s'",
1135911434 buf_ptr(&wanted_type->name),
11360 buf_ptr(&union_field->type_entry->name),
11435 buf_ptr(&field_type->name),
1136111436 buf_ptr(union_field->name)));
1136211437 add_error_note(ira->codegen, msg, field_node,
1136311438 buf_sprintf("field '%s' declared here", buf_ptr(union_field->name)));
......@@ -11373,7 +11448,7 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1137311448 bigint_init_bigint(&result->value.data.x_union.tag, &val->data.x_enum_tag);
1137411449 result->value.data.x_union.payload = create_const_vals(1);
1137511450 result->value.data.x_union.payload->special = ConstValSpecialStatic;
11376 result->value.data.x_union.payload->type = union_field->type_entry;
11451 result->value.data.x_union.payload->type = field_type;
1137711452 return result;
1137811453 }
1137911454
......@@ -11390,12 +11465,17 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1139011465 buf_ptr(&wanted_type->name)));
1139111466 for (uint32_t i = 0; i < wanted_type->data.unionation.src_field_count; i += 1) {
1139211467 TypeUnionField *union_field = &wanted_type->data.unionation.fields[i];
11393 if (type_has_bits(union_field->type_entry)) {
11468 ZigType *field_type = resolve_union_field_type(ira->codegen, union_field);
11469 if (field_type == nullptr)
11470 return ira->codegen->invalid_instruction;
11471 if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown)))
11472 return ira->codegen->invalid_instruction;
11473 if (type_has_bits(field_type)) {
1139411474 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(i);
1139511475 add_error_note(ira->codegen, msg, field_node,
1139611476 buf_sprintf("field '%s' has type '%s'",
1139711477 buf_ptr(union_field->name),
11398 buf_ptr(&union_field->type_entry->name)));
11478 buf_ptr(&field_type->name)));
1139911479 }
1140011480 }
1140111481 return ira->codegen->invalid_instruction;
......@@ -11461,7 +11541,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
1146111541
1146211542 ZigType *actual_type = target->value.type;
1146311543
11464 if ((err = ensure_complete_type(ira->codegen, wanted_type)))
11544 if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown)))
1146511545 return ira->codegen->invalid_instruction;
1146611546
1146711547 if (actual_type != wanted_type->data.enumeration.tag_int_type) {
......@@ -12508,26 +12588,22 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1250812588 return ir_build_load_ptr_gen(ira, source_instruction, ptr, child_type, result_loc_inst);
1250912589}
1251012590
12511static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
12512 if (type_is_invalid(value->value.type))
12513 return false;
12514
12515 IrInstruction *casted_value = ir_implicit_cast(ira, value, get_align_amt_type(ira->codegen));
12516 if (type_is_invalid(casted_value->value.type))
12517 return false;
12518
12519 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
12520 if (!const_val)
12591static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
12592 ConstExprValue *const_val, uint32_t *out)
12593{
12594 Error err;
12595 if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad)))
1252112596 return false;
1252212597
1252312598 uint32_t align_bytes = bigint_as_u32(&const_val->data.x_bigint);
1252412599 if (align_bytes == 0) {
12525 ir_add_error(ira, value, buf_sprintf("alignment must be >= 1"));
12600 exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment must be >= 1"));
1252612601 return false;
1252712602 }
1252812603
1252912604 if (!is_power_of_2(align_bytes)) {
12530 ir_add_error(ira, value, buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes));
12605 exec_add_error_node(codegen, exec, source_node,
12606 buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes));
1253112607 return false;
1253212608 }
1253312609
......@@ -12535,6 +12611,18 @@ static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out
1253512611 return true;
1253612612}
1253712613
12614static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
12615 if (type_is_invalid(value->value.type))
12616 return false;
12617
12618 IrInstruction *casted_value = ir_implicit_cast(ira, value, get_align_amt_type(ira->codegen));
12619 if (type_is_invalid(casted_value->value.type))
12620 return false;
12621
12622 return ir_resolve_const_align(ira->codegen, ira->new_irb.exec, value->source_node,
12623 &casted_value->value, out);
12624}
12625
1253812626static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) {
1253912627 if (type_is_invalid(value->value.type))
1254012628 return false;
......@@ -12719,7 +12807,9 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
1271912807 if (type_is_invalid(operand->value.type))
1272012808 return ir_unreach_error(ira);
1272112809
12722 if (!instr_is_comptime(operand) && handle_is_ptr(ira->explicit_return_type)) {
12810 if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr &&
12811 handle_is_ptr(ira->explicit_return_type))
12812 {
1272312813 // result location mechanism took care of it.
1272412814 IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
1272512815 instruction->base.source_node, nullptr);
......@@ -13576,21 +13666,34 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1357613666 if (type_is_invalid(casted_op2->value.type))
1357713667 return ira->codegen->invalid_instruction;
1357813668
13579 if (op1->value.special == ConstValSpecialUndef || casted_op2->value.special == ConstValSpecialUndef) {
13580 IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type);
13581 result->value.special = ConstValSpecialUndef;
13582 return result;
13669 // If either operand is undef, result is undef.
13670 ConstExprValue *op1_val = nullptr;
13671 ConstExprValue *op2_val = nullptr;
13672 if (instr_is_comptime(op1)) {
13673 op1_val = ir_resolve_const(ira, op1, UndefOk);
13674 if (op1_val == nullptr)
13675 return ira->codegen->invalid_instruction;
13676 if (op1_val->special == ConstValSpecialUndef)
13677 return ir_const_undef(ira, &instruction->base, op1->value.type);
1358313678 }
13584 if (casted_op2->value.special == ConstValSpecialStatic && op1->value.special == ConstValSpecialStatic &&
13679 if (instr_is_comptime(casted_op2)) {
13680 op2_val = ir_resolve_const(ira, casted_op2, UndefOk);
13681 if (op2_val == nullptr)
13682 return ira->codegen->invalid_instruction;
13683 if (op2_val->special == ConstValSpecialUndef)
13684 return ir_const_undef(ira, &instruction->base, op1->value.type);
13685 }
13686
13687 if (op2_val != nullptr && op1_val != nullptr &&
1358513688 (op1->value.data.x_ptr.special == ConstPtrSpecialHardCodedAddr ||
1358613689 op1->value.data.x_ptr.special == ConstPtrSpecialNull))
1358713690 {
13588 uint64_t start_addr = (op1->value.data.x_ptr.special == ConstPtrSpecialNull) ?
13589 0 : op1->value.data.x_ptr.data.hard_coded_addr.addr;
13691 uint64_t start_addr = (op1_val->data.x_ptr.special == ConstPtrSpecialNull) ?
13692 0 : op1_val->data.x_ptr.data.hard_coded_addr.addr;
1359013693 uint64_t elem_offset;
1359113694 if (!ir_resolve_usize(ira, casted_op2, &elem_offset))
1359213695 return ira->codegen->invalid_instruction;
13593 ZigType *elem_type = op1->value.type->data.pointer.child_type;
13696 ZigType *elem_type = op1_val->type->data.pointer.child_type;
1359413697 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown)))
1359513698 return ira->codegen->invalid_instruction;
1359613699 uint64_t byte_offset = type_size(ira->codegen, elem_type) * elem_offset;
......@@ -13602,7 +13705,7 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1360213705 } else {
1360313706 zig_unreachable();
1360413707 }
13605 IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type);
13708 IrInstruction *result = ir_const(ira, &instruction->base, op1_val->type);
1360613709 result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
1360713710 result->value.data.x_ptr.mut = ConstPtrMutRuntimeVar;
1360813711 result->value.data.x_ptr.data.hard_coded_addr.addr = new_addr;
......@@ -14171,9 +14274,13 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1417114274 }
1417214275 break;
1417314276 case ReqCompTimeNo:
14174 if (init_val != nullptr) {
14175 if (init_val->special == ConstValSpecialStatic &&
14176 init_val->type->id == ZigTypeIdFn &&
14277 if (init_val != nullptr && value_is_comptime(init_val)) {
14278 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
14279 decl_var_instruction->base.source_node, init_val, UndefOk)))
14280 {
14281 result_type = ira->codegen->builtin_types.entry_invalid;
14282 } else if (init_val->type->id == ZigTypeIdFn &&
14283 init_val->special != ConstValSpecialUndef &&
1417714284 init_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
1417814285 init_val->data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
1417914286 {
......@@ -14231,7 +14338,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1423114338 }
1423214339 }
1423314340
14234 if (init_val != nullptr && init_val->special != ConstValSpecialRuntime) {
14341 if (init_val != nullptr && value_is_comptime(init_val)) {
1423514342 // Resolve ConstPtrMutInfer
1423614343 if (var->gen_is_const) {
1423714344 var_ptr->value.data.x_ptr.mut = ConstPtrMutComptimeConst;
......@@ -14242,6 +14349,10 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1424214349 // since it's a comptime val there are no instructions for it.
1424314350 // we memcpy the init value here
1424414351 IrInstruction *deref = ir_get_deref(ira, var_ptr, var_ptr, nullptr);
14352 if (type_is_invalid(deref->value.type)) {
14353 var->var_type = ira->codegen->builtin_types.entry_invalid;
14354 return ira->codegen->invalid_instruction;
14355 }
1424514356 // If this assertion trips, something is wrong with the IR instructions, because
1424614357 // we expected the above deref to return a constant value, but it created a runtime
1424714358 // instruction.
......@@ -14250,7 +14361,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1425014361 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref, false);
1425114362 }
1425214363
14253 if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) {
14364 if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {
1425414365 assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length);
1425514366 ConstExprValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index);
1425614367 copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const);
......@@ -14555,6 +14666,10 @@ static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_in
1455514666
1455614667 if ((err = type_resolve(ira->codegen, var_type, ResolveStatusZeroBitsKnown)))
1455714668 return ira->codegen->invalid_instruction;
14669 if (align != 0) {
14670 if ((err = type_resolve(ira->codegen, var_type, ResolveStatusAlignmentKnown)))
14671 return ira->codegen->invalid_instruction;
14672 }
1455814673 assert(result->base.value.data.x_ptr.special != ConstPtrSpecialInvalid);
1455914674
1456014675 pointee->type = var_type;
......@@ -15174,23 +15289,25 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1517415289
1517515290 bool comptime_var_mem = ir_get_var_is_comptime(var);
1517615291 bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern;
15177 bool is_const = var->src_is_const;
1517815292 bool is_volatile = false;
1517915293
15294 IrInstruction *result = ir_build_var_ptr(&ira->new_irb,
15295 instruction->scope, instruction->source_node, var);
15296 result->value.type = get_pointer_to_type_extra(ira->codegen, var->var_type,
15297 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false);
15298
1518015299 if (linkage_makes_it_runtime)
1518115300 goto no_mem_slot;
1518215301
15183 if (var->const_value->special == ConstValSpecialStatic) {
15302 if (value_is_comptime(var->const_value)) {
1518415303 mem_slot = var->const_value;
15185 } else {
15186 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
15187 // find the relevant exec_context
15188 assert(var->owner_exec != nullptr);
15189 assert(var->owner_exec->analysis != nullptr);
15190 IrExecContext *exec_context = &var->owner_exec->analysis->exec_context;
15191 assert(var->mem_slot_index < exec_context->mem_slot_list.length);
15192 mem_slot = exec_context->mem_slot_list.at(var->mem_slot_index);
15193 }
15304 } else if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
15305 // find the relevant exec_context
15306 assert(var->owner_exec != nullptr);
15307 assert(var->owner_exec->analysis != nullptr);
15308 IrExecContext *exec_context = &var->owner_exec->analysis->exec_context;
15309 assert(var->mem_slot_index < exec_context->mem_slot_list.length);
15310 mem_slot = exec_context->mem_slot_list.at(var->mem_slot_index);
1519415311 }
1519515312
1519615313 if (mem_slot != nullptr) {
......@@ -15198,6 +15315,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1519815315 case ConstValSpecialRuntime:
1519915316 goto no_mem_slot;
1520015317 case ConstValSpecialStatic: // fallthrough
15318 case ConstValSpecialLazy: // fallthrough
1520115319 case ConstValSpecialUndef: {
1520215320 ConstPtrMut ptr_mut;
1520315321 if (comptime_var_mem) {
......@@ -15208,8 +15326,11 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1520815326 assert(!comptime_var_mem);
1520915327 ptr_mut = ConstPtrMutRuntimeVar;
1521015328 }
15211 return ir_get_const_ptr(ira, instruction, mem_slot, var->var_type,
15212 ptr_mut, is_const, is_volatile, var->align_bytes);
15329 result->value.special = ConstValSpecialStatic;
15330 result->value.data.x_ptr.mut = ptr_mut;
15331 result->value.data.x_ptr.special = ConstPtrSpecialRef;
15332 result->value.data.x_ptr.data.ref.pointee = mem_slot;
15333 return result;
1521315334 }
1521415335 }
1521515336 zig_unreachable();
......@@ -15217,15 +15338,10 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1521715338
1521815339no_mem_slot:
1521915340
15220 IrInstruction *var_ptr_instruction = ir_build_var_ptr(&ira->new_irb,
15221 instruction->scope, instruction->source_node, var);
15222 var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->var_type,
15223 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false);
15224
1522515341 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
15226 var_ptr_instruction->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;
15342 result->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;
1522715343
15228 return var_ptr_instruction;
15344 return result;
1522915345}
1523015346
1523115347// This function is called when a comptime value becomes accessible at runtime.
......@@ -15489,7 +15605,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1548915605 AstNode *body_node = fn_entry->body_node;
1549015606 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
1549115607 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
15492 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node);
15608 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node,
15609 UndefOk);
1549315610
1549415611 if (inferred_err_set_type != nullptr) {
1549515612 inferred_err_set_type->data.error_set.infer_fn = nullptr;
......@@ -15513,8 +15630,9 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1551315630 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);
1551415631 }
1551515632
15516 if (type_is_invalid(result->type))
15633 if (type_is_invalid(result->type)) {
1551715634 return ira->codegen->invalid_instruction;
15635 }
1551815636 }
1551915637
1552015638 IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);
......@@ -15685,7 +15803,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1568515803 ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
1568615804 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
1568715805 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
15688 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
15806 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec,
15807 nullptr, UndefBad);
1568915808 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1569015809 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
1569115810 copy_const_val(&const_instruction->base.value, align_result, true);
......@@ -16066,51 +16185,20 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1606616185 zig_unreachable();
1606716186}
1606816187
16069static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
16070 Error err;
16071 IrInstruction *value = un_op_instruction->value->child;
16072 ZigType *type_entry = ir_resolve_type(ira, value);
16073 if (type_is_invalid(type_entry))
16074 return ira->codegen->invalid_instruction;
16075 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
16076 return ira->codegen->invalid_instruction;
16188static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp *instruction) {
16189 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
16190 result->value.special = ConstValSpecialLazy;
1607716191
16078 switch (type_entry->id) {
16079 case ZigTypeIdInvalid:
16080 zig_unreachable();
16081 case ZigTypeIdMetaType:
16082 case ZigTypeIdVoid:
16083 case ZigTypeIdBool:
16084 case ZigTypeIdInt:
16085 case ZigTypeIdVector:
16086 case ZigTypeIdFloat:
16087 case ZigTypeIdPointer:
16088 case ZigTypeIdArray:
16089 case ZigTypeIdStruct:
16090 case ZigTypeIdComptimeFloat:
16091 case ZigTypeIdComptimeInt:
16092 case ZigTypeIdEnumLiteral:
16093 case ZigTypeIdUndefined:
16094 case ZigTypeIdNull:
16095 case ZigTypeIdOptional:
16096 case ZigTypeIdErrorUnion:
16097 case ZigTypeIdErrorSet:
16098 case ZigTypeIdEnum:
16099 case ZigTypeIdUnion:
16100 case ZigTypeIdFn:
16101 case ZigTypeIdBoundFn:
16102 case ZigTypeIdArgTuple:
16103 case ZigTypeIdFnFrame:
16104 case ZigTypeIdAnyFrame:
16105 return ir_const_type(ira, &un_op_instruction->base, get_optional_type(ira->codegen, type_entry));
16192 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);
16193 lazy_opt_type->ira = ira;
16194 result->value.data.x_lazy = &lazy_opt_type->base;
16195 lazy_opt_type->base.id = LazyValueIdOptType;
1610616196
16107 case ZigTypeIdUnreachable:
16108 case ZigTypeIdOpaque:
16109 ir_add_error_node(ira, un_op_instruction->base.source_node,
16110 buf_sprintf("type '%s' not optional", buf_ptr(&type_entry->name)));
16111 return ira->codegen->invalid_instruction;
16112 }
16113 zig_unreachable();
16197 lazy_opt_type->payload_type = instruction->value->child;
16198 if (ir_resolve_type_lazy(ira, lazy_opt_type->payload_type) == nullptr)
16199 return ira->codegen->invalid_instruction;
16200
16201 return result;
1611416202}
1611516203
1611616204static ErrorMsg *ir_eval_negation_scalar(IrAnalyze *ira, IrInstruction *source_instr, ZigType *scalar_type,
......@@ -16727,7 +16815,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1672716815 return ira->codegen->invalid_instruction;
1672816816
1672916817 bool safety_check_on = elem_ptr_instruction->safety_check_on;
16730 if ((err = ensure_complete_type(ira->codegen, return_type->data.pointer.child_type)))
16818 if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown)))
1673116819 return ira->codegen->invalid_instruction;
1673216820
1673316821 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
......@@ -17005,7 +17093,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1700517093 auto entry = container_scope->decl_table.maybe_get(field_name);
1700617094 Tld *tld = entry ? entry->value : nullptr;
1700717095 if (tld && tld->id == TldIdFn) {
17008 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node);
17096 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);
1700917097 if (tld->resolution == TldResolutionInvalid)
1701017098 return ira->codegen->invalid_instruction;
1701117099 TldFn *tld_fn = (TldFn *)tld;
......@@ -17038,6 +17126,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1703817126static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction *source_instr,
1703917127 TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing)
1704017128{
17129 Error err;
1704117130 switch (type_has_one_possible_value(ira->codegen, field->type_entry)) {
1704217131 case OnePossibleValueInvalid:
1704317132 return ira->codegen->invalid_instruction;
......@@ -17048,9 +17137,9 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
1704817137 case OnePossibleValueNo:
1704917138 break;
1705017139 }
17140 if ((err = type_resolve(ira->codegen, struct_type, ResolveStatusAlignmentKnown)))
17141 return ira->codegen->invalid_instruction;
1705117142 assert(struct_ptr->value.type->id == ZigTypeIdPointer);
17052 bool is_packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
17053 uint32_t align_bytes = is_packed ? 1 : get_abi_alignment(ira->codegen, field->type_entry);
1705417143 uint32_t ptr_bit_offset = struct_ptr->value.type->data.pointer.bit_offset_in_host;
1705517144 uint32_t ptr_host_int_bytes = struct_ptr->value.type->data.pointer.host_int_bytes;
1705617145 uint32_t host_int_bytes_for_result_type = (ptr_host_int_bytes == 0) ?
......@@ -17058,7 +17147,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
1705817147 bool is_const = struct_ptr->value.type->data.pointer.is_const;
1705917148 bool is_volatile = struct_ptr->value.type->data.pointer.is_volatile;
1706017149 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
17061 is_const, is_volatile, PtrLenSingle, align_bytes,
17150 is_const, is_volatile, PtrLenSingle, field->align,
1706217151 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
1706317152 (uint32_t)host_int_bytes_for_result_type, false);
1706417153 if (instr_is_comptime(struct_ptr)) {
......@@ -17113,7 +17202,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1711317202 Error err;
1711417203
1711517204 ZigType *bare_type = container_ref_type(container_type);
17116 if ((err = ensure_complete_type(ira->codegen, bare_type)))
17205 if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusSizeKnown)))
1711717206 return ira->codegen->invalid_instruction;
1711817207
1711917208 assert(container_ptr->value.type->id == ZigTypeIdPointer);
......@@ -17243,23 +17332,22 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1724317332}
1724417333
1724517334static 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);
17335 ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
1724817336 return ira->codegen->invalid_instruction;
1724917337}
1725017338
1725117339static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
17252 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
17253 if (tld->resolution == TldResolutionInvalid)
17340 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, true);
17341 if (tld->resolution == TldResolutionInvalid) {
1725417342 return ira->codegen->invalid_instruction;
17343 }
1725517344
1725617345 switch (tld->id) {
1725717346 case TldIdContainer:
1725817347 case TldIdCompTime:
1725917348 case TldIdUsingNamespace:
1726017349 zig_unreachable();
17261 case TldIdVar:
17262 {
17350 case TldIdVar: {
1726317351 TldVar *tld_var = (TldVar *)tld;
1726417352 ZigVar *var = tld_var->var;
1726517353 if (var == nullptr) {
......@@ -17271,8 +17359,7 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
1727117359
1727217360 return ir_get_var_ptr(ira, source_instruction, var);
1727317361 }
17274 case TldIdFn:
17275 {
17362 case TldIdFn: {
1727617363 TldFn *tld_fn = (TldFn *)tld;
1727717364 ZigFn *fn_entry = tld_fn->fn_entry;
1727817365 assert(fn_entry->type_entry);
......@@ -17396,7 +17483,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1739617483 return ira->codegen->invalid_instruction;
1739717484 } else if (is_container(child_type)) {
1739817485 if (child_type->id == ZigTypeIdEnum) {
17399 if ((err = ensure_complete_type(ira->codegen, child_type)))
17486 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1740017487 return ira->codegen->invalid_instruction;
1740117488
1740217489 TypeEnumField *field = find_enum_type_field(child_type, field_name);
......@@ -17425,7 +17512,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1742517512 (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
1742617513 child_type->data.unionation.decl_node->data.container_decl.auto_enum))
1742717514 {
17428 if ((err = ensure_complete_type(ira->codegen, child_type)))
17515 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1742917516 return ira->codegen->invalid_instruction;
1743017517 TypeUnionField *field = find_union_type_field(child_type, field_name);
1743117518 if (field) {
......@@ -17844,22 +17931,29 @@ static IrInstruction *ir_analyze_instruction_any_frame_type(IrAnalyze *ira,
1784417931static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1784517932 IrInstructionSliceType *slice_type_instruction)
1784617933{
17847 Error err;
17848 uint32_t align_bytes = 0;
17934 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);
17935 result->value.special = ConstValSpecialLazy;
17936
17937 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
17938 lazy_slice_type->ira = ira;
17939 result->value.data.x_lazy = &lazy_slice_type->base;
17940 lazy_slice_type->base.id = LazyValueIdSliceType;
17941
1784917942 if (slice_type_instruction->align_value != nullptr) {
17850 if (!ir_resolve_align(ira, slice_type_instruction->align_value->child, &align_bytes))
17943 lazy_slice_type->align_inst = slice_type_instruction->align_value->child;
17944 if (ir_resolve_const(ira, lazy_slice_type->align_inst, LazyOk) == nullptr)
1785117945 return ira->codegen->invalid_instruction;
1785217946 }
1785317947
17854 ZigType *child_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
17855 if (type_is_invalid(child_type))
17948 lazy_slice_type->elem_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
17949 if (type_is_invalid(lazy_slice_type->elem_type))
1785617950 return ira->codegen->invalid_instruction;
1785717951
17858 bool is_const = slice_type_instruction->is_const;
17859 bool is_volatile = slice_type_instruction->is_volatile;
17860 bool is_allow_zero = slice_type_instruction->is_allow_zero;
17952 lazy_slice_type->is_const = slice_type_instruction->is_const;
17953 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;
17954 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;
1786117955
17862 switch (child_type->id) {
17956 switch (lazy_slice_type->elem_type->id) {
1786317957 case ZigTypeIdInvalid: // handled above
1786417958 zig_unreachable();
1786517959 case ZigTypeIdUnreachable:
......@@ -17868,7 +17962,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1786817962 case ZigTypeIdArgTuple:
1786917963 case ZigTypeIdOpaque:
1787017964 ir_add_error_node(ira, slice_type_instruction->base.source_node,
17871 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&child_type->name)));
17965 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&lazy_slice_type->elem_type->name)));
1787217966 return ira->codegen->invalid_instruction;
1787317967 case ZigTypeIdMetaType:
1787417968 case ZigTypeIdVoid:
......@@ -17891,18 +17985,9 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1789117985 case ZigTypeIdVector:
1789217986 case ZigTypeIdFnFrame:
1789317987 case ZigTypeIdAnyFrame:
17894 {
17895 ResolveStatus needed_status = (align_bytes == 0) ?
17896 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;
17897 if ((err = type_resolve(ira->codegen, child_type, needed_status)))
17898 return ira->codegen->invalid_instruction;
17899 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
17900 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0, is_allow_zero);
17901 ZigType *result_type = get_slice_type(ira->codegen, slice_ptr_type);
17902 return ir_const_type(ira, &slice_type_instruction->base, result_type);
17903 }
17988 break;
1790417989 }
17905 zig_unreachable();
17990 return result;
1790617991}
1790717992
1790817993static IrInstruction *ir_analyze_instruction_global_asm(IrAnalyze *ira, IrInstructionGlobalAsm *instruction) {
......@@ -18008,7 +18093,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1800818093 case ZigTypeIdFnFrame:
1800918094 case ZigTypeIdAnyFrame:
1801018095 {
18011 if ((err = ensure_complete_type(ira->codegen, child_type)))
18096 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1801218097 return ira->codegen->invalid_instruction;
1801318098 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
1801418099 return ir_const_type(ira, &array_type_instruction->base, result_type);
......@@ -18024,7 +18109,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
1802418109 IrInstruction *type_value = size_of_instruction->type_value->child;
1802518110 ZigType *type_entry = ir_resolve_type(ira, type_value);
1802618111
18027 if ((err = ensure_complete_type(ira->codegen, type_entry)))
18112 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1802818113 return ira->codegen->invalid_instruction;
1802918114
1803018115 switch (type_entry->id) {
......@@ -18529,7 +18614,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1852918614 if (pointee_val->special == ConstValSpecialRuntime)
1853018615 pointee_val = nullptr;
1853118616 }
18532 if ((err = ensure_complete_type(ira->codegen, target_type)))
18617 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusSizeKnown)))
1853318618 return ira->codegen->invalid_instruction;
1853418619
1853518620 switch (target_type->id) {
......@@ -19070,7 +19155,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1907019155 Scope *analyze_scope = &get_container_scope(container_type)->base;
1907119156 // memoize it
1907219157 field->init_val = analyze_const_value(ira->codegen, analyze_scope, init_node,
19073 field->type_entry, nullptr);
19158 field->type_entry, nullptr, UndefOk);
1907419159 }
1907519160 if (type_is_invalid(field->init_val->type))
1907619161 return ira->codegen->invalid_instruction;
......@@ -19276,8 +19361,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
1927619361 if (!msg_buf)
1927719362 return ira->codegen->invalid_instruction;
1927819363
19279 ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf);
19280 emit_error_notes_for_ref_stack(ira->codegen, msg);
19364 ir_add_error(ira, &instruction->base, msg_buf);
1928119365
1928219366 return ira->codegen->invalid_instruction;
1928319367}
......@@ -19319,7 +19403,10 @@ static IrInstruction *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruct
1931919403 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
1932019404 true, false, PtrLenUnknown, 0, 0, 0, false);
1932119405 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
19322 if (casted_value->value.special == ConstValSpecialStatic) {
19406 if (instr_is_comptime(casted_value)) {
19407 ConstExprValue *val = ir_resolve_const(ira, casted_value, UndefBad);
19408 if (val == nullptr)
19409 return ira->codegen->invalid_instruction;
1932319410 ErrorTableEntry *err = casted_value->value.data.x_err_set;
1932419411 if (!err->cached_error_name_val) {
1932519412 ConstExprValue *array_val = create_const_str_lit(ira->codegen, &err->name);
......@@ -19391,7 +19478,7 @@ static IrInstruction *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1939119478 return ira->codegen->invalid_instruction;
1939219479 }
1939319480
19394 if ((err = ensure_complete_type(ira->codegen, container_type)))
19481 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
1939519482 return ira->codegen->invalid_instruction;
1939619483
1939719484 TypeStructField *field = find_struct_type_field(container_type, field_name);
......@@ -19470,7 +19557,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
1947019557 return nullptr;
1947119558
1947219559 Error err;
19473 if ((err = ensure_complete_type(ira->codegen, container_type)))
19560 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
1947419561 return nullptr;
1947519562
1947619563 Buf *field_name = ir_resolve_str(ira, field_name_value);
......@@ -19574,11 +19661,9 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1957419661
1957519662 ZigVar *var = tld->var;
1957619663
19577 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))
19578 return ira->codegen->builtin_types.entry_invalid;
19579
1958019664 assert(var->const_value->type->id == ZigTypeIdMetaType);
19581 return var->const_value->data.x_type;
19665
19666 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, nullptr, var->const_value);
1958219667}
1958319668
1958419669static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr, ConstExprValue *out_val,
......@@ -19594,15 +19679,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1959419679 ensure_field_index(type_info_declaration_type, "data", 2);
1959519680
1959619681 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)))
19682 if ((err = type_resolve(ira->codegen, type_info_declaration_data_type, ResolveStatusSizeKnown)))
1959819683 return err;
1959919684
1960019685 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)))
19686 if ((err = type_resolve(ira->codegen, type_info_fn_decl_type, ResolveStatusSizeKnown)))
1960219687 return err;
1960319688
1960419689 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)))
19690 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))
1960619691 return err;
1960719692
1960819693 // Loop through our declarations once to figure out how many declarations we will generate info for.
......@@ -19613,7 +19698,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1961319698 while ((curr_entry = decl_it.next()) != nullptr) {
1961419699 // If the declaration is unresolved, force it to be resolved again.
1961519700 if (curr_entry->value->resolution == TldResolutionUnresolved) {
19616 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
19701 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);
1961719702 if (curr_entry->value->resolution != TldResolutionOk) {
1961819703 return ErrorSemanticAnalyzeFail;
1961919704 }
......@@ -19673,7 +19758,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1967319758 case TldIdVar:
1967419759 {
1967519760 ZigVar *var = ((TldVar *)curr_entry->value)->var;
19676 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))
19761 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
1967719762 return ErrorSemanticAnalyzeFail;
1967819763
1967919764 if (var->const_value->type->id == ZigTypeIdMetaType) {
......@@ -19799,7 +19884,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
1979919884 case TldIdContainer:
1980019885 {
1980119886 ZigType *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
19802 if ((err = ensure_complete_type(ira->codegen, type_entry)))
19887 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1980319888 return ErrorSemanticAnalyzeFail;
1980419889
1980519890 // This is a type.
......@@ -19855,7 +19940,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1985519940 return nullptr;
1985619941
1985719942 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
19858 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_type));
19943 assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown));
1985919944
1986019945 ConstExprValue *result = create_const_vals(1);
1986119946 result->special = ConstValSpecialStatic;
......@@ -19867,7 +19952,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1986719952 // size: Size
1986819953 ensure_field_index(result->type, "size", 0);
1986919954 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));
19955 assertNoError(type_resolve(ira->codegen, type_info_pointer_size_type, ResolveStatusSizeKnown));
1987119956 fields[0].special = ConstValSpecialStatic;
1987219957 fields[0].type = type_info_pointer_size_type;
1987319958 bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);
......@@ -20581,7 +20666,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
2058120666 ZigType *void_type = ira->codegen->builtin_types.entry_void;
2058220667 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
2058320668 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
20584 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr);
20669 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, UndefBad);
2058520670 if (type_is_invalid(cimport_result->type))
2058620671 return ira->codegen->invalid_instruction;
2058720672
......@@ -21482,64 +21567,75 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
2148221567 return ira->codegen->invalid_instruction;
2148321568
2148421569 // TODO test this at comptime with u8 and non-u8 types
21485 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
21486 casted_byte->value.special == ConstValSpecialStatic &&
21487 casted_count->value.special == ConstValSpecialStatic &&
21488 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
21489 casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
21570 if (instr_is_comptime(casted_dest_ptr) &&
21571 instr_is_comptime(casted_byte) &&
21572 instr_is_comptime(casted_count))
2149021573 {
21491 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
21574 ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad);
21575 if (dest_ptr_val == nullptr)
21576 return ira->codegen->invalid_instruction;
2149221577
21493 ConstExprValue *dest_elements;
21494 size_t start;
21495 size_t bound_end;
21496 switch (dest_ptr_val->data.x_ptr.special) {
21497 case ConstPtrSpecialInvalid:
21498 case ConstPtrSpecialDiscard:
21499 zig_unreachable();
21500 case ConstPtrSpecialRef:
21501 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21502 start = 0;
21503 bound_end = 1;
21504 break;
21505 case ConstPtrSpecialBaseArray:
21506 {
21507 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21508 expand_undef_array(ira->codegen, array_val);
21509 dest_elements = array_val->data.x_array.data.s_none.elements;
21510 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21511 bound_end = array_val->type->data.array.len;
21512 break;
21513 }
21514 case ConstPtrSpecialBaseStruct:
21515 zig_panic("TODO memset on const inner struct");
21516 case ConstPtrSpecialBaseErrorUnionCode:
21517 zig_panic("TODO memset on const inner error union code");
21518 case ConstPtrSpecialBaseErrorUnionPayload:
21519 zig_panic("TODO memset on const inner error union payload");
21520 case ConstPtrSpecialBaseOptionalPayload:
21521 zig_panic("TODO memset on const inner optional payload");
21522 case ConstPtrSpecialHardCodedAddr:
21523 zig_unreachable();
21524 case ConstPtrSpecialFunction:
21525 zig_panic("TODO memset on ptr cast from function");
21526 case ConstPtrSpecialNull:
21527 zig_panic("TODO memset on null ptr");
21528 }
21578 ConstExprValue *byte_val = ir_resolve_const(ira, casted_byte, UndefOk);
21579 if (byte_val == nullptr)
21580 return ira->codegen->invalid_instruction;
2152921581
21530 size_t count = bigint_as_usize(&casted_count->value.data.x_bigint);
21531 size_t end = start + count;
21532 if (end > bound_end) {
21533 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
21582 ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad);
21583 if (count_val == nullptr)
2153421584 return ira->codegen->invalid_instruction;
21535 }
2153621585
21537 ConstExprValue *byte_val = &casted_byte->value;
21538 for (size_t i = start; i < end; i += 1) {
21539 copy_const_val(&dest_elements[i], byte_val, true);
21540 }
21586 if (casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
21587 casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
21588 {
21589 ConstExprValue *dest_elements;
21590 size_t start;
21591 size_t bound_end;
21592 switch (dest_ptr_val->data.x_ptr.special) {
21593 case ConstPtrSpecialInvalid:
21594 case ConstPtrSpecialDiscard:
21595 zig_unreachable();
21596 case ConstPtrSpecialRef:
21597 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21598 start = 0;
21599 bound_end = 1;
21600 break;
21601 case ConstPtrSpecialBaseArray:
21602 {
21603 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21604 expand_undef_array(ira->codegen, array_val);
21605 dest_elements = array_val->data.x_array.data.s_none.elements;
21606 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21607 bound_end = array_val->type->data.array.len;
21608 break;
21609 }
21610 case ConstPtrSpecialBaseStruct:
21611 zig_panic("TODO memset on const inner struct");
21612 case ConstPtrSpecialBaseErrorUnionCode:
21613 zig_panic("TODO memset on const inner error union code");
21614 case ConstPtrSpecialBaseErrorUnionPayload:
21615 zig_panic("TODO memset on const inner error union payload");
21616 case ConstPtrSpecialBaseOptionalPayload:
21617 zig_panic("TODO memset on const inner optional payload");
21618 case ConstPtrSpecialHardCodedAddr:
21619 zig_unreachable();
21620 case ConstPtrSpecialFunction:
21621 zig_panic("TODO memset on ptr cast from function");
21622 case ConstPtrSpecialNull:
21623 zig_panic("TODO memset on null ptr");
21624 }
2154121625
21542 return ir_const_void(ira, &instruction->base);
21626 size_t count = bigint_as_usize(&count_val->data.x_bigint);
21627 size_t end = start + count;
21628 if (end > bound_end) {
21629 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
21630 return ira->codegen->invalid_instruction;
21631 }
21632
21633 for (size_t i = start; i < end; i += 1) {
21634 copy_const_val(&dest_elements[i], byte_val, true);
21635 }
21636
21637 return ir_const_void(ira, &instruction->base);
21638 }
2154321639 }
2154421640
2154521641 IrInstruction *result = ir_build_memset(&ira->new_irb, instruction->base.scope, instruction->base.source_node,
......@@ -21607,107 +21703,118 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2160721703
2160821704 // TODO test this at comptime with u8 and non-u8 types
2160921705 // TODO test with dest ptr being a global runtime variable
21610 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
21611 casted_src_ptr->value.special == ConstValSpecialStatic &&
21612 casted_count->value.special == ConstValSpecialStatic &&
21613 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
21706 if (instr_is_comptime(casted_dest_ptr) &&
21707 instr_is_comptime(casted_src_ptr) &&
21708 instr_is_comptime(casted_count))
2161421709 {
21615 size_t count = bigint_as_usize(&casted_count->value.data.x_bigint);
21710 ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad);
21711 if (dest_ptr_val == nullptr)
21712 return ira->codegen->invalid_instruction;
2161621713
21617 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
21618 ConstExprValue *dest_elements;
21619 size_t dest_start;
21620 size_t dest_end;
21621 switch (dest_ptr_val->data.x_ptr.special) {
21622 case ConstPtrSpecialInvalid:
21623 case ConstPtrSpecialDiscard:
21624 zig_unreachable();
21625 case ConstPtrSpecialRef:
21626 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21627 dest_start = 0;
21628 dest_end = 1;
21629 break;
21630 case ConstPtrSpecialBaseArray:
21631 {
21632 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21633 expand_undef_array(ira->codegen, array_val);
21634 dest_elements = array_val->data.x_array.data.s_none.elements;
21635 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21636 dest_end = array_val->type->data.array.len;
21637 break;
21638 }
21639 case ConstPtrSpecialBaseStruct:
21640 zig_panic("TODO memcpy on const inner struct");
21641 case ConstPtrSpecialBaseErrorUnionCode:
21642 zig_panic("TODO memcpy on const inner error union code");
21643 case ConstPtrSpecialBaseErrorUnionPayload:
21644 zig_panic("TODO memcpy on const inner error union payload");
21645 case ConstPtrSpecialBaseOptionalPayload:
21646 zig_panic("TODO memcpy on const inner optional payload");
21647 case ConstPtrSpecialHardCodedAddr:
21648 zig_unreachable();
21649 case ConstPtrSpecialFunction:
21650 zig_panic("TODO memcpy on ptr cast from function");
21651 case ConstPtrSpecialNull:
21652 zig_panic("TODO memcpy on null ptr");
21653 }
21714 ConstExprValue *src_ptr_val = ir_resolve_const(ira, casted_src_ptr, UndefBad);
21715 if (src_ptr_val == nullptr)
21716 return ira->codegen->invalid_instruction;
2165421717
21655 if (dest_start + count > dest_end) {
21656 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21718 ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad);
21719 if (count_val == nullptr)
2165721720 return ira->codegen->invalid_instruction;
21658 }
2165921721
21660 ConstExprValue *src_ptr_val = &casted_src_ptr->value;
21661 ConstExprValue *src_elements;
21662 size_t src_start;
21663 size_t src_end;
21722 if (dest_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
21723 size_t count = bigint_as_usize(&count_val->data.x_bigint);
2166421724
21665 switch (src_ptr_val->data.x_ptr.special) {
21666 case ConstPtrSpecialInvalid:
21667 case ConstPtrSpecialDiscard:
21668 zig_unreachable();
21669 case ConstPtrSpecialRef:
21670 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
21671 src_start = 0;
21672 src_end = 1;
21673 break;
21674 case ConstPtrSpecialBaseArray:
21675 {
21676 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
21677 expand_undef_array(ira->codegen, array_val);
21678 src_elements = array_val->data.x_array.data.s_none.elements;
21679 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
21680 src_end = array_val->type->data.array.len;
21725 ConstExprValue *dest_elements;
21726 size_t dest_start;
21727 size_t dest_end;
21728 switch (dest_ptr_val->data.x_ptr.special) {
21729 case ConstPtrSpecialInvalid:
21730 case ConstPtrSpecialDiscard:
21731 zig_unreachable();
21732 case ConstPtrSpecialRef:
21733 dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee;
21734 dest_start = 0;
21735 dest_end = 1;
2168121736 break;
21682 }
21683 case ConstPtrSpecialBaseStruct:
21684 zig_panic("TODO memcpy on const inner struct");
21685 case ConstPtrSpecialBaseErrorUnionCode:
21686 zig_panic("TODO memcpy on const inner error union code");
21687 case ConstPtrSpecialBaseErrorUnionPayload:
21688 zig_panic("TODO memcpy on const inner error union payload");
21689 case ConstPtrSpecialBaseOptionalPayload:
21690 zig_panic("TODO memcpy on const inner optional payload");
21691 case ConstPtrSpecialHardCodedAddr:
21692 zig_unreachable();
21693 case ConstPtrSpecialFunction:
21694 zig_panic("TODO memcpy on ptr cast from function");
21695 case ConstPtrSpecialNull:
21696 zig_panic("TODO memcpy on null ptr");
21697 }
21737 case ConstPtrSpecialBaseArray:
21738 {
21739 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
21740 expand_undef_array(ira->codegen, array_val);
21741 dest_elements = array_val->data.x_array.data.s_none.elements;
21742 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
21743 dest_end = array_val->type->data.array.len;
21744 break;
21745 }
21746 case ConstPtrSpecialBaseStruct:
21747 zig_panic("TODO memcpy on const inner struct");
21748 case ConstPtrSpecialBaseErrorUnionCode:
21749 zig_panic("TODO memcpy on const inner error union code");
21750 case ConstPtrSpecialBaseErrorUnionPayload:
21751 zig_panic("TODO memcpy on const inner error union payload");
21752 case ConstPtrSpecialBaseOptionalPayload:
21753 zig_panic("TODO memcpy on const inner optional payload");
21754 case ConstPtrSpecialHardCodedAddr:
21755 zig_unreachable();
21756 case ConstPtrSpecialFunction:
21757 zig_panic("TODO memcpy on ptr cast from function");
21758 case ConstPtrSpecialNull:
21759 zig_panic("TODO memcpy on null ptr");
21760 }
2169821761
21699 if (src_start + count > src_end) {
21700 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21701 return ira->codegen->invalid_instruction;
21702 }
21762 if (dest_start + count > dest_end) {
21763 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21764 return ira->codegen->invalid_instruction;
21765 }
2170321766
21704 // TODO check for noalias violations - this should be generalized to work for any function
21767 ConstExprValue *src_elements;
21768 size_t src_start;
21769 size_t src_end;
2170521770
21706 for (size_t i = 0; i < count; i += 1) {
21707 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
21708 }
21771 switch (src_ptr_val->data.x_ptr.special) {
21772 case ConstPtrSpecialInvalid:
21773 case ConstPtrSpecialDiscard:
21774 zig_unreachable();
21775 case ConstPtrSpecialRef:
21776 src_elements = src_ptr_val->data.x_ptr.data.ref.pointee;
21777 src_start = 0;
21778 src_end = 1;
21779 break;
21780 case ConstPtrSpecialBaseArray:
21781 {
21782 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
21783 expand_undef_array(ira->codegen, array_val);
21784 src_elements = array_val->data.x_array.data.s_none.elements;
21785 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
21786 src_end = array_val->type->data.array.len;
21787 break;
21788 }
21789 case ConstPtrSpecialBaseStruct:
21790 zig_panic("TODO memcpy on const inner struct");
21791 case ConstPtrSpecialBaseErrorUnionCode:
21792 zig_panic("TODO memcpy on const inner error union code");
21793 case ConstPtrSpecialBaseErrorUnionPayload:
21794 zig_panic("TODO memcpy on const inner error union payload");
21795 case ConstPtrSpecialBaseOptionalPayload:
21796 zig_panic("TODO memcpy on const inner optional payload");
21797 case ConstPtrSpecialHardCodedAddr:
21798 zig_unreachable();
21799 case ConstPtrSpecialFunction:
21800 zig_panic("TODO memcpy on ptr cast from function");
21801 case ConstPtrSpecialNull:
21802 zig_panic("TODO memcpy on null ptr");
21803 }
2170921804
21710 return ir_const_void(ira, &instruction->base);
21805 if (src_start + count > src_end) {
21806 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access"));
21807 return ira->codegen->invalid_instruction;
21808 }
21809
21810 // TODO check for noalias violations - this should be generalized to work for any function
21811
21812 for (size_t i = 0; i < count; i += 1) {
21813 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
21814 }
21815
21816 return ir_const_void(ira, &instruction->base);
21817 }
2171121818 }
2171221819
2171321820 IrInstruction *result = ir_build_memcpy(&ira->new_irb, instruction->base.scope, instruction->base.source_node,
......@@ -21877,6 +21984,11 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2187721984 if (slice_ptr == nullptr)
2187821985 return ira->codegen->invalid_instruction;
2187921986
21987 if (slice_ptr->special == ConstValSpecialUndef) {
21988 ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined"));
21989 return ira->codegen->invalid_instruction;
21990 }
21991
2188021992 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];
2188121993 if (parent_ptr->special == ConstValSpecialUndef) {
2188221994 ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined"));
......@@ -22021,7 +22133,7 @@ static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInst
2202122133 return ira->codegen->invalid_instruction;
2202222134 ZigType *container_type = ir_resolve_type(ira, container);
2202322135
22024 if ((err = ensure_complete_type(ira->codegen, container_type)))
22136 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2202522137 return ira->codegen->invalid_instruction;
2202622138
2202722139 uint64_t result;
......@@ -22057,7 +22169,7 @@ static IrInstruction *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstr
2205722169 if (type_is_invalid(container_type))
2205822170 return ira->codegen->invalid_instruction;
2205922171
22060 if ((err = ensure_complete_type(ira->codegen, container_type)))
22172 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2206122173 return ira->codegen->invalid_instruction;
2206222174
2206322175
......@@ -22100,7 +22212,7 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr
2210022212 if (type_is_invalid(container_type))
2210122213 return ira->codegen->invalid_instruction;
2210222214
22103 if ((err = ensure_complete_type(ira->codegen, container_type)))
22215 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
2210422216 return ira->codegen->invalid_instruction;
2210522217
2210622218 uint64_t member_index;
......@@ -22249,53 +22361,24 @@ static IrInstruction *ir_analyze_instruction_frame_size(IrAnalyze *ira, IrInstru
2224922361}
2225022362
2225122363static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
22252 Error err;
22253 IrInstruction *type_value = instruction->type_value->child;
22254 if (type_is_invalid(type_value->value.type))
22255 return ira->codegen->invalid_instruction;
22256 ZigType *type_entry = ir_resolve_type(ira, type_value);
22364 // Here we create a lazy value in order to avoid resolving the alignment of the type
22365 // immediately. This avoids false positive dependency loops such as:
22366 // const Node = struct {
22367 // field: []align(@alignOf(Node)) Node,
22368 // };
22369 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
22370 result->value.special = ConstValSpecialLazy;
22371
22372 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
22373 lazy_align_of->ira = ira;
22374 result->value.data.x_lazy = &lazy_align_of->base;
22375 lazy_align_of->base.id = LazyValueIdAlignOf;
2225722376
22258 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusAlignmentKnown)))
22377 lazy_align_of->target_type = instruction->type_value->child;
22378 if (ir_resolve_type_lazy(ira, lazy_align_of->target_type) == nullptr)
2225922379 return ira->codegen->invalid_instruction;
2226022380
22261 switch (type_entry->id) {
22262 case ZigTypeIdInvalid:
22263 zig_unreachable();
22264 case ZigTypeIdMetaType:
22265 case ZigTypeIdUnreachable:
22266 case ZigTypeIdComptimeFloat:
22267 case ZigTypeIdComptimeInt:
22268 case ZigTypeIdEnumLiteral:
22269 case ZigTypeIdUndefined:
22270 case ZigTypeIdNull:
22271 case ZigTypeIdBoundFn:
22272 case ZigTypeIdArgTuple:
22273 case ZigTypeIdVoid:
22274 case ZigTypeIdOpaque:
22275 ir_add_error(ira, instruction->type_value,
22276 buf_sprintf("no align available for type '%s'", buf_ptr(&type_entry->name)));
22277 return ira->codegen->invalid_instruction;
22278 case ZigTypeIdBool:
22279 case ZigTypeIdInt:
22280 case ZigTypeIdFloat:
22281 case ZigTypeIdPointer:
22282 case ZigTypeIdArray:
22283 case ZigTypeIdStruct:
22284 case ZigTypeIdOptional:
22285 case ZigTypeIdErrorUnion:
22286 case ZigTypeIdErrorSet:
22287 case ZigTypeIdEnum:
22288 case ZigTypeIdUnion:
22289 case ZigTypeIdFn:
22290 case ZigTypeIdVector:
22291 case ZigTypeIdFnFrame:
22292 case ZigTypeIdAnyFrame:
22293 {
22294 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
22295 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
22296 }
22297 }
22298 zig_unreachable();
22381 return result;
2229922382}
2230022383
2230122384static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
......@@ -22359,13 +22442,26 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr
2235922442 if (type_is_invalid(casted_result_ptr->value.type))
2236022443 return ira->codegen->invalid_instruction;
2236122444
22362 if (casted_op1->value.special == ConstValSpecialStatic &&
22363 casted_op2->value.special == ConstValSpecialStatic &&
22364 casted_result_ptr->value.special == ConstValSpecialStatic)
22445 if (instr_is_comptime(casted_op1) &&
22446 instr_is_comptime(casted_op2) &&
22447 instr_is_comptime(casted_result_ptr))
2236522448 {
22366 BigInt *op1_bigint = &casted_op1->value.data.x_bigint;
22367 BigInt *op2_bigint = &casted_op2->value.data.x_bigint;
22368 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, &casted_result_ptr->value, casted_result_ptr->source_node);
22449 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad);
22450 if (op1_val == nullptr)
22451 return ira->codegen->invalid_instruction;
22452
22453 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad);
22454 if (op2_val == nullptr)
22455 return ira->codegen->invalid_instruction;
22456
22457 ConstExprValue *result_val = ir_resolve_const(ira, casted_result_ptr, UndefBad);
22458 if (result_val == nullptr)
22459 return ira->codegen->invalid_instruction;
22460
22461 BigInt *op1_bigint = &op1_val->data.x_bigint;
22462 BigInt *op2_bigint = &op2_val->data.x_bigint;
22463 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, result_val,
22464 casted_result_ptr->source_node);
2236922465 if (pointee_val == nullptr)
2237022466 return ira->codegen->invalid_instruction;
2237122467 BigInt *dest_bigint = &pointee_val->data.x_bigint;
......@@ -22754,84 +22850,64 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2275422850 AstNode *proto_node = instruction->base.source_node;
2275522851 assert(proto_node->type == NodeTypeFnProto);
2275622852
22853 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
22854 result->value.special = ConstValSpecialLazy;
22855
22856 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);
22857 lazy_fn_type->ira = ira;
22858 result->value.data.x_lazy = &lazy_fn_type->base;
22859 lazy_fn_type->base.id = LazyValueIdFnType;
22860
2275722861 if (proto_node->data.fn_proto.auto_err_set) {
2275822862 ir_add_error(ira, &instruction->base,
2275922863 buf_sprintf("inferring error set of return type valid only for function definitions"));
2276022864 return ira->codegen->invalid_instruction;
2276122865 }
2276222866
22763 FnTypeId fn_type_id = {0};
22764 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
22867 size_t param_count = proto_node->data.fn_proto.params.length;
22868 lazy_fn_type->proto_node = proto_node;
22869 lazy_fn_type->param_types = allocate<IrInstruction *>(param_count);
2276522870
22766 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
22767 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
22871 for (size_t param_index = 0; param_index < param_count; param_index += 1) {
22872 AstNode *param_node = proto_node->data.fn_proto.params.at(param_index);
2276822873 assert(param_node->type == NodeTypeParamDecl);
2276922874
2277022875 bool param_is_var_args = param_node->data.param_decl.is_var_args;
2277122876 if (param_is_var_args) {
22772 if (fn_type_id.cc == CallingConventionC) {
22773 fn_type_id.param_count = fn_type_id.next_param_index;
22774 continue;
22775 } else if (fn_type_id.cc == CallingConventionUnspecified) {
22776 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
22877 if (proto_node->data.fn_proto.cc == CallingConventionC) {
22878 break;
22879 } else if (proto_node->data.fn_proto.cc == CallingConventionUnspecified) {
22880 lazy_fn_type->is_generic = true;
22881 return result;
2277722882 } else {
2277822883 zig_unreachable();
2277922884 }
2278022885 }
22781 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
22782 param_info->is_noalias = param_node->data.param_decl.is_noalias;
2278322886
22784 if (instruction->param_types[fn_type_id.next_param_index] == nullptr) {
22785 param_info->type = nullptr;
22786 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
22787 } else {
22788 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->child;
22789 if (type_is_invalid(param_type_value->value.type))
22790 return ira->codegen->invalid_instruction;
22791 ZigType *param_type = ir_resolve_type(ira, param_type_value);
22792 switch (type_requires_comptime(ira->codegen, param_type)) {
22793 case ReqCompTimeYes:
22794 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
22795 ir_add_error(ira, param_type_value,
22796 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
22797 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
22798 return ira->codegen->invalid_instruction;
22799 }
22800 param_info->type = param_type;
22801 fn_type_id.next_param_index += 1;
22802 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
22803 case ReqCompTimeInvalid:
22804 return ira->codegen->invalid_instruction;
22805 case ReqCompTimeNo:
22806 break;
22807 }
22808 if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) {
22809 ir_add_error(ira, param_type_value,
22810 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
22811 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
22812 return ira->codegen->invalid_instruction;
22813 }
22814 param_info->type = param_type;
22887 if (instruction->param_types[param_index] == nullptr) {
22888 lazy_fn_type->is_generic = true;
22889 return result;
2281522890 }
2281622891
22892 IrInstruction *param_type_value = instruction->param_types[param_index]->child;
22893 if (type_is_invalid(param_type_value->value.type))
22894 return ira->codegen->invalid_instruction;
22895 if (ir_resolve_const(ira, param_type_value, LazyOk) == nullptr)
22896 return ira->codegen->invalid_instruction;
22897 lazy_fn_type->param_types[param_index] = param_type_value;
2281722898 }
2281822899
2281922900 if (instruction->align_value != nullptr) {
22820 if (!ir_resolve_align(ira, instruction->align_value->child, &fn_type_id.alignment))
22901 lazy_fn_type->align_inst = instruction->align_value->child;
22902 if (ir_resolve_const(ira, lazy_fn_type->align_inst, LazyOk) == nullptr)
2282122903 return ira->codegen->invalid_instruction;
2282222904 }
2282322905
22824 IrInstruction *return_type_value = instruction->return_type->child;
22825 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
22826 if (type_is_invalid(fn_type_id.return_type))
22827 return ira->codegen->invalid_instruction;
22828 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
22829 ir_add_error(ira, instruction->return_type,
22830 buf_sprintf("return type cannot be opaque"));
22831 return ira->codegen->invalid_instruction;
22832 }
22906 lazy_fn_type->return_type = instruction->return_type->child;
22907 if (ir_resolve_const(ira, lazy_fn_type->return_type, LazyOk) == nullptr)
22908 return ira->codegen->invalid_instruction;
2283322909
22834 return ir_const_type(ira, &instruction->base, get_fn_type(ira->codegen, &fn_type_id));
22910 return result;
2283522911}
2283622912
2283722913static IrInstruction *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {
......@@ -23218,7 +23294,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
2321823294 if (!val)
2321923295 return ira->codegen->invalid_instruction;
2322023296
23221 if (val->special == ConstValSpecialStatic) {
23297 if (value_is_comptime(val) && val->special != ConstValSpecialUndef) {
2322223298 bool is_addr_zero = val->data.x_ptr.special == ConstPtrSpecialNull ||
2322323299 (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr &&
2322423300 val->data.x_ptr.data.hard_coded_addr.addr == 0);
......@@ -23258,6 +23334,12 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
2325823334
2325923335 IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);
2326023336
23337 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
23338 return ira->codegen->invalid_instruction;
23339
23340 if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
23341 return ira->codegen->invalid_instruction;
23342
2326123343 if (type_has_bits(dest_type) && !type_has_bits(src_type)) {
2326223344 ErrorMsg *msg = ir_add_error(ira, source_instr,
2326323345 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
......@@ -23309,8 +23391,10 @@ static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExp
2330923391}
2331023392
2331123393static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) {
23312 if (val->special == ConstValSpecialUndef)
23394 if (val->special == ConstValSpecialUndef) {
23395 expand_undef_struct(codegen, val);
2331323396 val->special = ConstValSpecialStatic;
23397 }
2331423398 assert(val->special == ConstValSpecialStatic);
2331523399 switch (val->type->id) {
2331623400 case ZigTypeIdInvalid:
......@@ -23746,8 +23830,9 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2374623830 IrInstructionDeclRef *instruction)
2374723831{
2374823832 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);
23749 if (type_is_invalid(ref_instruction->value.type))
23833 if (type_is_invalid(ref_instruction->value.type)) {
2375023834 return ira->codegen->invalid_instruction;
23835 }
2375123836
2375223837 if (instruction->lval == LValPtr) {
2375323838 return ref_instruction;
......@@ -23795,53 +23880,32 @@ static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstru
2379523880}
2379623881
2379723882static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtrType *instruction) {
23798 Error err;
23799 ZigType *child_type = ir_resolve_type(ira, instruction->child_type->child);
23800 if (type_is_invalid(child_type))
23801 return ira->codegen->invalid_instruction;
23883 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
23884 result->value.special = ConstValSpecialLazy;
2380223885
23803 if (child_type->id == ZigTypeIdUnreachable) {
23804 ir_add_error(ira, &instruction->base, buf_sprintf("pointer to noreturn not allowed"));
23805 return ira->codegen->invalid_instruction;
23806 } else if (child_type->id == ZigTypeIdOpaque && instruction->ptr_len == PtrLenUnknown) {
23807 ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque"));
23886 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);
23887 lazy_ptr_type->ira = ira;
23888 result->value.data.x_lazy = &lazy_ptr_type->base;
23889 lazy_ptr_type->base.id = LazyValueIdPtrType;
23890
23891 lazy_ptr_type->elem_type = instruction->child_type->child;
23892 if (ir_resolve_type_lazy(ira, lazy_ptr_type->elem_type) == nullptr)
2380823893 return ira->codegen->invalid_instruction;
23809 } else if (instruction->ptr_len == PtrLenC) {
23810 if (!type_allowed_in_extern(ira->codegen, child_type)) {
23811 ir_add_error(ira, &instruction->base,
23812 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'", buf_ptr(&child_type->name)));
23813 return ira->codegen->invalid_instruction;
23814 } else if (child_type->id == ZigTypeIdOpaque) {
23815 ir_add_error(ira, &instruction->base, buf_sprintf("C pointers cannot point opaque types"));
23816 return ira->codegen->invalid_instruction;
23817 } else if (instruction->is_allow_zero) {
23818 ir_add_error(ira, &instruction->base, buf_sprintf("C pointers always allow address zero"));
23819 return ira->codegen->invalid_instruction;
23820 }
23821 }
2382223894
23823 uint32_t align_bytes;
2382423895 if (instruction->align_value != nullptr) {
23825 if (!ir_resolve_align(ira, instruction->align_value->child, &align_bytes))
23826 return ira->codegen->invalid_instruction;
23827 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown)))
23896 lazy_ptr_type->align_inst = instruction->align_value->child;
23897 if (ir_resolve_const(ira, lazy_ptr_type->align_inst, LazyOk) == nullptr)
2382823898 return ira->codegen->invalid_instruction;
23829 if (!type_has_bits(child_type)) {
23830 align_bytes = 0;
23831 }
23832 } else {
23833 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
23834 return ira->codegen->invalid_instruction;
23835 align_bytes = 0;
2383623899 }
2383723900
23838 bool allow_zero = instruction->is_allow_zero || instruction->ptr_len == PtrLenC;
23901 lazy_ptr_type->ptr_len = instruction->ptr_len;
23902 lazy_ptr_type->is_const = instruction->is_const;
23903 lazy_ptr_type->is_volatile = instruction->is_volatile;
23904 lazy_ptr_type->is_allowzero = instruction->is_allow_zero;
23905 lazy_ptr_type->bit_offset_in_host = instruction->bit_offset_start;
23906 lazy_ptr_type->host_int_bytes = instruction->host_int_bytes;
2383923907
23840 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
23841 instruction->is_const, instruction->is_volatile,
23842 instruction->ptr_len, align_bytes,
23843 instruction->bit_offset_start, instruction->host_int_bytes, allow_zero);
23844 return ir_const_type(ira, &instruction->base, result_type);
23908 return result;
2384523909}
2384623910
2384723911static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstructionAlignCast *instruction) {
......@@ -23954,7 +24018,7 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct
2395424018 return ira->codegen->invalid_instruction;
2395524019
2395624020 if (enum_type->id == ZigTypeIdEnum) {
23957 if ((err = ensure_complete_type(ira->codegen, enum_type)))
24021 if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown)))
2395824022 return ira->codegen->invalid_instruction;
2395924023
2396024024 return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type);
......@@ -25100,7 +25164,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2510025164ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec,
2510125165 ZigType *expected_type, AstNode *expected_type_source_node)
2510225166{
25103 assert(!old_exec->invalid);
25167 assert(old_exec->first_err_trace_msg == nullptr);
2510425168 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2510525169
2510625170 IrAnalyze *ira = allocate<IrAnalyze>(1);
......@@ -25147,6 +25211,18 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2514725211 old_instruction->child = new_instruction;
2514825212
2514925213 if (type_is_invalid(new_instruction->value.type)) {
25214 if (new_exec->first_err_trace_msg != nullptr) {
25215 ira->codegen->trace_err = new_exec->first_err_trace_msg;
25216 } else {
25217 new_exec->first_err_trace_msg = ira->codegen->trace_err;
25218 }
25219 if (new_exec->first_err_trace_msg != nullptr &&
25220 !old_instruction->source_node->already_traced_this_node)
25221 {
25222 old_instruction->source_node->already_traced_this_node = true;
25223 new_exec->first_err_trace_msg = add_error_note(ira->codegen, new_exec->first_err_trace_msg,
25224 old_instruction->source_node, buf_create_from_str("referenced here"));
25225 }
2515025226 return ira->codegen->builtin_types.entry_invalid;
2515125227 }
2515225228
......@@ -25158,7 +25234,15 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2515825234 ira->instruction_index += 1;
2515925235 }
2516025236
25161 if (new_exec->invalid) {
25237 if (new_exec->first_err_trace_msg != nullptr) {
25238 codegen->trace_err = new_exec->first_err_trace_msg;
25239 if (codegen->trace_err != nullptr && new_exec->source_node != nullptr &&
25240 !new_exec->source_node->already_traced_this_node)
25241 {
25242 new_exec->source_node->already_traced_this_node = true;
25243 codegen->trace_err = add_error_note(codegen, codegen->trace_err,
25244 new_exec->source_node, buf_create_from_str("referenced here"));
25245 }
2516225246 return ira->codegen->builtin_types.entry_invalid;
2516325247 } else if (ira->src_implicit_return_type_list.length == 0) {
2516425248 return codegen->builtin_types.entry_unreachable;
......@@ -25354,3 +25438,269 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2535425438 }
2535525439 zig_unreachable();
2535625440}
25441
25442static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, LazyValueFnType *lazy_fn_type) {
25443 Error err;
25444 AstNode *proto_node = lazy_fn_type->proto_node;
25445
25446 FnTypeId fn_type_id = {0};
25447 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
25448
25449 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
25450 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
25451 assert(param_node->type == NodeTypeParamDecl);
25452
25453 bool param_is_var_args = param_node->data.param_decl.is_var_args;
25454 if (param_is_var_args) {
25455 if (fn_type_id.cc == CallingConventionC) {
25456 fn_type_id.param_count = fn_type_id.next_param_index;
25457 continue;
25458 } else if (fn_type_id.cc == CallingConventionUnspecified) {
25459 return get_generic_fn_type(ira->codegen, &fn_type_id);
25460 } else {
25461 zig_unreachable();
25462 }
25463 }
25464 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
25465 param_info->is_noalias = param_node->data.param_decl.is_noalias;
25466
25467 if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) {
25468 param_info->type = nullptr;
25469 return get_generic_fn_type(ira->codegen, &fn_type_id);
25470 } else {
25471 IrInstruction *param_type_inst = lazy_fn_type->param_types[fn_type_id.next_param_index];
25472 ZigType *param_type = ir_resolve_type(ira, param_type_inst);
25473 if (type_is_invalid(param_type))
25474 return nullptr;
25475 switch (type_requires_comptime(ira->codegen, param_type)) {
25476 case ReqCompTimeYes:
25477 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
25478 ir_add_error(ira, param_type_inst,
25479 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
25480 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
25481 return nullptr;
25482 }
25483 param_info->type = param_type;
25484 fn_type_id.next_param_index += 1;
25485 return get_generic_fn_type(ira->codegen, &fn_type_id);
25486 case ReqCompTimeInvalid:
25487 return nullptr;
25488 case ReqCompTimeNo:
25489 break;
25490 }
25491 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
25492 if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown)))
25493 return nullptr;
25494 if (!type_has_bits(param_type)) {
25495 ir_add_error(ira, param_type_inst,
25496 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
25497 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
25498 return nullptr;
25499 }
25500 }
25501 param_info->type = param_type;
25502 }
25503 }
25504
25505 if (lazy_fn_type->align_inst != nullptr) {
25506 if (!ir_resolve_align(ira, lazy_fn_type->align_inst, &fn_type_id.alignment))
25507 return nullptr;
25508 }
25509
25510 fn_type_id.return_type = ir_resolve_type(ira, lazy_fn_type->return_type);
25511 if (type_is_invalid(fn_type_id.return_type))
25512 return nullptr;
25513 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
25514 ir_add_error(ira, lazy_fn_type->return_type, buf_create_from_str("return type cannot be opaque"));
25515 return nullptr;
25516 }
25517
25518 return get_fn_type(ira->codegen, &fn_type_id);
25519}
25520
25521static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
25522 Error err;
25523 if (val->special != ConstValSpecialLazy)
25524 return ErrorNone;
25525 switch (val->data.x_lazy->id) {
25526 case LazyValueIdInvalid:
25527 zig_unreachable();
25528 case LazyValueIdAlignOf: {
25529 LazyValueAlignOf *lazy_align_of = reinterpret_cast<LazyValueAlignOf *>(val->data.x_lazy);
25530 IrAnalyze *ira = lazy_align_of->ira;
25531
25532 if (lazy_align_of->target_type->value.special == ConstValSpecialStatic) {
25533 switch (lazy_align_of->target_type->value.data.x_type->id) {
25534 case ZigTypeIdInvalid:
25535 zig_unreachable();
25536 case ZigTypeIdMetaType:
25537 case ZigTypeIdUnreachable:
25538 case ZigTypeIdComptimeFloat:
25539 case ZigTypeIdComptimeInt:
25540 case ZigTypeIdEnumLiteral:
25541 case ZigTypeIdUndefined:
25542 case ZigTypeIdNull:
25543 case ZigTypeIdBoundFn:
25544 case ZigTypeIdArgTuple:
25545 case ZigTypeIdVoid:
25546 case ZigTypeIdOpaque:
25547 ir_add_error(ira, lazy_align_of->target_type,
25548 buf_sprintf("no align available for type '%s'",
25549 buf_ptr(&lazy_align_of->target_type->value.data.x_type->name)));
25550 return ErrorSemanticAnalyzeFail;
25551 case ZigTypeIdBool:
25552 case ZigTypeIdInt:
25553 case ZigTypeIdFloat:
25554 case ZigTypeIdPointer:
25555 case ZigTypeIdArray:
25556 case ZigTypeIdStruct:
25557 case ZigTypeIdOptional:
25558 case ZigTypeIdErrorUnion:
25559 case ZigTypeIdErrorSet:
25560 case ZigTypeIdEnum:
25561 case ZigTypeIdUnion:
25562 case ZigTypeIdFn:
25563 case ZigTypeIdVector:
25564 case ZigTypeIdFnFrame:
25565 case ZigTypeIdAnyFrame:
25566 break;
25567 }
25568 }
25569
25570 uint32_t align_in_bytes;
25571 if ((err = type_val_resolve_abi_align(ira->codegen, &lazy_align_of->target_type->value,
25572 &align_in_bytes)))
25573 {
25574 return err;
25575 }
25576
25577 val->special = ConstValSpecialStatic;
25578 assert(val->type->id == ZigTypeIdComptimeInt);
25579 bigint_init_unsigned(&val->data.x_bigint, align_in_bytes);
25580 return ErrorNone;
25581 }
25582 case LazyValueIdSliceType: {
25583 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(val->data.x_lazy);
25584 IrAnalyze *ira = lazy_slice_type->ira;
25585
25586 uint32_t align_bytes = 0;
25587 if (lazy_slice_type->align_inst != nullptr) {
25588 if (!ir_resolve_align(ira, lazy_slice_type->align_inst, &align_bytes))
25589 return ErrorSemanticAnalyzeFail;
25590 }
25591 ResolveStatus needed_status = (align_bytes == 0) ?
25592 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;
25593 if ((err = type_resolve(ira->codegen, lazy_slice_type->elem_type, needed_status)))
25594 return err;
25595 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, lazy_slice_type->elem_type,
25596 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,
25597 0, 0, lazy_slice_type->is_allowzero);
25598 val->special = ConstValSpecialStatic;
25599 assert(val->type->id == ZigTypeIdMetaType);
25600 val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type);
25601 return ErrorNone;
25602 }
25603 case LazyValueIdPtrType: {
25604 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(val->data.x_lazy);
25605 IrAnalyze *ira = lazy_ptr_type->ira;
25606
25607 uint32_t align_bytes = 0;
25608 if (lazy_ptr_type->align_inst != nullptr) {
25609 if (!ir_resolve_align(ira, lazy_ptr_type->align_inst, &align_bytes))
25610 return ErrorSemanticAnalyzeFail;
25611 }
25612 ZigType *elem_type = ir_resolve_type(ira, lazy_ptr_type->elem_type);
25613 if (type_is_invalid(elem_type))
25614 return ErrorSemanticAnalyzeFail;
25615
25616 if (elem_type->id == ZigTypeIdUnreachable) {
25617 ir_add_error(ira, lazy_ptr_type->elem_type,
25618 buf_create_from_str("pointer to noreturn not allowed"));
25619 return ErrorSemanticAnalyzeFail;
25620 } else if (elem_type->id == ZigTypeIdOpaque && lazy_ptr_type->ptr_len == PtrLenUnknown) {
25621 ir_add_error(ira, lazy_ptr_type->elem_type,
25622 buf_create_from_str("unknown-length pointer to opaque"));
25623 return ErrorSemanticAnalyzeFail;
25624 } else if (lazy_ptr_type->ptr_len == PtrLenC) {
25625 if (!type_allowed_in_extern(ira->codegen, elem_type)) {
25626 ir_add_error(ira, lazy_ptr_type->elem_type,
25627 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'",
25628 buf_ptr(&elem_type->name)));
25629 return ErrorSemanticAnalyzeFail;
25630 } else if (elem_type->id == ZigTypeIdOpaque) {
25631 ir_add_error(ira, lazy_ptr_type->elem_type,
25632 buf_sprintf("C pointers cannot point opaque types"));
25633 return ErrorSemanticAnalyzeFail;
25634 } else if (lazy_ptr_type->is_allowzero) {
25635 ir_add_error(ira, lazy_ptr_type->elem_type,
25636 buf_sprintf("C pointers always allow address zero"));
25637 return ErrorSemanticAnalyzeFail;
25638 }
25639 }
25640
25641 if (align_bytes != 0) {
25642 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown)))
25643 return err;
25644 if (!type_has_bits(elem_type))
25645 align_bytes = 0;
25646 }
25647 bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC;
25648 assert(val->type->id == ZigTypeIdMetaType);
25649 val->data.x_type = get_pointer_to_type_extra(ira->codegen, elem_type,
25650 lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes,
25651 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
25652 allow_zero);
25653 val->special = ConstValSpecialStatic;
25654 return ErrorNone;
25655 }
25656 case LazyValueIdOptType: {
25657 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(val->data.x_lazy);
25658 IrAnalyze *ira = lazy_opt_type->ira;
25659
25660 ZigType *payload_type = ir_resolve_type(ira, lazy_opt_type->payload_type);
25661 if (type_is_invalid(payload_type))
25662 return ErrorSemanticAnalyzeFail;
25663
25664 if (payload_type->id == ZigTypeIdOpaque || payload_type->id == ZigTypeIdUnreachable) {
25665 ir_add_error(ira, lazy_opt_type->payload_type,
25666 buf_sprintf("type '%s' cannot be optional", buf_ptr(&payload_type->name)));
25667 return ErrorSemanticAnalyzeFail;
25668 }
25669
25670 if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown)))
25671 return err;
25672
25673 assert(val->type->id == ZigTypeIdMetaType);
25674 val->data.x_type = get_optional_type(ira->codegen, payload_type);
25675 val->special = ConstValSpecialStatic;
25676 return ErrorNone;
25677 }
25678 case LazyValueIdFnType: {
25679 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(val->data.x_lazy);
25680 ZigType *fn_type = ir_resolve_lazy_fn_type(lazy_fn_type->ira, source_node, lazy_fn_type);
25681 if (fn_type == nullptr)
25682 return ErrorSemanticAnalyzeFail;
25683 val->special = ConstValSpecialStatic;
25684 assert(val->type->id == ZigTypeIdMetaType);
25685 val->data.x_type = fn_type;
25686 return ErrorNone;
25687 }
25688 }
25689 zig_unreachable();
25690}
25691
25692Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
25693 Error err;
25694 if ((err = ir_resolve_lazy_raw(source_node, val))) {
25695 if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) {
25696 source_node->already_traced_this_node = true;
25697 codegen->trace_err = add_error_note(codegen, codegen->trace_err, source_node,
25698 buf_create_from_str("referenced here"));
25699 }
25700 return err;
25701 }
25702 if (type_is_invalid(val->type)) {
25703 return ErrorSemanticAnalyzeFail;
25704 }
25705 return ErrorNone;
25706}
src/ir.hpp+3-3
......@@ -16,7 +16,9 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1616ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
1818 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node);
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
20
21Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val);
2022
2123ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
2224 ZigType *expected_type, AstNode *expected_type_source_node);
......@@ -28,6 +30,4 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal
2830 AstNode *source_node);
2931const char *float_op_to_name(BuiltinFnId op, bool llvm_name);
3032
31void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text);
32
3333#endif
src/tokenizer.cpp+1-1
......@@ -841,7 +841,7 @@ void tokenize(Buf *buf, Tokenization *out) {
841841 case TokenizeStateSawAmpersand:
842842 switch (c) {
843843 case '&':
844 tokenize_error(&t, "`&&` is invalid. Note that `and` is boolean AND.");
844 tokenize_error(&t, "`&&` is invalid. Note that `and` is boolean AND");
845845 break;
846846 case '=':
847847 set_token_id(&t, t.cur_tok, TokenIdBitAndEq);
std/array_list.zig+12-9
......@@ -6,20 +6,23 @@ const mem = std.mem;
66const Allocator = mem.Allocator;
77
88pub fn ArrayList(comptime T: type) type {
9 return AlignedArrayList(T, @alignOf(T));
9 return AlignedArrayList(T, null);
1010}
1111
12pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
12pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
1313 return struct {
1414 const Self = @This();
1515
1616 /// Use toSlice instead of slicing this directly, because if you don't
1717 /// specify the end position of the slice, this will potentially give
1818 /// you uninitialized memory.
19 items: []align(A) T,
19 items: Slice,
2020 len: usize,
2121 allocator: *Allocator,
2222
23 pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
24 pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T;
25
2326 /// Deinitialize with `deinit` or use `toOwnedSlice`.
2427 pub fn init(allocator: *Allocator) Self {
2528 return Self{
......@@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
3336 self.allocator.free(self.items);
3437 }
3538
36 pub fn toSlice(self: Self) []align(A) T {
39 pub fn toSlice(self: Self) Slice {
3740 return self.items[0..self.len];
3841 }
3942
40 pub fn toSliceConst(self: Self) []align(A) const T {
43 pub fn toSliceConst(self: Self) SliceConst {
4144 return self.items[0..self.len];
4245 }
4346
......@@ -69,7 +72,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
6972 /// ArrayList takes ownership of the passed in slice. The slice must have been
7073 /// allocated with `allocator`.
7174 /// Deinitialize with `deinit` or use `toOwnedSlice`.
72 pub fn fromOwnedSlice(allocator: *Allocator, slice: []align(A) T) Self {
75 pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self {
7376 return Self{
7477 .items = slice,
7578 .len = slice.len,
......@@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
7881 }
7982
8083 /// The caller owns the returned memory. ArrayList becomes empty.
81 pub fn toOwnedSlice(self: *Self) []align(A) T {
84 pub fn toOwnedSlice(self: *Self) Slice {
8285 const allocator = self.allocator;
8386 const result = allocator.shrink(self.items, self.len);
8487 self.* = init(allocator);
......@@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
9396 self.items[n] = item;
9497 }
9598
96 pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void {
99 pub fn insertSlice(self: *Self, n: usize, items: SliceConst) !void {
97100 try self.ensureCapacity(self.len + items.len);
98101 self.len += items.len;
99102
......@@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
141144 return self.swapRemove(i);
142145 }
143146
144 pub fn appendSlice(self: *Self, items: []align(A) const T) !void {
147 pub fn appendSlice(self: *Self, items: SliceConst) !void {
145148 try self.ensureCapacity(self.len + items.len);
146149 mem.copy(T, self.items[self.len..], items);
147150 self.len += items.len;
test/compile_errors.zig+71-44
......@@ -2,6 +2,33 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "alignment of enum field specified",
7 \\const Number = enum {
8 \\ a,
9 \\ b align(i32),
10 \\};
11 \\export fn entry1() void {
12 \\ var x: Number = undefined;
13 \\}
14 ,
15 "tmp.zig:3:13: error: structs and unions, not enums, support field alignment",
16 "tmp.zig:1:16: note: consider 'union(enum)' here",
17 );
18
19 cases.add(
20 "bad alignment type",
21 \\export fn entry1() void {
22 \\ var x: []align(true) i32 = undefined;
23 \\}
24 \\export fn entry2() void {
25 \\ var x: *align(f64(12.34)) i32 = undefined;
26 \\}
27 ,
28 "tmp.zig:2:20: error: expected type 'u29', found 'bool'",
29 "tmp.zig:5:22: error: fractional component prevents float value 12.340000 from being casted to type 'u29'",
30 );
31
532 cases.addCase(x: {
633 var tc = cases.create("variable in inline assembly template cannot be found",
734 \\export fn entry() void {
......@@ -10,7 +37,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1037 \\ : [bar] "=r" (-> usize)
1138 \\ );
1239 \\}
13 , "tmp.zig:2:14: error: could not find 'foo' in the inputs or outputs.");
40 , "tmp.zig:2:14: error: could not find 'foo' in the inputs or outputs");
1441 tc.target = tests.Target{
1542 .Cross = tests.CrossTarget{
1643 .arch = .x86_64,
......@@ -53,8 +80,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5380 \\}
5481 ,
5582 "tmp.zig:8:1: error: '@Frame(rangeSum)' depends on itself",
56 "tmp.zig:15:33: note: when analyzing type '@Frame(rangeSumIndirect)' here",
57 "tmp.zig:26:25: note: when analyzing type '@Frame(rangeSum)' here",
83 "tmp.zig:15:33: note: when analyzing type '@Frame(rangeSum)' here",
84 "tmp.zig:26:25: note: when analyzing type '@Frame(rangeSumIndirect)' here",
5885 );
5986
6087 cases.add(
......@@ -245,7 +272,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
245272 ,
246273 "tmp.zig:4:1: error: unable to determine async function frame of 'amain'",
247274 "tmp.zig:5:10: note: analysis of function 'other' depends on the frame",
248 "tmp.zig:8:13: note: depends on the frame here",
275 "tmp.zig:8:13: note: referenced here",
249276 );
250277
251278 cases.add(
......@@ -258,7 +285,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
258285 \\}
259286 ,
260287 "tmp.zig:4:1: error: cannot resolve '@Frame(amain)': function not fully analyzed yet",
261 "tmp.zig:5:13: note: depends on its own frame here",
288 "tmp.zig:5:13: note: referenced here",
262289 );
263290
264291 cases.add(
......@@ -404,7 +431,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
404431 \\ const foo: Foo = undefined;
405432 \\}
406433 ,
407 "tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
434 "tmp.zig:2:8: error: use of undefined value here causes undefined behavior",
408435 );
409436
410437 cases.add(
......@@ -470,7 +497,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
470497 );
471498
472499 cases.add(
473 "Generic function where return type is self-referenced",
500 "generic function where return type is self-referenced",
474501 \\fn Foo(comptime T: type) Foo(T) {
475502 \\ return struct{ x: T };
476503 \\}
......@@ -481,7 +508,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
481508 \\}
482509 ,
483510 "tmp.zig:1:29: error: evaluation exceeded 1000 backwards branches",
484 "tmp.zig:1:29: note: called from here",
511 "tmp.zig:5:18: note: referenced here",
485512 );
486513
487514 cases.add(
......@@ -645,7 +672,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
645672 \\const A = struct { a : A, };
646673 \\export fn entry() usize { return @sizeOf(A); }
647674 ,
648 "tmp.zig:1:11: error: struct 'A' contains itself",
675 "tmp.zig:1:11: error: struct 'A' depends on itself",
649676 );
650677
651678 cases.add(
......@@ -655,7 +682,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
655682 \\const C = struct { a : A, };
656683 \\export fn entry() usize { return @sizeOf(A); }
657684 ,
658 "tmp.zig:1:11: error: struct 'A' contains itself",
685 "tmp.zig:1:11: error: struct 'A' depends on itself",
659686 );
660687
661688 cases.add(
......@@ -670,7 +697,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
670697 \\ return @sizeOf(@typeOf(foo.x));
671698 \\}
672699 ,
673 "tmp.zig:1:13: error: struct 'Foo' contains itself",
700 "tmp.zig:1:13: error: struct 'Foo' depends on itself",
674701 "tmp.zig:8:28: note: referenced here",
675702 );
676703
......@@ -689,7 +716,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
689716 \\}
690717 ,
691718 "tmp.zig:7:9: error: dependency loop detected",
692 "tmp.zig:2:19: note: called from here",
719 "tmp.zig:2:19: note: referenced here",
693720 "tmp.zig:10:21: note: referenced here",
694721 );
695722
......@@ -703,7 +730,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
703730 \\ var s: Foo = Foo.E;
704731 \\}
705732 ,
706 "tmp.zig:1:17: error: 'Foo' depends on itself",
733 "tmp.zig:1:17: error: enum 'Foo' depends on itself",
707734 );
708735
709736 cases.add(
......@@ -866,7 +893,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
866893 break :x tc;
867894 });
868895
869 cases.addTest(
896 cases.add(
870897 "export generic function",
871898 \\export fn foo(num: var) i32 {
872899 \\ return 0;
......@@ -875,17 +902,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
875902 "tmp.zig:1:15: error: parameter of type 'var' not allowed in function with calling convention 'ccc'",
876903 );
877904
878 cases.addTest(
905 cases.add(
879906 "C pointer to c_void",
880907 \\export fn a() void {
881908 \\ var x: *c_void = undefined;
882909 \\ var y: [*c]c_void = x;
883910 \\}
884911 ,
885 "tmp.zig:3:12: error: C pointers cannot point opaque types",
912 "tmp.zig:3:16: error: C pointers cannot point opaque types",
886913 );
887914
888 cases.addTest(
915 cases.add(
889916 "directly embedding opaque type in struct and union",
890917 \\const O = @OpaqueType();
891918 \\const Foo = struct {
......@@ -906,7 +933,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
906933 "tmp.zig:7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
907934 );
908935
909 cases.addTest(
936 cases.add(
910937 "implicit cast between C pointer and Zig pointer - bad const/align/child",
911938 \\export fn a() void {
912939 \\ var x: [*c]u8 = undefined;
......@@ -942,7 +969,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
942969 "tmp.zig:23:22: error: expected type '[*c]u32', found '*u8'",
943970 );
944971
945 cases.addTest(
972 cases.add(
946973 "implicit casting null c pointer to zig pointer",
947974 \\comptime {
948975 \\ var c_ptr: [*c]u8 = 0;
......@@ -952,7 +979,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
952979 "tmp.zig:3:24: error: null pointer casted to type '*u8'",
953980 );
954981
955 cases.addTest(
982 cases.add(
956983 "implicit casting undefined c pointer to zig pointer",
957984 \\comptime {
958985 \\ var c_ptr: [*c]u8 = undefined;
......@@ -962,7 +989,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
962989 "tmp.zig:3:24: error: use of undefined value here causes undefined behavior",
963990 );
964991
965 cases.addTest(
992 cases.add(
966993 "implicit casting C pointers which would mess up null semantics",
967994 \\export fn entry() void {
968995 \\ var slice: []const u8 = "aoeu";
......@@ -987,7 +1014,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
9871014 "tmp.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'",
9881015 );
9891016
990 cases.addTest(
1017 cases.add(
9911018 "implicit casting too big integers to C pointers",
9921019 \\export fn a() void {
9931020 \\ var ptr: [*c]u8 = (1 << 64) + 1;
......@@ -1001,14 +1028,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10011028 "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",
10021029 );
10031030
1004 cases.addTest(
1031 cases.add(
10051032 "C pointer pointing to non C ABI compatible type or has align attr",
10061033 \\const Foo = struct {};
10071034 \\export fn a() void {
10081035 \\ const T = [*c]Foo;
10091036 \\}
10101037 ,
1011 "tmp.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
1038 "tmp.zig:3:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
10121039 );
10131040
10141041 cases.addCase(x: {
......@@ -1029,7 +1056,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10291056 break :x tc;
10301057 });
10311058
1032 cases.addTest(
1059 cases.add(
10331060 "assign to invalid dereference",
10341061 \\export fn entry() void {
10351062 \\ 'a'.* = 1;
......@@ -1038,7 +1065,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10381065 "tmp.zig:2:8: error: attempt to dereference non-pointer type 'comptime_int'",
10391066 );
10401067
1041 cases.addTest(
1068 cases.add(
10421069 "take slice of invalid dereference",
10431070 \\export fn entry() void {
10441071 \\ const x = 'a'.*[0..];
......@@ -1047,7 +1074,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10471074 "tmp.zig:2:18: error: attempt to dereference non-pointer type 'comptime_int'",
10481075 );
10491076
1050 cases.addTest(
1077 cases.add(
10511078 "@truncate undefined value",
10521079 \\export fn entry() void {
10531080 \\ var z = @truncate(u8, u16(undefined));
......@@ -1091,7 +1118,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10911118 \\ return 5678;
10921119 \\}
10931120 ,
1094 "tmp.zig:2:12: error: `&&` is invalid. Note that `and` is boolean AND.",
1121 "tmp.zig:2:12: error: `&&` is invalid. Note that `and` is boolean AND",
10951122 );
10961123
10971124 cases.add(
......@@ -1935,7 +1962,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19351962 "unknown length pointer to opaque",
19361963 \\export const T = [*]@OpaqueType();
19371964 ,
1938 "tmp.zig:1:18: error: unknown-length pointer to opaque",
1965 "tmp.zig:1:21: error: unknown-length pointer to opaque",
19391966 );
19401967
19411968 cases.add(
......@@ -2924,7 +2951,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29242951 \\fn a() *noreturn {}
29252952 \\export fn entry() void { _ = a(); }
29262953 ,
2927 "tmp.zig:1:8: error: pointer to noreturn not allowed",
2954 "tmp.zig:1:9: error: pointer to noreturn not allowed",
29282955 );
29292956
29302957 cases.add(
......@@ -3596,7 +3623,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35963623 );
35973624
35983625 cases.add(
3599 "non constant expression in array size outside function",
3626 "non constant expression in array size",
36003627 \\const Foo = struct {
36013628 \\ y: [get()]u8,
36023629 \\};
......@@ -3606,8 +3633,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36063633 \\export fn entry() usize { return @sizeOf(@typeOf(Foo)); }
36073634 ,
36083635 "tmp.zig:5:25: error: unable to evaluate constant expression",
3609 "tmp.zig:2:12: note: called from here",
3610 "tmp.zig:2:8: note: called from here",
3636 "tmp.zig:2:12: note: referenced here",
36113637 );
36123638
36133639 cases.add(
......@@ -3701,7 +3727,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37013727 \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
37023728 ,
37033729 "tmp.zig:3:14: error: division by zero",
3704 "tmp.zig:1:14: note: called from here",
3730 "tmp.zig:1:14: note: referenced here",
37053731 );
37063732
37073733 cases.add(
......@@ -4133,7 +4159,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41334159 \\export fn entry() usize { return @sizeOf(@typeOf(seventh_fib_number)); }
41344160 ,
41354161 "tmp.zig:3:21: error: evaluation exceeded 1000 backwards branches",
4136 "tmp.zig:3:21: note: called from here",
4162 "tmp.zig:1:37: note: referenced here",
4163 "tmp.zig:6:50: note: referenced here",
41374164 );
41384165
41394166 cases.add(
......@@ -4174,7 +4201,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41744201 \\export fn entry() usize { return @sizeOf(@typeOf(a)); }
41754202 ,
41764203 "tmp.zig:6:26: error: unable to evaluate constant expression",
4177 "tmp.zig:4:17: note: called from here",
4204 "tmp.zig:4:17: note: referenced here",
41784205 );
41794206
41804207 cases.add(
......@@ -4257,7 +4284,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42574284 \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
42584285 ,
42594286 "tmp.zig:3:12: error: negation caused overflow",
4260 "tmp.zig:1:14: note: called from here",
4287 "tmp.zig:1:14: note: referenced here",
42614288 );
42624289
42634290 cases.add(
......@@ -4270,7 +4297,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42704297 \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
42714298 ,
42724299 "tmp.zig:3:14: error: operation caused overflow",
4273 "tmp.zig:1:14: note: called from here",
4300 "tmp.zig:1:14: note: referenced here",
42744301 );
42754302
42764303 cases.add(
......@@ -4283,7 +4310,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42834310 \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
42844311 ,
42854312 "tmp.zig:3:14: error: operation caused overflow",
4286 "tmp.zig:1:14: note: called from here",
4313 "tmp.zig:1:14: note: referenced here",
42874314 );
42884315
42894316 cases.add(
......@@ -4296,7 +4323,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42964323 \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
42974324 ,
42984325 "tmp.zig:3:14: error: operation caused overflow",
4299 "tmp.zig:1:14: note: called from here",
4326 "tmp.zig:1:14: note: referenced here",
43004327 );
43014328
43024329 cases.add(
......@@ -4388,7 +4415,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
43884415 \\}
43894416 ,
43904417 "tmp.zig:3:7: error: unable to evaluate constant expression",
4391 "tmp.zig:16:19: note: called from here",
4418 "tmp.zig:16:19: note: referenced here",
43924419 );
43934420
43944421 cases.add(
......@@ -4717,7 +4744,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
47174744 \\}
47184745 ,
47194746 "tmp.zig:10:14: error: unable to evaluate constant expression",
4720 "tmp.zig:6:20: note: called from here",
4747 "tmp.zig:6:20: note: referenced here",
47214748 );
47224749
47234750 cases.add(
......@@ -5864,7 +5891,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58645891 \\}
58655892 ,
58665893 "tmp.zig:4:25: error: aoeu",
5867 "tmp.zig:1:36: note: called from here",
5894 "tmp.zig:1:36: note: referenced here",
58685895 "tmp.zig:12:20: note: referenced here",
58695896 );
58705897
......@@ -5939,7 +5966,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59395966 \\ var x: MultipleChoice = undefined;
59405967 \\}
59415968 ,
5942 "tmp.zig:2:14: error: non-enum union field assignment",
5969 "tmp.zig:2:14: error: untagged union field assignment",
59435970 "tmp.zig:1:24: note: consider 'union(enum)' here",
59445971 );
59455972
test/stage1/behavior/align.zig+15
......@@ -290,3 +290,18 @@ test "read 128-bit field from default aligned struct in global memory" {
290290 expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
291291 expect(12 == default_aligned_global.badguy);
292292}
293
294test "struct field explicit alignment" {
295 const S = struct {
296 const Node = struct {
297 next: *Node,
298 massive_byte: u8 align(64),
299 };
300 };
301
302 var node: S.Node = undefined;
303 node.massive_byte = 100;
304 expect(node.massive_byte == 100);
305 comptime expect(@typeOf(&node.massive_byte) == *align(64) u8);
306 expect(@ptrToInt(&node.massive_byte) % 64 == 0);
307}
test/stage1/behavior/misc.zig+15
......@@ -706,3 +706,18 @@ test "result location zero sized array inside struct field implicit cast to slic
706706 var foo = E{ .entries = [_]u32{} };
707707 expect(foo.entries.len == 0);
708708}
709
710var global_foo: *i32 = undefined;
711
712test "global variable assignment with optional unwrapping with var initialized to undefined" {
713 const S = struct {
714 var data: i32 = 1234;
715 fn foo() ?*i32 {
716 return &data;
717 }
718 };
719 global_foo = S.foo() orelse {
720 @panic("bad");
721 };
722 expect(global_foo.* == 1234);
723}