authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-28 14:58:20+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-29 10:12:36+03:00
logfdaf9c40d6a351477aacb1af27871f3de12d485e
tree1060d38a06a0889cd89eed6bd0522d487772d37c
parent9e0a930ce3be01923602adbfee13b50842da08b7

stage2: handle tuple init edge cases


7 files changed, 192 insertions(+), 25 deletions(-)

src/AstGen.zig+4-1
......@@ -1349,7 +1349,10 @@ fn arrayInitExpr(
13491349 }
13501350 }
13511351 const array_type_inst = try typeExpr(gz, scope, array_init.ast.type_expr);
1352 _ = try gz.addUnNode(.validate_array_init_ty, array_type_inst, array_init.ast.type_expr);
1352 _ = try gz.addPlNode(.validate_array_init_ty, node, Zir.Inst.ArrayInit{
1353 .ty = array_type_inst,
1354 .init_count = @intCast(u32, array_init.ast.elements.len),
1355 });
13531356 break :inst .{
13541357 .array = array_type_inst,
13551358 .elem = .none,
src/Module.zig+19
......@@ -2728,6 +2728,21 @@ pub const SrcLoc = struct {
27282728 };
27292729 return nodeToSpan(tree, full.ast.value_expr);
27302730 },
2731 .node_offset_init_ty => |node_off| {
2732 const tree = try src_loc.file_scope.getTree(gpa);
2733 const node_tags = tree.nodes.items(.tag);
2734 const parent_node = src_loc.declRelativeToNodeIndex(node_off);
2735
2736 var buf: [2]Ast.Node.Index = undefined;
2737 const full: Ast.full.ArrayInit = switch (node_tags[parent_node]) {
2738 .array_init_one, .array_init_one_comma => tree.arrayInitOne(buf[0..1], parent_node),
2739 .array_init_dot_two, .array_init_dot_two_comma => tree.arrayInitDotTwo(&buf, parent_node),
2740 .array_init_dot, .array_init_dot_comma => tree.arrayInitDot(parent_node),
2741 .array_init, .array_init_comma => tree.arrayInit(parent_node),
2742 else => unreachable,
2743 };
2744 return nodeToSpan(tree, full.ast.type_expr);
2745 },
27312746 }
27322747 }
27332748
......@@ -3048,6 +3063,9 @@ pub const LazySrcLoc = union(enum) {
30483063 /// The source location points to the default value of a field.
30493064 /// The Decl is determined contextually.
30503065 node_offset_field_default: i32,
3066 /// The source location points to the type of an array or struct initializer.
3067 /// The Decl is determined contextually.
3068 node_offset_init_ty: i32,
30513069
30523070 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
30533071
......@@ -3126,6 +3144,7 @@ pub const LazySrcLoc = union(enum) {
31263144 .node_offset_ptr_hostsize,
31273145 .node_offset_container_tag,
31283146 .node_offset_field_default,
3147 .node_offset_init_ty,
31293148 => .{
31303149 .file_scope = decl.getFileScope(),
31313150 .parent_decl_node = decl.src_node,
src/Sema.zig+105-21
......@@ -3493,19 +3493,43 @@ fn validateArrayInitTy(
34933493 block: *Block,
34943494 inst: Zir.Inst.Index,
34953495) CompileError!void {
3496 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
3496 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
34973497 const src = inst_data.src();
3498 const ty = try sema.resolveType(block, src, inst_data.operand);
3498 const ty_src: LazySrcLoc = .{ .node_offset_init_ty = inst_data.src_node };
3499 const extra = sema.code.extraData(Zir.Inst.ArrayInit, inst_data.payload_index).data;
3500 const ty = try sema.resolveType(block, ty_src, extra.ty);
34993501
35003502 switch (ty.zigTypeTag()) {
3501 .Array, .Vector => return,
3503 .Array => {
3504 const array_len = ty.arrayLen();
3505 if (extra.init_count != array_len) {
3506 return sema.fail(block, src, "expected {d} array elements; found {d}", .{
3507 array_len, extra.init_count,
3508 });
3509 }
3510 return;
3511 },
3512 .Vector => {
3513 const array_len = ty.arrayLen();
3514 if (extra.init_count != array_len) {
3515 return sema.fail(block, src, "expected {d} vector elements; found {d}", .{
3516 array_len, extra.init_count,
3517 });
3518 }
3519 return;
3520 },
35023521 .Struct => if (ty.isTuple()) {
3503 // TODO validate element count
3522 const array_len = ty.arrayLen();
3523 if (extra.init_count > array_len) {
3524 return sema.fail(block, src, "expected at most {d} tuple fields; found {d}", .{
3525 array_len, extra.init_count,
3526 });
3527 }
35043528 return;
35053529 },
35063530 else => {},
35073531 }
3508 return sema.failWithArrayInitNotSupported(block, src, ty);
3532 return sema.failWithArrayInitNotSupported(block, ty_src, ty);
35093533}
35103534
35113535fn validateStructInitTy(
......@@ -3741,6 +3765,15 @@ fn validateStructInit(
37413765
37423766 const default_val = struct_ty.structFieldDefaultValue(i);
37433767 if (default_val.tag() == .unreachable_value) {
3768 if (struct_ty.isTuple()) {
3769 const template = "missing tuple field with index {d}";
3770 if (root_msg) |msg| {
3771 try sema.errNote(block, init_src, msg, template, .{i});
3772 } else {
3773 root_msg = try sema.errMsg(block, init_src, template, .{i});
3774 }
3775 continue;
3776 }
37443777 const field_name = struct_ty.structFieldName(i);
37453778 const template = "missing struct field: {s}";
37463779 const args = .{field_name};
......@@ -3753,7 +3786,10 @@ fn validateStructInit(
37533786 }
37543787
37553788 const field_src = init_src; // TODO better source location
3756 const default_field_ptr = try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(u32, i), field_src, struct_ty, true);
3789 const default_field_ptr = if (struct_ty.isTuple())
3790 try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @intCast(u32, i), true)
3791 else
3792 try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(u32, i), field_src, struct_ty, true);
37573793 const field_ty = sema.typeOf(default_field_ptr).childType();
37583794 const init = try sema.addConstant(field_ty, default_val);
37593795 try sema.storePtr2(block, init_src, default_field_ptr, init_src, init, field_src, .store);
......@@ -3868,6 +3904,15 @@ fn validateStructInit(
38683904
38693905 const default_val = struct_ty.structFieldDefaultValue(i);
38703906 if (default_val.tag() == .unreachable_value) {
3907 if (struct_ty.isTuple()) {
3908 const template = "missing tuple field with index {d}";
3909 if (root_msg) |msg| {
3910 try sema.errNote(block, init_src, msg, template, .{i});
3911 } else {
3912 root_msg = try sema.errMsg(block, init_src, template, .{i});
3913 }
3914 continue;
3915 }
38713916 const field_name = struct_ty.structFieldName(i);
38723917 const template = "missing struct field: {s}";
38733918 const args = .{field_name};
......@@ -3911,7 +3956,10 @@ fn validateStructInit(
39113956 if (field_ptr != 0) continue;
39123957
39133958 const field_src = init_src; // TODO better source location
3914 const default_field_ptr = try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(u32, i), field_src, struct_ty, true);
3959 const default_field_ptr = if (struct_ty.isTuple())
3960 try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @intCast(u32, i), true)
3961 else
3962 try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(u32, i), field_src, struct_ty, true);
39153963 const field_ty = sema.typeOf(default_field_ptr).childType();
39163964 const init = try sema.addConstant(field_ty, field_values[i]);
39173965 try sema.storePtr2(block, init_src, default_field_ptr, init_src, init, field_src, .store);
......@@ -3934,15 +3982,24 @@ fn zirValidateArrayInit(
39343982 const array_ty = sema.typeOf(array_ptr).childType();
39353983 const array_len = array_ty.arrayLen();
39363984
3937 if (instrs.len != array_len) {
3938 if (array_ty.zigTypeTag() == .Array) {
3939 return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{
3940 array_len, instrs.len,
3941 });
3942 } else {
3943 return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{
3944 array_len, instrs.len,
3945 });
3985 if (instrs.len != array_len and array_ty.isTuple()) {
3986 const struct_obj = array_ty.castTag(.tuple).?.data;
3987 var root_msg: ?*Module.ErrorMsg = null;
3988 for (struct_obj.values) |default_val, i| {
3989 if (i < instrs.len) continue;
3990
3991 if (default_val.tag() == .unreachable_value) {
3992 const template = "missing tuple field with index {d}";
3993 if (root_msg) |msg| {
3994 try sema.errNote(block, init_src, msg, template, .{i});
3995 } else {
3996 root_msg = try sema.errMsg(block, init_src, template, .{i});
3997 }
3998 }
3999 }
4000
4001 if (root_msg) |msg| {
4002 return sema.failWithOwnedErrorMsg(block, msg);
39464003 }
39474004 }
39484005
......@@ -3995,10 +4052,17 @@ fn zirValidateArrayInit(
39954052 }
39964053 first_block_index = @minimum(first_block_index, block_index);
39974054
3998 // Array has one possible value, so value is always comptime-known
3999 if (opt_opv) |opv| {
4000 element_vals[i] = opv;
4001 continue;
4055 if (array_ty.isTuple()) {
4056 if (array_ty.structFieldValueComptime(i)) |opv| {
4057 element_vals[i] = opv;
4058 continue;
4059 }
4060 } else {
4061 // Array has one possible value, so value is always comptime-known
4062 if (opt_opv) |opv| {
4063 element_vals[i] = opv;
4064 continue;
4065 }
40024066 }
40034067
40044068 // If the next instructon is a store with a comptime operand, this element
......@@ -14710,6 +14774,22 @@ fn finishStructInit(
1471014774 field_inits[i] = try sema.addConstant(struct_obj.types[i], default_val);
1471114775 }
1471214776 }
14777 } else if (struct_ty.isTuple()) {
14778 const struct_obj = struct_ty.castTag(.tuple).?.data;
14779 for (struct_obj.values) |default_val, i| {
14780 if (field_inits[i] != .none) continue;
14781
14782 if (default_val.tag() == .unreachable_value) {
14783 const template = "missing tuple field with index {d}";
14784 if (root_msg) |msg| {
14785 try sema.errNote(block, init_src, msg, template, .{i});
14786 } else {
14787 root_msg = try sema.errMsg(block, init_src, template, .{i});
14788 }
14789 } else {
14790 field_inits[i] = try sema.addConstant(struct_obj.types[i], default_val);
14791 }
14792 }
1471314793 } else {
1471414794 const struct_obj = struct_ty.castTag(.@"struct").?.data;
1471514795 for (struct_obj.fields.values()) |field, i| {
......@@ -20255,7 +20335,7 @@ fn tupleFieldVal(
2025520335 return tupleFieldValByIndex(sema, block, src, tuple_byval, field_index, tuple_ty);
2025620336}
2025720337
20258/// Don't forget to check for "len" before calling this.
20338/// Asserts that `field_name` is not "len".
2025920339fn tupleFieldIndex(
2026020340 sema: *Sema,
2026120341 block: *Block,
......@@ -20263,8 +20343,12 @@ fn tupleFieldIndex(
2026320343 field_name: []const u8,
2026420344 field_name_src: LazySrcLoc,
2026520345) CompileError!u32 {
20346 assert(!std.mem.eql(u8, field_name, "len"));
2026620347 if (std.fmt.parseUnsigned(u32, field_name, 10)) |field_index| {
2026720348 if (field_index < tuple_ty.structFieldCount()) return field_index;
20349 return sema.fail(block, field_name_src, "index '{s}' out of bounds of tuple '{}'", .{
20350 field_name, tuple_ty.fmt(sema.mod),
20351 });
2026820352 } else |_| {}
2026920353
2027020354 return sema.fail(block, field_name_src, "no field named '{s}' in tuple '{}'", .{
src/Zir.zig+6-1
......@@ -1709,7 +1709,7 @@ pub const Inst = struct {
17091709 .switch_capture_multi_ref = .switch_capture,
17101710 .array_base_ptr = .un_node,
17111711 .field_base_ptr = .un_node,
1712 .validate_array_init_ty = .un_node,
1712 .validate_array_init_ty = .pl_node,
17131713 .validate_struct_init_ty = .un_node,
17141714 .validate_struct_init = .pl_node,
17151715 .validate_struct_init_comptime = .pl_node,
......@@ -3543,6 +3543,11 @@ pub const Inst = struct {
35433543 line: u32,
35443544 column: u32,
35453545 };
3546
3547 pub const ArrayInit = struct {
3548 ty: Ref,
3549 init_count: u32,
3550 };
35463551};
35473552
35483553pub const SpecialProng = enum { none, @"else", under };
src/print_zir.zig+13-1
......@@ -229,7 +229,6 @@ const Writer = struct {
229229 .switch_cond_ref,
230230 .array_base_ptr,
231231 .field_base_ptr,
232 .validate_array_init_ty,
233232 .validate_struct_init_ty,
234233 .make_ptr_const,
235234 .validate_deref,
......@@ -246,6 +245,7 @@ const Writer = struct {
246245 .bool_br_or,
247246 => try self.writeBoolBr(stream, inst),
248247
248 .validate_array_init_ty => try self.writeValidateArrayInitTy(stream, inst),
249249 .array_type_sentinel => try self.writeArrayTypeSentinel(stream, inst),
250250 .param_type => try self.writeParamType(stream, inst),
251251 .ptr_type => try self.writePtrType(stream, inst),
......@@ -577,6 +577,18 @@ const Writer = struct {
577577 try self.writeSrc(stream, inst_data.src());
578578 }
579579
580 fn writeValidateArrayInitTy(
581 self: *Writer,
582 stream: anytype,
583 inst: Zir.Inst.Index,
584 ) (@TypeOf(stream).Error || error{OutOfMemory})!void {
585 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
586 const extra = self.code.extraData(Zir.Inst.ArrayInit, inst_data.payload_index).data;
587 try self.writeInstRef(stream, extra.ty);
588 try stream.print(", {d}) ", .{extra.init_count});
589 try self.writeSrc(stream, inst_data.src());
590 }
591
580592 fn writeArrayTypeSentinel(
581593 self: *Writer,
582594 stream: anytype,
test/cases/compile_errors/tuple_init_edge_cases.zig created+44
......@@ -0,0 +1,44 @@
1pub export fn entry1() void {
2 const T = @TypeOf(.{ 123, 3 });
3 var b = T{ .@"1" = 3 }; _ = b;
4 var c = T{ 123, 3 }; _ = c;
5 var d = T{}; _ = d;
6}
7pub export fn entry2() void {
8 var a: u32 = 2;
9 const T = @TypeOf(.{ 123, a });
10 var b = T{ .@"1" = 3 }; _ = b;
11 var c = T{ 123, 3 }; _ = c;
12 var d = T{}; _ = d;
13}
14pub export fn entry3() void {
15 var a: u32 = 2;
16 const T = @TypeOf(.{ 123, a });
17 var b = T{ .@"0" = 123 }; _ = b;
18}
19comptime {
20 var a: u32 = 2;
21 const T = @TypeOf(.{ 123, a });
22 var b = T{ .@"0" = 123 }; _ = b;
23 var c = T{ 123, 2 }; _ = c;
24 var d = T{}; _ = d;
25}
26pub export fn entry4() void {
27 var a: u32 = 2;
28 const T = @TypeOf(.{ 123, a });
29 var b = T{ 123, 4, 5 }; _ = b;
30}
31pub export fn entry5() void {
32 var a: u32 = 2;
33 const T = @TypeOf(.{ 123, a });
34 var b = T{ .@"0" = 123, .@"2" = 123, .@"1" = 123 }; _ = b;
35}
36
37// error
38// backend=stage2
39// target=native
40//
41// :12:14: error: missing tuple field with index 1
42// :17:14: error: missing tuple field with index 1
43// :29:14: error: expected at most 2 tuple fields; found 3
44// :34:30: error: index '2' out of bounds of tuple 'tuple{comptime comptime_int = 123, u32}'
test/cases/compile_errors/wrong_size_to_an_array_literal.zig+1-1
......@@ -7,4 +7,4 @@ comptime {
77// backend=stage2
88// target=native
99//
10// :2:31: error: index 2 outside array of length 2
10// :2:24: error: expected 2 array elements; found 3