authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-13 13:31:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-13 13:31:15-04:00
logefb064449f4db63c1e841f6f0e434f26fcd487ab
tree44073f801cd144bfbc4291550d79927977cf1713
parentca0988e1d01bd121100590fb97f8cd9dde15b7d8
signaturelock-open Commit is signed but in an unrecognized format.

fix runtime initialize array elem and then implicit cast to slice


4 files changed, 141 insertions(+), 123 deletions(-)

src/all_types.hpp+3-1
......@@ -2555,8 +2555,8 @@ struct IrInstructionElemPtr {
25552555 IrInstruction *array_ptr;
25562556 IrInstruction *elem_index;
25572557 PtrLen ptr_len;
2558 bool is_const;
25592558 bool safety_check_on;
2559 bool initializing;
25602560};
25612561
25622562struct IrInstructionVarPtr {
......@@ -2651,6 +2651,7 @@ struct IrInstructionContainerInitList {
26512651 IrInstruction *elem_type;
26522652 size_t item_count;
26532653 IrInstruction **items;
2654 ResultLoc *result_loc;
26542655};
26552656
26562657struct IrInstructionContainerInitFieldsField {
......@@ -2666,6 +2667,7 @@ struct IrInstructionContainerInitFields {
26662667 IrInstruction *container_type;
26672668 size_t field_count;
26682669 IrInstructionContainerInitFieldsField *fields;
2670 ResultLoc *result_loc;
26692671};
26702672
26712673struct IrInstructionUnreachable {
src/ir.cpp+37-27
......@@ -1298,14 +1298,16 @@ static IrInstruction *ir_build_return_ptr(IrAnalyze *ira, IrInstruction *source_
12981298 return &instruction->base;
12991299}
13001300
1301static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_ptr,
1302 IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len)
1301static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1302 IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len,
1303 bool initializing)
13031304{
13041305 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node);
13051306 instruction->array_ptr = array_ptr;
13061307 instruction->elem_index = elem_index;
13071308 instruction->safety_check_on = safety_check_on;
13081309 instruction->ptr_len = ptr_len;
1310 instruction->initializing = initializing;
13091311
13101312 ir_ref_instruction(array_ptr, irb->current_basic_block);
13111313 ir_ref_instruction(elem_index, irb->current_basic_block);
......@@ -1505,13 +1507,14 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
15051507}
15061508
15071509static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
1508 IrInstruction *container_type, size_t item_count, IrInstruction **items)
1510 IrInstruction *container_type, size_t item_count, IrInstruction **items, ResultLoc *result_loc)
15091511{
15101512 IrInstructionContainerInitList *container_init_list_instruction =
15111513 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
15121514 container_init_list_instruction->container_type = container_type;
15131515 container_init_list_instruction->item_count = item_count;
15141516 container_init_list_instruction->items = items;
1517 container_init_list_instruction->result_loc = result_loc;
15151518
15161519 ir_ref_instruction(container_type, irb->current_basic_block);
15171520 for (size_t i = 0; i < item_count; i += 1) {
......@@ -1522,13 +1525,15 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope,
15221525}
15231526
15241527static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node,
1525 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields)
1528 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields,
1529 ResultLoc *result_loc)
15261530{
15271531 IrInstructionContainerInitFields *container_init_fields_instruction =
15281532 ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node);
15291533 container_init_fields_instruction->container_type = container_type;
15301534 container_init_fields_instruction->field_count = field_count;
15311535 container_init_fields_instruction->fields = fields;
1536 container_init_fields_instruction->result_loc = result_loc;
15321537
15331538 ir_ref_instruction(container_type, irb->current_basic_block);
15341539 for (size_t i = 0; i < field_count; i += 1) {
......@@ -4202,7 +4207,7 @@ static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode
42024207 return subscript_instruction;
42034208
42044209 IrInstruction *ptr_instruction = ir_build_elem_ptr(irb, scope, node, array_ref_instruction,
4205 subscript_instruction, true, PtrLenSingle);
4210 subscript_instruction, true, PtrLenSingle, false);
42064211 if (lval == LValPtr)
42074212 return ptr_instruction;
42084213
......@@ -5734,7 +5739,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
57345739 fields[i].value = expr_value;
57355740 fields[i].source_node = entry_node;
57365741 }
5737 IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type, field_count, fields);
5742 IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type,
5743 field_count, fields, result_loc);
57385744
57395745 return ir_lval_wrap(irb, scope, init_fields, lval, result_loc);
57405746 }
......@@ -5764,7 +5770,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
57645770 if (container_ptr != nullptr) {
57655771 IrInstruction *elem_index = ir_build_const_usize(irb, &result_loc->scope_elide->base, expr_node, i);
57665772 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, &result_loc->scope_elide->base, expr_node,
5767 container_ptr, elem_index, false, PtrLenSingle);
5773 container_ptr, elem_index, false, PtrLenSingle, true);
57685774 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
57695775 result_loc_inst->base.id = ResultLocIdInstruction;
57705776 result_loc_inst->base.source_instruction = elem_ptr;
......@@ -5781,7 +5787,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
57815787 values[i] = expr_value;
57825788 }
57835789 IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type,
5784 item_count, values);
5790 item_count, values, result_loc);
57855791 return ir_lval_wrap(irb, scope, init_list, lval, result_loc);
57865792 }
57875793 }
......@@ -6264,7 +6270,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
62646270 is_comptime);
62656271
62666272 ir_set_cursor_at_end_and_append_block(irb, body_block);
6267 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
6273 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false,
6274 PtrLenSingle, false);
62686275 // TODO make it an error to write to element variable or i variable.
62696276 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
62706277 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
......@@ -16825,8 +16832,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1682516832 } else if (is_slice(array_type)) {
1682616833 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];
1682716834 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
16828 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node,
16829 array_ptr, casted_elem_index, false, elem_ptr_instruction->ptr_len);
16835 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
16836 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,
16837 elem_ptr_instruction->ptr_len, true);
1683016838 result->value.type = return_type;
1683116839 return result;
1683216840 }
......@@ -16917,8 +16925,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1691716925 }
1691816926 }
1691916927
16920 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node,
16921 array_ptr, casted_elem_index, safety_check_on, elem_ptr_instruction->ptr_len);
16928 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
16929 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, safety_check_on,
16930 elem_ptr_instruction->ptr_len, elem_ptr_instruction->initializing);
1692216931 result->value.type = return_type;
1692316932 return result;
1692416933}
......@@ -18784,7 +18793,8 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
1878418793}
1878518794
1878618795static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction,
18787 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields)
18796 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields,
18797 ResultLoc *result_loc_pass1)
1878818798{
1878918799 Error err;
1879018800 if (container_type->id == ZigTypeIdUnion) {
......@@ -18919,11 +18929,11 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1891918929 return ira->codegen->invalid_instruction;
1892018930 }
1892118931
18922 // this instruction should not get to codegen
18923 IrInstruction *result = ir_const(ira, instruction, container_type);
18924 // this is how we signal to EndExpr the value is not comptime known
18925 result->value.special = ConstValSpecialRuntime;
18926 return result;
18932 IrInstruction *result_loc = ir_resolve_result(ira, instruction, result_loc_pass1,
18933 container_type, nullptr);
18934 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))
18935 return result_loc;
18936 return ir_get_deref(ira, instruction, result_loc, nullptr);
1892718937}
1892818938
1892918939static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
......@@ -18942,8 +18952,8 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1894218952 buf_sprintf("expected array type or [_], found slice"));
1894318953 return ira->codegen->invalid_instruction;
1894418954 } else if (container_type->id == ZigTypeIdStruct && !is_slice(container_type) && elem_count == 0) {
18945 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
18946 0, nullptr);
18955 return ir_analyze_container_init_fields(ira, &instruction->base, container_type, 0, nullptr,
18956 instruction->result_loc);
1894718957 } else if (container_type->id == ZigTypeIdArray) {
1894818958 // array is same as slice init but we make a compile error if the length is wrong
1894918959 ZigType *child_type;
......@@ -19029,11 +19039,11 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1902919039 return ira->codegen->invalid_instruction;
1903019040 }
1903119041
19032 // this instruction should not get to codegen
19033 IrInstruction *new_instruction = ir_const(ira, &instruction->base, fixed_size_array_type);
19034 // this is how we signal to EndExpr the value is not comptime known
19035 new_instruction->value.special = ConstValSpecialRuntime;
19036 return new_instruction;
19042 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
19043 fixed_size_array_type, nullptr);
19044 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))
19045 return result_loc;
19046 return ir_get_deref(ira, &instruction->base, result_loc, nullptr);
1903719047 } else if (container_type->id == ZigTypeIdVoid) {
1903819048 if (elem_count != 0) {
1903919049 ir_add_error_node(ira, instruction->base.source_node,
......@@ -19058,7 +19068,7 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir
1905819068 return ira->codegen->invalid_instruction;
1905919069
1906019070 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
19061 instruction->field_count, instruction->fields);
19071 instruction->field_count, instruction->fields, instruction->result_loc);
1906219072}
1906319073
1906419074static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
test/stage1/behavior.zig+1-1
......@@ -1,7 +1,7 @@
11comptime {
22 _ = @import("behavior/align.zig");
33 _ = @import("behavior/alignof.zig");
4 //_ = @import("behavior/array.zig");
4 _ = @import("behavior/array.zig");
55 _ = @import("behavior/asm.zig");
66 //_ = @import("behavior/atomics.zig");
77 _ = @import("behavior/bit_shifting.zig");
test/stage1/behavior/array.zig+100-94
......@@ -172,99 +172,105 @@ fn plusOne(x: u32) u32 {
172172 return x + 1;
173173}
174174
175test "array literal as argument to function" {
176 const S = struct {
177 fn entry(two: i32) void {
178 foo([_]i32{
179 1,
180 2,
181 3,
182 });
183 foo([_]i32{
184 1,
185 two,
186 3,
187 });
188 foo2(true, [_]i32{
189 1,
190 2,
191 3,
192 });
193 foo2(true, [_]i32{
194 1,
195 two,
196 3,
197 });
198 }
199 fn foo(x: []const i32) void {
200 expect(x[0] == 1);
201 expect(x[1] == 2);
202 expect(x[2] == 3);
203 }
204 fn foo2(trash: bool, x: []const i32) void {
205 expect(trash);
206 expect(x[0] == 1);
207 expect(x[1] == 2);
208 expect(x[2] == 3);
209 }
210 };
211 S.entry(2);
212 comptime S.entry(2);
175test "runtime initialize array elem and then implicit cast to slice" {
176 var two: i32 = 2;
177 const x: []const i32 = [_]i32{two};
178 expect(x[0] == 2);
213179}
214180
215test "double nested array to const slice cast in array literal" {
216 const S = struct {
217 fn entry(two: i32) void {
218 const cases = [_][]const []const i32{
219 [_][]const i32{[_]i32{1}},
220 [_][]const i32{[_]i32{ 2, 3 }},
221 [_][]const i32{
222 [_]i32{4},
223 [_]i32{ 5, 6, 7 },
224 },
225 };
226 check(cases);
227
228 const cases2 = [_][]const i32{
229 [_]i32{1},
230 [_]i32{ two, 3 },
231 };
232 expect(cases2.len == 2);
233 expect(cases2[0].len == 1);
234 expect(cases2[0][0] == 1);
235 expect(cases2[1].len == 2);
236 expect(cases2[1][0] == 2);
237 expect(cases2[1][1] == 3);
238
239 const cases3 = [_][]const []const i32{
240 [_][]const i32{[_]i32{1}},
241 [_][]const i32{[_]i32{ two, 3 }},
242 [_][]const i32{
243 [_]i32{4},
244 [_]i32{ 5, 6, 7 },
245 },
246 };
247 check(cases3);
248 }
249
250 fn check(cases: []const []const []const i32) void {
251 expect(cases.len == 3);
252 expect(cases[0].len == 1);
253 expect(cases[0][0].len == 1);
254 expect(cases[0][0][0] == 1);
255 expect(cases[1].len == 1);
256 expect(cases[1][0].len == 2);
257 expect(cases[1][0][0] == 2);
258 expect(cases[1][0][1] == 3);
259 expect(cases[2].len == 2);
260 expect(cases[2][0].len == 1);
261 expect(cases[2][0][0] == 4);
262 expect(cases[2][1].len == 3);
263 expect(cases[2][1][0] == 5);
264 expect(cases[2][1][1] == 6);
265 expect(cases[2][1][2] == 7);
266 }
267 };
268 S.entry(2);
269 comptime S.entry(2);
270}
181//test "array literal as argument to function" {
182// const S = struct {
183// fn entry(two: i32) void {
184// foo([_]i32{
185// 1,
186// 2,
187// 3,
188// });
189// foo([_]i32{
190// 1,
191// two,
192// 3,
193// });
194// foo2(true, [_]i32{
195// 1,
196// 2,
197// 3,
198// });
199// foo2(true, [_]i32{
200// 1,
201// two,
202// 3,
203// });
204// }
205// fn foo(x: []const i32) void {
206// expect(x[0] == 1);
207// expect(x[1] == 2);
208// expect(x[2] == 3);
209// }
210// fn foo2(trash: bool, x: []const i32) void {
211// expect(trash);
212// expect(x[0] == 1);
213// expect(x[1] == 2);
214// expect(x[2] == 3);
215// }
216// };
217// S.entry(2);
218// comptime S.entry(2);
219//}
220
221//test "double nested array to const slice cast in array literal" {
222// const S = struct {
223// fn entry(two: i32) void {
224// const cases = [_][]const []const i32{
225// [_][]const i32{[_]i32{1}},
226// [_][]const i32{[_]i32{ 2, 3 }},
227// [_][]const i32{
228// [_]i32{4},
229// [_]i32{ 5, 6, 7 },
230// },
231// };
232// check(cases);
233//
234// const cases2 = [_][]const i32{
235// [_]i32{1},
236// [_]i32{ two, 3 },
237// };
238// expect(cases2.len == 2);
239// expect(cases2[0].len == 1);
240// expect(cases2[0][0] == 1);
241// expect(cases2[1].len == 2);
242// expect(cases2[1][0] == 2);
243// expect(cases2[1][1] == 3);
244//
245// const cases3 = [_][]const []const i32{
246// [_][]const i32{[_]i32{1}},
247// [_][]const i32{[_]i32{ two, 3 }},
248// [_][]const i32{
249// [_]i32{4},
250// [_]i32{ 5, 6, 7 },
251// },
252// };
253// check(cases3);
254// }
255//
256// fn check(cases: []const []const []const i32) void {
257// expect(cases.len == 3);
258// expect(cases[0].len == 1);
259// expect(cases[0][0].len == 1);
260// expect(cases[0][0][0] == 1);
261// expect(cases[1].len == 1);
262// expect(cases[1][0].len == 2);
263// expect(cases[1][0][0] == 2);
264// expect(cases[1][0][1] == 3);
265// expect(cases[2].len == 2);
266// expect(cases[2][0].len == 1);
267// expect(cases[2][0][0] == 4);
268// expect(cases[2][1].len == 3);
269// expect(cases[2][1][0] == 5);
270// expect(cases[2][1][1] == 6);
271// expect(cases[2][1][2] == 7);
272// }
273// };
274// S.entry(2);
275// comptime S.entry(2);
276//}