authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-10 21:58:05-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-11 13:11:58-05:00
log5b279434986b18d07ceec217c5c402a589ccc0b4
treeeac7bb2993f594bb5471633a92671d9b63833b27
parentde30438ed2efb909538f3177cced441b57eb1a5d
signaturelock-open Commit is signed but in an unrecognized format.

implement anon struct literal syntax

This implements stage1 parser support for anonymous struct literal syntax (see #685), as well as semantic analysis support for anonymous struct literals and anonymous list literals (see #208). The semantic analysis works when there is a type coercion in the result location; inferring the struct type based on the values in the literal is not implemented yet. Also remaining to do is zig fmt support for this new syntax and documentation updates.

9 files changed, 174 insertions(+), 120 deletions(-)

lib/std/builtin.zig+2-31
...@@ -90,40 +90,11 @@ pub const Mode = enum {...@@ -90,40 +90,11 @@ pub const Mode = enum {
90 ReleaseSmall,90 ReleaseSmall,
91};91};
9292
93/// This data structure is used by the Zig language code generation and93pub const TypeId = @TagType(TypeInfo);
94/// therefore must be kept in sync with the compiler implementation.
95pub const TypeId = enum {
96 Type,
97 Void,
98 Bool,
99 NoReturn,
100 Int,
101 Float,
102 Pointer,
103 Array,
104 Struct,
105 ComptimeFloat,
106 ComptimeInt,
107 Undefined,
108 Null,
109 Optional,
110 ErrorUnion,
111 ErrorSet,
112 Enum,
113 Union,
114 Fn,
115 BoundFn,
116 ArgTuple,
117 Opaque,
118 Frame,
119 AnyFrame,
120 Vector,
121 EnumLiteral,
122};
12394
124/// This data structure is used by the Zig language code generation and95/// This data structure is used by the Zig language code generation and
125/// therefore must be kept in sync with the compiler implementation.96/// therefore must be kept in sync with the compiler implementation.
126pub const TypeInfo = union(TypeId) {97pub const TypeInfo = union(enum) {
127 Type: void,98 Type: void,
128 Void: void,99 Void: void,
129 Bool: void,100 Bool: void,
src/all_types.hpp+4-3
...@@ -1237,6 +1237,7 @@ struct TypeStructField {...@@ -1237,6 +1237,7 @@ struct TypeStructField {
1237enum ResolveStatus {1237enum ResolveStatus {
1238 ResolveStatusUnstarted,1238 ResolveStatusUnstarted,
1239 ResolveStatusInvalid,1239 ResolveStatusInvalid,
1240 ResolveStatusBeingInferred,
1240 ResolveStatusZeroBitsKnown,1241 ResolveStatusZeroBitsKnown,
1241 ResolveStatusAlignmentKnown,1242 ResolveStatusAlignmentKnown,
1242 ResolveStatusSizeKnown,1243 ResolveStatusSizeKnown,
...@@ -1285,6 +1286,7 @@ struct ZigTypeStruct {...@@ -1285,6 +1286,7 @@ struct ZigTypeStruct {
1285 bool requires_comptime;1286 bool requires_comptime;
1286 bool resolve_loop_flag_zero_bits;1287 bool resolve_loop_flag_zero_bits;
1287 bool resolve_loop_flag_other;1288 bool resolve_loop_flag_other;
1289 bool is_inferred;
1288};1290};
12891291
1290struct ZigTypeOptional {1292struct ZigTypeOptional {
...@@ -2812,7 +2814,7 @@ struct IrInstructionElemPtr {...@@ -2812,7 +2814,7 @@ struct IrInstructionElemPtr {
28122814
2813 IrInstruction *array_ptr;2815 IrInstruction *array_ptr;
2814 IrInstruction *elem_index;2816 IrInstruction *elem_index;
2815 IrInstruction *init_array_type;2817 AstNode *init_array_type_source_node;
2816 PtrLen ptr_len;2818 PtrLen ptr_len;
2817 bool safety_check_on;2819 bool safety_check_on;
2818};2820};
...@@ -2909,11 +2911,11 @@ struct IrInstructionResizeSlice {...@@ -2909,11 +2911,11 @@ struct IrInstructionResizeSlice {
2909struct IrInstructionContainerInitList {2911struct IrInstructionContainerInitList {
2910 IrInstruction base;2912 IrInstruction base;
29112913
2912 IrInstruction *container_type;
2913 IrInstruction *elem_type;2914 IrInstruction *elem_type;
2914 size_t item_count;2915 size_t item_count;
2915 IrInstruction **elem_result_loc_list;2916 IrInstruction **elem_result_loc_list;
2916 IrInstruction *result_loc;2917 IrInstruction *result_loc;
2918 AstNode *init_array_type_source_node;
2917};2919};
29182920
2919struct IrInstructionContainerInitFieldsField {2921struct IrInstructionContainerInitFieldsField {
...@@ -2926,7 +2928,6 @@ struct IrInstructionContainerInitFieldsField {...@@ -2926,7 +2928,6 @@ struct IrInstructionContainerInitFieldsField {
2926struct IrInstructionContainerInitFields {2928struct IrInstructionContainerInitFields {
2927 IrInstruction base;2929 IrInstruction base;
29282930
2929 IrInstruction *container_type;
2930 size_t field_count;2931 size_t field_count;
2931 IrInstructionContainerInitFieldsField *fields;2932 IrInstructionContainerInitFieldsField *fields;
2932 IrInstruction *result_loc;2933 IrInstruction *result_loc;
src/analyze.cpp+8-2
...@@ -140,7 +140,6 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope...@@ -140,7 +140,6 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
140static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,140static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,
141 ZigType *import, Buf *bare_name)141 ZigType *import, Buf *bare_name)
142{142{
143 assert(node == nullptr || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
144 ScopeDecls *scope = allocate<ScopeDecls>(1);143 ScopeDecls *scope = allocate<ScopeDecls>(1);
145 init_scope(g, &scope->base, ScopeIdDecls, node, parent);144 init_scope(g, &scope->base, ScopeIdDecls, node, parent);
146 scope->decl_table.init(4);145 scope->decl_table.init(4);
...@@ -346,6 +345,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -346,6 +345,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
346 switch (status) {345 switch (status) {
347 case ResolveStatusInvalid:346 case ResolveStatusInvalid:
348 zig_unreachable();347 zig_unreachable();
348 case ResolveStatusBeingInferred:
349 zig_unreachable();
349 case ResolveStatusUnstarted:350 case ResolveStatusUnstarted:
350 case ResolveStatusZeroBitsKnown:351 case ResolveStatusZeroBitsKnown:
351 return true;352 return true;
...@@ -362,6 +363,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -362,6 +363,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
362 switch (status) {363 switch (status) {
363 case ResolveStatusInvalid:364 case ResolveStatusInvalid:
364 zig_unreachable();365 zig_unreachable();
366 case ResolveStatusBeingInferred:
367 zig_unreachable();
365 case ResolveStatusUnstarted:368 case ResolveStatusUnstarted:
366 return true;369 return true;
367 case ResolveStatusZeroBitsKnown:370 case ResolveStatusZeroBitsKnown:
...@@ -6132,6 +6135,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {...@@ -6132,6 +6135,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
6132 continue;6135 continue;
6133 if (instruction->ref_count == 0)6136 if (instruction->ref_count == 0)
6134 continue;6137 continue;
6138 if ((err = type_resolve(g, instruction->value.type, ResolveStatusZeroBitsKnown)))
6139 return ErrorSemanticAnalyzeFail;
6135 if (!type_has_bits(instruction->value.type))6140 if (!type_has_bits(instruction->value.type))
6136 continue;6141 continue;
6137 if (scope_needs_spill(instruction->scope)) {6142 if (scope_needs_spill(instruction->scope)) {
...@@ -6271,6 +6276,8 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {...@@ -6271,6 +6276,8 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
6271 switch (status) {6276 switch (status) {
6272 case ResolveStatusUnstarted:6277 case ResolveStatusUnstarted:
6273 return ErrorNone;6278 return ErrorNone;
6279 case ResolveStatusBeingInferred:
6280 zig_unreachable();
6274 case ResolveStatusInvalid:6281 case ResolveStatusInvalid:
6275 zig_unreachable();6282 zig_unreachable();
6276 case ResolveStatusZeroBitsKnown:6283 case ResolveStatusZeroBitsKnown:
...@@ -9038,4 +9045,3 @@ Error analyze_import(CodeGen *g, ZigType *source_import, Buf *import_target_str,...@@ -9038,4 +9045,3 @@ Error analyze_import(CodeGen *g, ZigType *source_import, Buf *import_target_str,
9038 *out_import = add_source_file(g, target_package, resolved_path, import_code, source_kind);9045 *out_import = add_source_file(g, target_package, resolved_path, import_code, source_kind);
9039 return ErrorNone;9046 return ErrorNone;
9040}9047}
9041
src/ast_render.cpp+3-1
...@@ -821,7 +821,9 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -821,7 +821,9 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
821 break;821 break;
822 }822 }
823 case NodeTypeContainerInitExpr:823 case NodeTypeContainerInitExpr:
824 render_node_ungrouped(ar, node->data.container_init_expr.type);824 if (node->data.container_init_expr.type != nullptr) {
825 render_node_ungrouped(ar, node->data.container_init_expr.type);
826 }
825 if (node->data.container_init_expr.kind == ContainerInitKindStruct) {827 if (node->data.container_init_expr.kind == ContainerInitKindStruct) {
826 fprintf(ar->f, "{\n");828 fprintf(ar->f, "{\n");
827 ar->indent += ar->indent_size;829 ar->indent += ar->indent_size;
src/ir.cpp+129-75
...@@ -1350,18 +1350,17 @@ static IrInstruction *ir_build_return_ptr(IrAnalyze *ira, IrInstruction *source_...@@ -1350,18 +1350,17 @@ static IrInstruction *ir_build_return_ptr(IrAnalyze *ira, IrInstruction *source_
13501350
1351static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,1351static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1352 IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len,1352 IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len,
1353 IrInstruction *init_array_type)1353 AstNode *init_array_type_source_node)
1354{1354{
1355 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node);1355 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node);
1356 instruction->array_ptr = array_ptr;1356 instruction->array_ptr = array_ptr;
1357 instruction->elem_index = elem_index;1357 instruction->elem_index = elem_index;
1358 instruction->safety_check_on = safety_check_on;1358 instruction->safety_check_on = safety_check_on;
1359 instruction->ptr_len = ptr_len;1359 instruction->ptr_len = ptr_len;
1360 instruction->init_array_type = init_array_type;1360 instruction->init_array_type_source_node = init_array_type_source_node;
13611361
1362 ir_ref_instruction(array_ptr, irb->current_basic_block);1362 ir_ref_instruction(array_ptr, irb->current_basic_block);
1363 ir_ref_instruction(elem_index, irb->current_basic_block);1363 ir_ref_instruction(elem_index, irb->current_basic_block);
1364 if (init_array_type != nullptr) ir_ref_instruction(init_array_type, irb->current_basic_block);
13651364
1366 return &instruction->base;1365 return &instruction->base;
1367}1366}
...@@ -1575,17 +1574,16 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -1575,17 +1574,16 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
1575}1574}
15761575
1577static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,1576static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
1578 IrInstruction *container_type, size_t item_count, IrInstruction **elem_result_loc_list,1577 size_t item_count, IrInstruction **elem_result_loc_list, IrInstruction *result_loc,
1579 IrInstruction *result_loc)1578 AstNode *init_array_type_source_node)
1580{1579{
1581 IrInstructionContainerInitList *container_init_list_instruction =1580 IrInstructionContainerInitList *container_init_list_instruction =
1582 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);1581 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
1583 container_init_list_instruction->container_type = container_type;
1584 container_init_list_instruction->item_count = item_count;1582 container_init_list_instruction->item_count = item_count;
1585 container_init_list_instruction->elem_result_loc_list = elem_result_loc_list;1583 container_init_list_instruction->elem_result_loc_list = elem_result_loc_list;
1586 container_init_list_instruction->result_loc = result_loc;1584 container_init_list_instruction->result_loc = result_loc;
1585 container_init_list_instruction->init_array_type_source_node = init_array_type_source_node;
15871586
1588 ir_ref_instruction(container_type, irb->current_basic_block);
1589 for (size_t i = 0; i < item_count; i += 1) {1587 for (size_t i = 0; i < item_count; i += 1) {
1590 ir_ref_instruction(elem_result_loc_list[i], irb->current_basic_block);1588 ir_ref_instruction(elem_result_loc_list[i], irb->current_basic_block);
1591 }1589 }
...@@ -1595,17 +1593,14 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,...@@ -1595,17 +1593,14 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,
1595}1593}
15961594
1597static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node,1595static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node,
1598 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields,1596 size_t field_count, IrInstructionContainerInitFieldsField *fields, IrInstruction *result_loc)
1599 IrInstruction *result_loc)
1600{1597{
1601 IrInstructionContainerInitFields *container_init_fields_instruction =1598 IrInstructionContainerInitFields *container_init_fields_instruction =
1602 ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node);1599 ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node);
1603 container_init_fields_instruction->container_type = container_type;
1604 container_init_fields_instruction->field_count = field_count;1600 container_init_fields_instruction->field_count = field_count;
1605 container_init_fields_instruction->fields = fields;1601 container_init_fields_instruction->fields = fields;
1606 container_init_fields_instruction->result_loc = result_loc;1602 container_init_fields_instruction->result_loc = result_loc;
16071603
1608 ir_ref_instruction(container_type, irb->current_basic_block);
1609 for (size_t i = 0; i < field_count; i += 1) {1604 for (size_t i = 0; i < field_count; i += 1) {
1610 ir_ref_instruction(fields[i].result_loc, irb->current_basic_block);1605 ir_ref_instruction(fields[i].result_loc, irb->current_basic_block);
1611 }1606 }
...@@ -3084,7 +3079,7 @@ static IrInstruction *ir_build_resolve_result(IrBuilder *irb, Scope *scope, AstN...@@ -3084,7 +3079,7 @@ static IrInstruction *ir_build_resolve_result(IrBuilder *irb, Scope *scope, AstN
3084 instruction->result_loc = result_loc;3079 instruction->result_loc = result_loc;
3085 instruction->ty = ty;3080 instruction->ty = ty;
30863081
3087 ir_ref_instruction(ty, irb->current_basic_block);3082 if (ty != nullptr) ir_ref_instruction(ty, irb->current_basic_block);
30883083
3089 return &instruction->base;3084 return &instruction->base;
3090}3085}
...@@ -6127,28 +6122,42 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -6127,28 +6122,42 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
6127 AstNodeContainerInitExpr *container_init_expr = &node->data.container_init_expr;6122 AstNodeContainerInitExpr *container_init_expr = &node->data.container_init_expr;
6128 ContainerInitKind kind = container_init_expr->kind;6123 ContainerInitKind kind = container_init_expr->kind;
61296124
6130 IrInstruction *container_type = nullptr;6125 ResultLocCast *result_loc_cast = nullptr;
6131 IrInstruction *elem_type = nullptr;6126 ResultLoc *child_result_loc;
6132 if (container_init_expr->type->type == NodeTypeInferredArrayType) {6127 AstNode *init_array_type_source_node;
6133 elem_type = ir_gen_node(irb, container_init_expr->type->data.inferred_array_type.child_type, scope);6128 if (container_init_expr->type != nullptr) {
6134 if (elem_type == irb->codegen->invalid_instruction)6129 IrInstruction *container_type;
6135 return elem_type;6130 if (container_init_expr->type->type == NodeTypeInferredArrayType) {
6136 } else {6131 if (kind == ContainerInitKindStruct) {
6137 container_type = ir_gen_node(irb, container_init_expr->type, scope);
6138 if (container_type == irb->codegen->invalid_instruction)
6139 return container_type;
6140 }
6141
6142 switch (kind) {
6143 case ContainerInitKindStruct: {
6144 if (elem_type != nullptr) {
6145 add_node_error(irb->codegen, container_init_expr->type,6132 add_node_error(irb->codegen, container_init_expr->type,
6146 buf_sprintf("initializing array with struct syntax"));6133 buf_sprintf("initializing array with struct syntax"));
6147 return irb->codegen->invalid_instruction;6134 return irb->codegen->invalid_instruction;
6148 }6135 }
6136 IrInstruction *elem_type = ir_gen_node(irb,
6137 container_init_expr->type->data.inferred_array_type.child_type, scope);
6138 if (elem_type == irb->codegen->invalid_instruction)
6139 return elem_type;
6140 size_t item_count = container_init_expr->entries.length;
6141 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);
6142 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type);
6143 } else {
6144 container_type = ir_gen_node(irb, container_init_expr->type, scope);
6145 if (container_type == irb->codegen->invalid_instruction)
6146 return container_type;
6147 }
6148
6149 result_loc_cast = ir_build_cast_result_loc(irb, container_type, parent_result_loc);
6150 child_result_loc = &result_loc_cast->base;
6151 init_array_type_source_node = container_type->source_node;
6152 } else {
6153 child_result_loc = parent_result_loc;
6154 init_array_type_source_node = parent_result_loc->source_instruction->source_node;
6155 }
61496156
6150 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, parent_result_loc,6157 switch (kind) {
6151 container_type);6158 case ContainerInitKindStruct: {
6159 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, child_result_loc,
6160 nullptr);
61526161
6153 size_t field_count = container_init_expr->entries.length;6162 size_t field_count = container_init_expr->entries.length;
6154 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);6163 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);
...@@ -6176,29 +6185,27 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -6176,29 +6185,27 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
6176 fields[i].source_node = entry_node;6185 fields[i].source_node = entry_node;
6177 fields[i].result_loc = field_ptr;6186 fields[i].result_loc = field_ptr;
6178 }6187 }
6179 IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type,6188 IrInstruction *result = ir_build_container_init_fields(irb, scope, node, field_count,
6180 field_count, fields, container_ptr);6189 fields, container_ptr);
61816190
6182 return ir_lval_wrap(irb, scope, init_fields, lval, parent_result_loc);6191 if (result_loc_cast != nullptr) {
6192 result = ir_build_implicit_cast(irb, scope, node, result, result_loc_cast);
6193 }
6194 return ir_lval_wrap(irb, scope, result, lval, parent_result_loc);
6183 }6195 }
6184 case ContainerInitKindArray: {6196 case ContainerInitKindArray: {
6185 size_t item_count = container_init_expr->entries.length;6197 size_t item_count = container_init_expr->entries.length;
61866198
6187 if (container_type == nullptr) {6199 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, child_result_loc,
6188 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);6200 nullptr);
6189 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type);
6190 }
6191
6192 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, parent_result_loc,
6193 container_type);
61946201
6195 IrInstruction **result_locs = allocate<IrInstruction *>(item_count);6202 IrInstruction **result_locs = allocate<IrInstruction *>(item_count);
6196 for (size_t i = 0; i < item_count; i += 1) {6203 for (size_t i = 0; i < item_count; i += 1) {
6197 AstNode *expr_node = container_init_expr->entries.at(i);6204 AstNode *expr_node = container_init_expr->entries.at(i);
61986205
6199 IrInstruction *elem_index = ir_build_const_usize(irb, scope, expr_node, i);6206 IrInstruction *elem_index = ir_build_const_usize(irb, scope, expr_node, i);
6200 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, scope, expr_node, container_ptr, elem_index,6207 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, scope, expr_node, container_ptr,
6201 false, PtrLenSingle, container_type);6208 elem_index, false, PtrLenSingle, init_array_type_source_node);
6202 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);6209 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
6203 result_loc_inst->base.id = ResultLocIdInstruction;6210 result_loc_inst->base.id = ResultLocIdInstruction;
6204 result_loc_inst->base.source_instruction = elem_ptr;6211 result_loc_inst->base.source_instruction = elem_ptr;
...@@ -6213,9 +6220,12 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -6213,9 +6220,12 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
62136220
6214 result_locs[i] = elem_ptr;6221 result_locs[i] = elem_ptr;
6215 }6222 }
6216 IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type,6223 IrInstruction *result = ir_build_container_init_list(irb, scope, node, item_count,
6217 item_count, result_locs, container_ptr);6224 result_locs, container_ptr, init_array_type_source_node);
6218 return ir_lval_wrap(irb, scope, init_list, lval, parent_result_loc);6225 if (result_loc_cast != nullptr) {
6226 result = ir_build_implicit_cast(irb, scope, node, result, result_loc_cast);
6227 }
6228 return ir_lval_wrap(irb, scope, result, lval, parent_result_loc);
6219 }6229 }
6220 }6230 }
6221 zig_unreachable();6231 zig_unreachable();
...@@ -7935,14 +7945,14 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o...@@ -7935,14 +7945,14 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
7935static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,7945static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
7936 Scope *scope, AstNode *source_node, Buf *out_bare_name)7946 Scope *scope, AstNode *source_node, Buf *out_bare_name)
7937{7947{
7938 if (exec->name) {7948 if (exec != nullptr && exec->name) {
7939 ZigType *import = get_scope_import(scope);7949 ZigType *import = get_scope_import(scope);
7940 Buf *namespace_name = buf_alloc();7950 Buf *namespace_name = buf_alloc();
7941 append_namespace_qualification(codegen, namespace_name, import);7951 append_namespace_qualification(codegen, namespace_name, import);
7942 buf_append_buf(namespace_name, exec->name);7952 buf_append_buf(namespace_name, exec->name);
7943 buf_init_from_buf(out_bare_name, exec->name);7953 buf_init_from_buf(out_bare_name, exec->name);
7944 return namespace_name;7954 return namespace_name;
7945 } else if (exec->name_fn != nullptr) {7955 } else if (exec != nullptr && exec->name_fn != nullptr) {
7946 Buf *name = buf_alloc();7956 Buf *name = buf_alloc();
7947 buf_append_buf(name, &exec->name_fn->symbol_name);7957 buf_append_buf(name, &exec->name_fn->symbol_name);
7948 buf_appendf(name, "(");7958 buf_appendf(name, "(");
...@@ -15541,11 +15551,7 @@ static bool ir_result_has_type(ResultLoc *result_loc) {...@@ -15541,11 +15551,7 @@ static bool ir_result_has_type(ResultLoc *result_loc) {
15541static IrInstruction *ir_resolve_no_result_loc(IrAnalyze *ira, IrInstruction *suspend_source_instr,15551static IrInstruction *ir_resolve_no_result_loc(IrAnalyze *ira, IrInstruction *suspend_source_instr,
15542 ResultLoc *result_loc, ZigType *value_type, bool force_runtime, bool non_null_comptime)15552 ResultLoc *result_loc, ZigType *value_type, bool force_runtime, bool non_null_comptime)
15543{15553{
15544 Error err;
15545
15546 IrInstructionAllocaGen *alloca_gen = ir_build_alloca_gen(ira, suspend_source_instr, 0, "");15554 IrInstructionAllocaGen *alloca_gen = ir_build_alloca_gen(ira, suspend_source_instr, 0, "");
15547 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusZeroBitsKnown)))
15548 return ira->codegen->invalid_instruction;
15549 alloca_gen->base.value.type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,15555 alloca_gen->base.value.type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,
15550 PtrLenSingle, 0, 0, 0, false);15556 PtrLenSingle, 0, 0, 0, false);
15551 set_up_result_loc_for_inferred_comptime(&alloca_gen->base);15557 set_up_result_loc_for_inferred_comptime(&alloca_gen->base);
...@@ -15750,6 +15756,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15750,6 +15756,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15750 return casted_value;15756 return casted_value;
15751 }15757 }
1575215758
15759 bool old_parent_result_loc_written = result_cast->parent->written;
15753 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_cast->parent,15760 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_cast->parent,
15754 dest_type, casted_value, force_runtime, non_null_comptime, true);15761 dest_type, casted_value, force_runtime, non_null_comptime, true);
15755 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||15762 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
...@@ -15775,6 +15782,22 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15775,6 +15782,22 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15775 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,15782 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
15776 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);15783 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);
1577715784
15785 {
15786 // we also need to check that this cast is OK.
15787 ConstCastOnly const_cast_result = types_match_const_cast_only(ira,
15788 parent_result_loc->value.type, ptr_type,
15789 result_cast->base.source_instruction->source_node, false);
15790 if (const_cast_result.id == ConstCastResultIdInvalid)
15791 return ira->codegen->invalid_instruction;
15792 if (const_cast_result.id != ConstCastResultIdOk) {
15793 // We will not be able to provide a result location for this value. Create
15794 // a new result location.
15795 result_cast->parent->written = old_parent_result_loc_written;
15796 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,
15797 force_runtime, non_null_comptime);
15798 }
15799 }
15800
15778 result_loc->written = true;15801 result_loc->written = true;
15779 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,15802 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
15780 ptr_type, result_cast->base.source_instruction, false);15803 ptr_type, result_cast->base.source_instruction, false);
...@@ -15902,10 +15925,33 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s...@@ -15902,10 +15925,33 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
15902 return result_loc;15925 return result_loc;
15903}15926}
1590415927
15905static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstructionResolveResult *instruction) {15928static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira,
15906 ZigType *implicit_elem_type = ir_resolve_type(ira, instruction->ty->child);15929 IrInstructionResolveResult *instruction)
15907 if (type_is_invalid(implicit_elem_type))15930{
15908 return ira->codegen->invalid_instruction;15931 ZigType *implicit_elem_type;
15932 if (instruction->ty == nullptr) {
15933 if (instruction->result_loc->id == ResultLocIdCast) {
15934 implicit_elem_type = ir_resolve_type(ira,
15935 instruction->result_loc->source_instruction->child);
15936 if (type_is_invalid(implicit_elem_type))
15937 return ira->codegen->invalid_instruction;
15938 } else {
15939 Buf *bare_name = buf_alloc();
15940 Buf *name = get_anon_type_name(ira->codegen, nullptr, container_string(ContainerKindStruct),
15941 instruction->base.scope, instruction->base.source_node, bare_name);
15942
15943 ZigType *inferred_struct_type = get_partial_container_type(ira->codegen,
15944 instruction->base.scope, ContainerKindStruct, instruction->base.source_node,
15945 buf_ptr(name), bare_name, ContainerLayoutAuto);
15946 inferred_struct_type->data.structure.is_inferred = true;
15947 inferred_struct_type->data.structure.resolve_status = ResolveStatusBeingInferred;
15948 implicit_elem_type = inferred_struct_type;
15949 }
15950 } else {
15951 implicit_elem_type = ir_resolve_type(ira, instruction->ty->child);
15952 if (type_is_invalid(implicit_elem_type))
15953 return ira->codegen->invalid_instruction;
15954 }
15909 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,15955 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
15910 implicit_elem_type, nullptr, false, true, true);15956 implicit_elem_type, nullptr, false, true, true);
15911 if (result_loc != nullptr)15957 if (result_loc != nullptr)
...@@ -17837,7 +17883,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -17837,7 +17883,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
17837 if (array_ptr_val == nullptr)17883 if (array_ptr_val == nullptr)
17838 return ira->codegen->invalid_instruction;17884 return ira->codegen->invalid_instruction;
1783917885
17840 if (array_ptr_val->special == ConstValSpecialUndef && elem_ptr_instruction->init_array_type != nullptr) {17886 if (array_ptr_val->special == ConstValSpecialUndef &&
17887 elem_ptr_instruction->init_array_type_source_node != nullptr)
17888 {
17841 if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {17889 if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {
17842 array_ptr_val->data.x_array.special = ConstArraySpecialNone;17890 array_ptr_val->data.x_array.special = ConstArraySpecialNone;
17843 array_ptr_val->data.x_array.data.s_none.elements = create_const_vals(array_type->data.array.len);17891 array_ptr_val->data.x_array.data.s_none.elements = create_const_vals(array_type->data.array.len);
...@@ -17851,11 +17899,13 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -17851,11 +17899,13 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
17851 elem_val->parent.data.p_array.elem_index = i;17899 elem_val->parent.data.p_array.elem_index = i;
17852 }17900 }
17853 } else if (is_slice(array_type)) {17901 } else if (is_slice(array_type)) {
17854 ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);17902 ir_assert(array_ptr->value.type->id == ZigTypeIdPointer, &elem_ptr_instruction->base);
17903 ZigType *actual_array_type = array_ptr->value.type->data.pointer.child_type;
17904
17855 if (type_is_invalid(actual_array_type))17905 if (type_is_invalid(actual_array_type))
17856 return ira->codegen->invalid_instruction;17906 return ira->codegen->invalid_instruction;
17857 if (actual_array_type->id != ZigTypeIdArray) {17907 if (actual_array_type->id != ZigTypeIdArray) {
17858 ir_add_error(ira, elem_ptr_instruction->init_array_type,17908 ir_add_error_node(ira, elem_ptr_instruction->init_array_type_source_node,
17859 buf_sprintf("expected array type or [_], found slice"));17909 buf_sprintf("expected array type or [_], found slice"));
17860 return ira->codegen->invalid_instruction;17910 return ira->codegen->invalid_instruction;
17861 }17911 }
...@@ -17879,7 +17929,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -17879,7 +17929,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
17879 false);17929 false);
17880 array_ptr_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.mut = ConstPtrMutInfer;17930 array_ptr_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.mut = ConstPtrMutInfer;
17881 } else {17931 } else {
17882 ir_add_error(ira, elem_ptr_instruction->init_array_type,17932 ir_add_error_node(ira, elem_ptr_instruction->init_array_type_source_node,
17883 buf_sprintf("expected array type or [_], found '%s'",17933 buf_sprintf("expected array type or [_], found '%s'",
17884 buf_ptr(&array_type->name)));17934 buf_ptr(&array_type->name)));
17885 return ira->codegen->invalid_instruction;17935 return ira->codegen->invalid_instruction;
...@@ -18012,7 +18062,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -18012,7 +18062,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
18012 if (orig_array_ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {18062 if (orig_array_ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
18013 result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,18063 result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
18014 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index,18064 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index,
18015 false, elem_ptr_instruction->ptr_len, elem_ptr_instruction->init_array_type);18065 false, elem_ptr_instruction->ptr_len, nullptr);
18016 result->value.type = return_type;18066 result->value.type = return_type;
18017 result->value.special = ConstValSpecialStatic;18067 result->value.special = ConstValSpecialStatic;
18018 } else {18068 } else {
...@@ -18073,7 +18123,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -18073,7 +18123,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1807318123
18074 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,18124 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
18075 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, safety_check_on,18125 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, safety_check_on,
18076 elem_ptr_instruction->ptr_len, elem_ptr_instruction->init_array_type);18126 elem_ptr_instruction->ptr_len, nullptr);
18077 result->value.type = return_type;18127 result->value.type = return_type;
18078 return result;18128 return result;
18079}18129}
...@@ -18223,6 +18273,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -18223,6 +18273,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
18223 Error err;18273 Error err;
1822418274
18225 ZigType *bare_type = container_ref_type(container_type);18275 ZigType *bare_type = container_ref_type(container_type);
18276
18226 if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusZeroBitsKnown)))18277 if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusZeroBitsKnown)))
18227 return ira->codegen->invalid_instruction;18278 return ira->codegen->invalid_instruction;
1822818279
...@@ -20124,14 +20175,18 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc...@@ -20124,14 +20175,18 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
20124static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,20175static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
20125 IrInstructionContainerInitList *instruction)20176 IrInstructionContainerInitList *instruction)
20126{20177{
20127 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);20178 ir_assert(instruction->result_loc != nullptr, &instruction->base);
20128 if (type_is_invalid(container_type))20179 IrInstruction *result_loc = instruction->result_loc->child;
20129 return ira->codegen->invalid_instruction;20180 if (type_is_invalid(result_loc->value.type))
20181 return result_loc;
20182 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, &instruction->base);
20183
20184 ZigType *container_type = result_loc->value.type->data.pointer.child_type;
2013020185
20131 size_t elem_count = instruction->item_count;20186 size_t elem_count = instruction->item_count;
2013220187
20133 if (is_slice(container_type)) {20188 if (is_slice(container_type)) {
20134 ir_add_error(ira, instruction->container_type,20189 ir_add_error_node(ira, instruction->init_array_type_source_node,
20135 buf_sprintf("expected array type or [_], found slice"));20190 buf_sprintf("expected array type or [_], found slice"));
20136 return ira->codegen->invalid_instruction;20191 return ira->codegen->invalid_instruction;
20137 }20192 }
...@@ -20160,12 +20215,6 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -20160,12 +20215,6 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
20160 return ira->codegen->invalid_instruction;20215 return ira->codegen->invalid_instruction;
20161 }20216 }
2016220217
20163 ir_assert(instruction->result_loc != nullptr, &instruction->base);
20164 IrInstruction *result_loc = instruction->result_loc->child;
20165 if (type_is_invalid(result_loc->value.type))
20166 return result_loc;
20167 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, &instruction->base);
20168
20169 ZigType *child_type = container_type->data.array.child_type;20218 ZigType *child_type = container_type->data.array.child_type;
20170 if (container_type->data.array.len != elem_count) {20219 if (container_type->data.array.len != elem_count) {
20171 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count);20220 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count);
...@@ -20262,16 +20311,14 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -20262,16 +20311,14 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
20262static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ira,20311static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ira,
20263 IrInstructionContainerInitFields *instruction)20312 IrInstructionContainerInitFields *instruction)
20264{20313{
20265 IrInstruction *container_type_value = instruction->container_type->child;
20266 ZigType *container_type = ir_resolve_type(ira, container_type_value);
20267 if (type_is_invalid(container_type))
20268 return ira->codegen->invalid_instruction;
20269
20270 ir_assert(instruction->result_loc != nullptr, &instruction->base);20314 ir_assert(instruction->result_loc != nullptr, &instruction->base);
20271 IrInstruction *result_loc = instruction->result_loc->child;20315 IrInstruction *result_loc = instruction->result_loc->child;
20272 if (type_is_invalid(result_loc->value.type))20316 if (type_is_invalid(result_loc->value.type))
20273 return result_loc;20317 return result_loc;
2027420318
20319 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, &instruction->base);
20320 ZigType *container_type = result_loc->value.type->data.pointer.child_type;
20321
20275 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,20322 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
20276 instruction->field_count, instruction->fields, result_loc);20323 instruction->field_count, instruction->fields, result_loc);
20277}20324}
...@@ -24607,6 +24654,10 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_...@@ -24607,6 +24654,10 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
24607 ZigType *src_type = ptr->value.type;24654 ZigType *src_type = ptr->value.type;
24608 assert(!type_is_invalid(src_type));24655 assert(!type_is_invalid(src_type));
2460924656
24657 if (src_type == dest_type) {
24658 return ptr;
24659 }
24660
24610 // We have a check for zero bits later so we use get_src_ptr_type to24661 // We have a check for zero bits later so we use get_src_ptr_type to
24611 // validate src_type and dest_type.24662 // validate src_type and dest_type.
2461224663
...@@ -24656,6 +24707,9 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_...@@ -24656,6 +24707,9 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
24656 IrInstruction *result;24707 IrInstruction *result;
24657 if (ptr->value.data.x_ptr.mut == ConstPtrMutInfer) {24708 if (ptr->value.data.x_ptr.mut == ConstPtrMutInfer) {
24658 result = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);24709 result = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);
24710
24711 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
24712 return ira->codegen->invalid_instruction;
24659 } else {24713 } else {
24660 result = ir_const(ira, source_instr, dest_type);24714 result = ir_const(ira, source_instr, dest_type);
24661 }24715 }
src/ir_print.cpp+4-4
...@@ -731,7 +731,6 @@ static void ir_print_phi(IrPrint *irp, IrInstructionPhi *phi_instruction) {...@@ -731,7 +731,6 @@ static void ir_print_phi(IrPrint *irp, IrInstructionPhi *phi_instruction) {
731}731}
732732
733static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerInitList *instruction) {733static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerInitList *instruction) {
734 ir_print_other_instruction(irp, instruction->container_type);
735 fprintf(irp->f, "{");734 fprintf(irp->f, "{");
736 if (instruction->item_count > 50) {735 if (instruction->item_count > 50) {
737 fprintf(irp->f, "...(%" ZIG_PRI_usize " items)...", instruction->item_count);736 fprintf(irp->f, "...(%" ZIG_PRI_usize " items)...", instruction->item_count);
...@@ -743,11 +742,11 @@ static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerIni...@@ -743,11 +742,11 @@ static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerIni
743 ir_print_other_instruction(irp, result_loc);742 ir_print_other_instruction(irp, result_loc);
744 }743 }
745 }744 }
746 fprintf(irp->f, "}");745 fprintf(irp->f, "}result=");
746 ir_print_other_instruction(irp, instruction->result_loc);
747}747}
748748
749static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerInitFields *instruction) {749static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerInitFields *instruction) {
750 ir_print_other_instruction(irp, instruction->container_type);
751 fprintf(irp->f, "{");750 fprintf(irp->f, "{");
752 for (size_t i = 0; i < instruction->field_count; i += 1) {751 for (size_t i = 0; i < instruction->field_count; i += 1) {
753 IrInstructionContainerInitFieldsField *field = &instruction->fields[i];752 IrInstructionContainerInitFieldsField *field = &instruction->fields[i];
...@@ -755,7 +754,8 @@ static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerI...@@ -755,7 +754,8 @@ static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerI
755 fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field->name));754 fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field->name));
756 ir_print_other_instruction(irp, field->result_loc);755 ir_print_other_instruction(irp, field->result_loc);
757 }756 }
758 fprintf(irp->f, "} // container init");757 fprintf(irp->f, "}result=");
758 ir_print_other_instruction(irp, instruction->result_loc);
759}759}
760760
761static void ir_print_unreachable(IrPrint *irp, IrInstructionUnreachable *instruction) {761static void ir_print_unreachable(IrPrint *irp, IrInstructionUnreachable *instruction) {
test/compile_errors.zig+2-2
...@@ -216,9 +216,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -216,9 +216,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
216 \\ const obj = AstObject{ .lhsExpr = lhsExpr };216 \\ const obj = AstObject{ .lhsExpr = lhsExpr };
217 \\}217 \\}
218 ,218 ,
219 "tmp.zig:4:19: error: union 'AstObject' depends on itself",219 "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself",
220 "tmp.zig:2:5: note: while checking this field",
221 "tmp.zig:5:5: note: while checking this field",220 "tmp.zig:5:5: note: while checking this field",
221 "tmp.zig:2:5: note: while checking this field",
222 );222 );
223223
224 cases.add(224 cases.add(
test/stage1/behavior/async_fn.zig+2-2
...@@ -1214,7 +1214,7 @@ test "spill target expr in a for loop" {...@@ -1214,7 +1214,7 @@ test "spill target expr in a for loop" {
1214 }1214 }
12151215
1216 const Foo = struct {1216 const Foo = struct {
1217 slice: []i32,1217 slice: []const i32,
1218 };1218 };
12191219
1220 fn atest(foo: *Foo) i32 {1220 fn atest(foo: *Foo) i32 {
...@@ -1245,7 +1245,7 @@ test "spill target expr in a for loop, with a var decl in the loop body" {...@@ -1245,7 +1245,7 @@ test "spill target expr in a for loop, with a var decl in the loop body" {
1245 }1245 }
12461246
1247 const Foo = struct {1247 const Foo = struct {
1248 slice: []i32,1248 slice: []const i32,
1249 };1249 };
12501250
1251 fn atest(foo: *Foo) i32 {1251 fn atest(foo: *Foo) i32 {
test/stage1/behavior/struct.zig+20
...@@ -709,3 +709,23 @@ test "packed struct field passed to generic function" {...@@ -709,3 +709,23 @@ test "packed struct field passed to generic function" {
709 var loaded = S.genericReadPackedField(&p.b);709 var loaded = S.genericReadPackedField(&p.b);
710 expect(loaded == 29);710 expect(loaded == 29);
711}711}
712
713test "anonymous struct literal syntax" {
714 const S = struct {
715 const Point = struct {
716 x: i32,
717 y: i32,
718 };
719
720 fn doTheTest() void {
721 var p: Point = .{
722 .x = 1,
723 .y = 2,
724 };
725 expect(p.x == 1);
726 expect(p.y == 2);
727 }
728 };
729 S.doTheTest();
730 comptime S.doTheTest();
731}