authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-23 15:54:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-23 15:54:51-04:00
logac4dd9d665ba195a6b21b3b033f27c865015ccac
treed3ae7db58ea39d073eda0fe70a9058f66cdebce3
parentbe0a9a72772566667d4c225972c6bc7c17b751ef
signaturelock-open Commit is signed but in an unrecognized format.

better handling of lazy structs

this case works now: ```zig const A = struct { b_list_pointer: *const []B, }; const B = struct { a_pointer: *const A, }; const b_list: []B = [_]B{}; const a = A{ .b_list_pointer = &b_list }; const obj = B{ .a_pointer = &a }; ```

5 files changed, 44 insertions(+), 42 deletions(-)

src/all_types.hpp+2-1
......@@ -1190,7 +1190,8 @@ struct ZigTypeStruct {
11901190 // whether any of the fields require comptime
11911191 // known after ResolveStatusZeroBitsKnown
11921192 bool requires_comptime;
1193 bool resolve_loop_flag;
1193 bool resolve_loop_flag_zero_bits;
1194 bool resolve_loop_flag_other;
11941195};
11951196
11961197struct ZigTypeOptional {
src/analyze.cpp+29-28
......@@ -685,7 +685,7 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
685685 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
686686 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
687687
688 switch (type_requires_comptime(g, ptr_type, entry)) {
688 switch (type_requires_comptime(g, ptr_type)) {
689689 case ReqCompTimeInvalid:
690690 zig_unreachable();
691691 case ReqCompTimeNo:
......@@ -1016,9 +1016,9 @@ Error type_val_resolve_is_opaque_type(CodeGen *g, ConstExprValue *type_val, bool
10161016 zig_unreachable();
10171017}
10181018
1019static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue *type_val, ZigType *parent_type) {
1019static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue *type_val) {
10201020 if (type_val->special != ConstValSpecialLazy) {
1021 return type_requires_comptime(g, type_val->data.x_type, parent_type);
1021 return type_requires_comptime(g, type_val->data.x_type);
10221022 }
10231023 switch (type_val->data.x_lazy->id) {
10241024 case LazyValueIdInvalid:
......@@ -1028,17 +1028,17 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10281028 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(type_val->data.x_lazy);
10291029 if (type_is_invalid(lazy_slice_type->elem_type))
10301030 return ReqCompTimeInvalid;
1031 return type_requires_comptime(g, lazy_slice_type->elem_type, parent_type);
1031 return type_requires_comptime(g, lazy_slice_type->elem_type);
10321032 }
10331033 case LazyValueIdPtrType: {
10341034 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
1035 return type_val_resolve_requires_comptime(g, lazy_ptr_type->elem_type_val, parent_type);
1035 return type_val_resolve_requires_comptime(g, lazy_ptr_type->elem_type_val);
10361036 }
10371037 case LazyValueIdFnType: {
10381038 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
10391039 if (lazy_fn_type->is_generic)
10401040 return ReqCompTimeYes;
1041 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->return_type, parent_type)) {
1041 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->return_type)) {
10421042 case ReqCompTimeInvalid:
10431043 return ReqCompTimeInvalid;
10441044 case ReqCompTimeYes:
......@@ -1051,7 +1051,7 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10511051 AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i);
10521052 bool param_is_var_args = param_node->data.param_decl.is_var_args;
10531053 if (param_is_var_args) break;
1054 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->param_types[i], parent_type)) {
1054 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->param_types[i])) {
10551055 case ReqCompTimeInvalid:
10561056 return ReqCompTimeInvalid;
10571057 case ReqCompTimeYes:
......@@ -1517,7 +1517,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
15171517 case ZigTypeIdVector:
15181518 case ZigTypeIdFnFrame:
15191519 case ZigTypeIdAnyFrame:
1520 switch (type_requires_comptime(g, type_entry, fn_entry->type_entry)) {
1520 switch (type_requires_comptime(g, type_entry)) {
15211521 case ReqCompTimeNo:
15221522 break;
15231523 case ReqCompTimeYes:
......@@ -1613,7 +1613,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
16131613 case ZigTypeIdVector:
16141614 case ZigTypeIdFnFrame:
16151615 case ZigTypeIdAnyFrame:
1616 switch (type_requires_comptime(g, fn_type_id.return_type, fn_entry->type_entry)) {
1616 switch (type_requires_comptime(g, fn_type_id.return_type)) {
16171617 case ReqCompTimeInvalid:
16181618 return g->builtin_types.entry_invalid;
16191619 case ReqCompTimeYes:
......@@ -1745,7 +1745,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
17451745
17461746 AstNode *decl_node = struct_type->data.structure.decl_node;
17471747
1748 if (struct_type->data.structure.resolve_loop_flag) {
1748 if (struct_type->data.structure.resolve_loop_flag_other) {
17491749 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
17501750 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
17511751 g->trace_err = add_node_error(g, decl_node,
......@@ -1760,7 +1760,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
17601760 size_t field_count = struct_type->data.structure.src_field_count;
17611761
17621762 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
1763 struct_type->data.structure.resolve_loop_flag = true;
1763 struct_type->data.structure.resolve_loop_flag_other = true;
17641764
17651765 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
17661766
......@@ -1877,7 +1877,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
18771877 struct_type->size_in_bits = size_in_bits;
18781878 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
18791879 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
1880 struct_type->data.structure.resolve_loop_flag = false;
1880 struct_type->data.structure.resolve_loop_flag_other = false;
18811881 struct_type->data.structure.host_int_bytes = host_int_bytes;
18821882
18831883 return ErrorNone;
......@@ -2275,7 +2275,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
22752275 AstNode *decl_node = struct_type->data.structure.decl_node;
22762276 assert(decl_node->type == NodeTypeContainerDecl);
22772277
2278 if (struct_type->data.structure.resolve_loop_flag) {
2278 if (struct_type->data.structure.resolve_loop_flag_zero_bits) {
22792279 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
22802280 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
22812281 g->trace_err = add_node_error(g, decl_node,
......@@ -2285,7 +2285,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
22852285 return ErrorSemanticAnalyzeFail;
22862286 }
22872287
2288 struct_type->data.structure.resolve_loop_flag = true;
2288 struct_type->data.structure.resolve_loop_flag_zero_bits = true;
22892289
22902290 assert(!struct_type->data.structure.fields);
22912291 size_t field_count = decl_node->data.container_decl.fields.length;
......@@ -2343,7 +2343,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
23432343 type_struct_field->src_index = i;
23442344 type_struct_field->gen_index = SIZE_MAX;
23452345
2346 switch (type_val_resolve_requires_comptime(g, field_type_val, struct_type)) {
2346 switch (type_val_resolve_requires_comptime(g, field_type_val)) {
23472347 case ReqCompTimeYes:
23482348 struct_type->data.structure.requires_comptime = true;
23492349 break;
......@@ -2370,7 +2370,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
23702370 gen_field_index += 1;
23712371 }
23722372
2373 struct_type->data.structure.resolve_loop_flag = false;
2373 struct_type->data.structure.resolve_loop_flag_zero_bits = false;
23742374 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
23752375 if (gen_field_index != 0) {
23762376 struct_type->abi_size = SIZE_MAX;
......@@ -2400,7 +2400,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
24002400
24012401 AstNode *decl_node = struct_type->data.structure.decl_node;
24022402
2403 if (struct_type->data.structure.resolve_loop_flag) {
2403 if (struct_type->data.structure.resolve_loop_flag_other) {
24042404 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
24052405 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
24062406 g->trace_err = add_node_error(g, decl_node,
......@@ -2409,7 +2409,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
24092409 return ErrorSemanticAnalyzeFail;
24102410 }
24112411
2412 struct_type->data.structure.resolve_loop_flag = true;
2412 struct_type->data.structure.resolve_loop_flag_other = true;
24132413 assert(decl_node->type == NodeTypeContainerDecl);
24142414
24152415 size_t field_count = struct_type->data.structure.src_field_count;
......@@ -2444,7 +2444,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
24442444 }
24452445 }
24462446
2447 struct_type->data.structure.resolve_loop_flag = false;
2447 struct_type->data.structure.resolve_loop_flag_other = false;
24482448
24492449 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
24502450 return ErrorSemanticAnalyzeFail;
......@@ -2614,7 +2614,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
26142614 return ErrorSemanticAnalyzeFail;
26152615 }
26162616
2617 switch (type_requires_comptime(g, field_type, union_type)) {
2617 switch (type_requires_comptime(g, field_type)) {
26182618 case ReqCompTimeInvalid:
26192619 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
26202620 return ErrorSemanticAnalyzeFail;
......@@ -4959,11 +4959,8 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
49594959 zig_unreachable();
49604960}
49614961
4962ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty, ZigType *parent_type) {
4962ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty) {
49634963 Error err;
4964 if (ty == parent_type) {
4965 return ReqCompTimeNo;
4966 }
49674964 switch (ty->id) {
49684965 case ZigTypeIdInvalid:
49694966 zig_unreachable();
......@@ -4977,8 +4974,12 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty, ZigType *parent_type
49774974 case ZigTypeIdArgTuple:
49784975 return ReqCompTimeYes;
49794976 case ZigTypeIdArray:
4980 return type_requires_comptime(g, ty->data.array.child_type, parent_type);
4977 return type_requires_comptime(g, ty->data.array.child_type);
49814978 case ZigTypeIdStruct:
4979 if (ty->data.structure.resolve_loop_flag_zero_bits) {
4980 // Does a struct which contains a pointer field to itself require comptime? No.
4981 return ReqCompTimeNo;
4982 }
49824983 if ((err = type_resolve(g, ty, ResolveStatusZeroBitsKnown)))
49834984 return ReqCompTimeInvalid;
49844985 return ty->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
......@@ -4987,14 +4988,14 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty, ZigType *parent_type
49874988 return ReqCompTimeInvalid;
49884989 return ty->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
49894990 case ZigTypeIdOptional:
4990 return type_requires_comptime(g, ty->data.maybe.child_type, parent_type);
4991 return type_requires_comptime(g, ty->data.maybe.child_type);
49914992 case ZigTypeIdErrorUnion:
4992 return type_requires_comptime(g, ty->data.error_union.payload_type, parent_type);
4993 return type_requires_comptime(g, ty->data.error_union.payload_type);
49934994 case ZigTypeIdPointer:
49944995 if (ty->data.pointer.child_type->id == ZigTypeIdOpaque) {
49954996 return ReqCompTimeNo;
49964997 } else {
4997 return type_requires_comptime(g, ty->data.pointer.child_type, parent_type);
4998 return type_requires_comptime(g, ty->data.pointer.child_type);
49984999 }
49995000 case ZigTypeIdFn:
50005001 return ty->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo;
src/analyze.hpp+1-1
......@@ -221,7 +221,7 @@ enum ReqCompTime {
221221 ReqCompTimeNo,
222222 ReqCompTimeYes,
223223};
224ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry, ZigType *parent_type);
224ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);
225225
226226OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry);
227227
src/codegen.cpp+2-2
......@@ -3688,7 +3688,7 @@ static void render_async_spills(CodeGen *g) {
36883688 }
36893689 if (ir_get_var_is_comptime(var))
36903690 continue;
3691 switch (type_requires_comptime(g, var->var_type, nullptr)) {
3691 switch (type_requires_comptime(g, var->var_type)) {
36923692 case ReqCompTimeInvalid:
36933693 zig_unreachable();
36943694 case ReqCompTimeYes:
......@@ -7049,7 +7049,7 @@ static void do_code_gen(CodeGen *g) {
70497049 }
70507050 if (ir_get_var_is_comptime(var))
70517051 continue;
7052 switch (type_requires_comptime(g, var->var_type, nullptr)) {
7052 switch (type_requires_comptime(g, var->var_type)) {
70537053 case ReqCompTimeInvalid:
70547054 zig_unreachable();
70557055 case ReqCompTimeYes:
src/ir.cpp+10-10
......@@ -14234,7 +14234,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1423414234 }
1423514235 }
1423614236
14237 switch (type_requires_comptime(ira->codegen, result_type, nullptr)) {
14237 switch (type_requires_comptime(ira->codegen, result_type)) {
1423814238 case ReqCompTimeInvalid:
1423914239 result_type = ira->codegen->builtin_types.entry_invalid;
1424014240 break;
......@@ -15200,7 +15200,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1520015200 }
1520115201
1520215202 if (!comptime_arg) {
15203 switch (type_requires_comptime(ira->codegen, casted_arg->value.type, nullptr)) {
15203 switch (type_requires_comptime(ira->codegen, casted_arg->value.type)) {
1520415204 case ReqCompTimeYes:
1520515205 ir_add_error(ira, casted_arg,
1520615206 buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value.type->name)));
......@@ -15401,7 +15401,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1540115401 }
1540215402 }
1540315403
15404 switch (type_requires_comptime(ira->codegen, child_type, nullptr)) {
15404 switch (type_requires_comptime(ira->codegen, child_type)) {
1540515405 case ReqCompTimeInvalid:
1540615406 return ira->codegen->invalid_instruction;
1540715407 case ReqCompTimeYes:
......@@ -15794,7 +15794,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1579415794 inst_fn_type_id.return_type = specified_return_type;
1579515795 }
1579615796
15797 switch (type_requires_comptime(ira->codegen, specified_return_type, nullptr)) {
15797 switch (type_requires_comptime(ira->codegen, specified_return_type)) {
1579815798 case ReqCompTimeYes:
1579915799 // Throw out our work and call the function as if it were comptime.
1580015800 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr,
......@@ -16601,7 +16601,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1660116601 break;
1660216602 }
1660316603
16604 switch (type_requires_comptime(ira->codegen, resolved_type, nullptr)) {
16604 switch (type_requires_comptime(ira->codegen, resolved_type)) {
1660516605 case ReqCompTimeInvalid:
1660616606 return ira->codegen->invalid_instruction;
1660716607 case ReqCompTimeYes:
......@@ -17049,7 +17049,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1704917049 }
1705017050 } else {
1705117051 // runtime known element index
17052 switch (type_requires_comptime(ira->codegen, return_type, nullptr)) {
17052 switch (type_requires_comptime(ira->codegen, return_type)) {
1705317053 case ReqCompTimeYes:
1705417054 ir_add_error(ira, elem_index,
1705517055 buf_sprintf("values of type '%s' must be comptime known, but index value is runtime known",
......@@ -19034,7 +19034,7 @@ static IrInstruction *ir_analyze_union_init(IrAnalyze *ira, IrInstruction *sourc
1903419034 }
1903519035
1903619036 bool is_comptime = ir_should_inline(ira->new_irb.exec, source_instruction->scope)
19037 || type_requires_comptime(ira->codegen, union_type, nullptr) == ReqCompTimeYes;
19037 || type_requires_comptime(ira->codegen, union_type) == ReqCompTimeYes;
1903819038
1903919039 IrInstruction *result = ir_get_deref(ira, source_instruction, result_loc, nullptr);
1904019040 if (is_comptime && !instr_is_comptime(result)) {
......@@ -19082,7 +19082,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1908219082 ZigList<IrInstruction *> const_ptrs = {};
1908319083
1908419084 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope)
19085 || type_requires_comptime(ira->codegen, container_type, nullptr) == ReqCompTimeYes;
19085 || type_requires_comptime(ira->codegen, container_type) == ReqCompTimeYes;
1908619086
1908719087
1908819088 // Here we iterate over the fields that have been initialized, and emit
......@@ -19260,7 +19260,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1926019260 }
1926119261
1926219262 bool is_comptime;
19263 switch (type_requires_comptime(ira->codegen, container_type, nullptr)) {
19263 switch (type_requires_comptime(ira->codegen, container_type)) {
1926419264 case ReqCompTimeInvalid:
1926519265 return ira->codegen->invalid_instruction;
1926619266 case ReqCompTimeNo:
......@@ -25502,7 +25502,7 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
2550225502 lazy_fn_type->param_types[fn_type_id.next_param_index]);
2550325503 if (type_is_invalid(param_type))
2550425504 return nullptr;
25505 switch (type_requires_comptime(codegen, param_type, nullptr)) {
25505 switch (type_requires_comptime(codegen, param_type)) {
2550625506 case ReqCompTimeYes:
2550725507 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
2550825508 exec_add_error_node(codegen, exec, source_node,