authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-18 13:41:20+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-20 20:25:12+02:00
log835a1f7f0cfb6fdefdb7a6a3d3a6e04eafeb96d0
tree77b30ab8ff414dec7100b8ddab919993f5dfbfbd
parent034507ef7cc492e105ca90c42686aa52bc2097e3

Sema: fix missing error on mismatched array init count

Closes #13582

2 files changed, 41 insertions(+), 18 deletions(-)

src/Sema.zig+31-18
...@@ -4288,29 +4288,42 @@ fn zirValidateArrayInit(...@@ -4288,29 +4288,42 @@ fn zirValidateArrayInit(
4288 const array_ty = sema.typeOf(array_ptr).childType();4288 const array_ty = sema.typeOf(array_ptr).childType();
4289 const array_len = array_ty.arrayLen();4289 const array_len = array_ty.arrayLen();
42904290
4291 if (instrs.len != array_len and array_ty.isTuple()) {4291 if (instrs.len != array_len) switch (array_ty.zigTypeTag()) {
4292 const struct_obj = array_ty.castTag(.tuple).?.data;4292 .Struct => {
4293 var root_msg: ?*Module.ErrorMsg = null;4293 const struct_obj = array_ty.castTag(.tuple).?.data;
4294 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);4294 var root_msg: ?*Module.ErrorMsg = null;
4295 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
42954296
4296 for (struct_obj.values) |default_val, i| {4297 for (struct_obj.values) |default_val, i| {
4297 if (i < instrs.len) continue;4298 if (i < instrs.len) continue;
42984299
4299 if (default_val.tag() == .unreachable_value) {4300 if (default_val.tag() == .unreachable_value) {
4300 const template = "missing tuple field with index {d}";4301 const template = "missing tuple field with index {d}";
4301 if (root_msg) |msg| {4302 if (root_msg) |msg| {
4302 try sema.errNote(block, init_src, msg, template, .{i});4303 try sema.errNote(block, init_src, msg, template, .{i});
4303 } else {4304 } else {
4304 root_msg = try sema.errMsg(block, init_src, template, .{i});4305 root_msg = try sema.errMsg(block, init_src, template, .{i});
4306 }
4305 }4307 }
4306 }4308 }
4307 }
43084309
4309 if (root_msg) |msg| {4310 if (root_msg) |msg| {
4310 root_msg = null;4311 root_msg = null;
4311 return sema.failWithOwnedErrorMsg(msg);4312 return sema.failWithOwnedErrorMsg(msg);
4312 }4313 }
4313 }4314 },
4315 .Array => {
4316 return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{
4317 array_len, instrs.len,
4318 });
4319 },
4320 .Vector => {
4321 return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{
4322 array_len, instrs.len,
4323 });
4324 },
4325 else => unreachable,
4326 };
43144327
4315 if ((is_comptime or block.is_comptime) and4328 if ((is_comptime or block.is_comptime) and
4316 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)4329 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
test/cases/compile_errors/array_init_invalid_elem_count.zig+10
...@@ -16,6 +16,14 @@ comptime {...@@ -16,6 +16,14 @@ comptime {
16 var a: A = A{};16 var a: A = A{};
17 _ = a;17 _ = a;
18}18}
19pub export fn entry1() void {
20 var bla: V = .{ 1, 2, 3, 4 };
21 _ = bla;
22}
23pub export fn entry2() void {
24 var bla: A = .{ 1, 2, 3, 4 };
25 _ = bla;
26}
1927
20// error28// error
21// backend=stage229// backend=stage2
...@@ -25,3 +33,5 @@ comptime {...@@ -25,3 +33,5 @@ comptime {
25// :8:17: error: expected 8 vector elements; found 033// :8:17: error: expected 8 vector elements; found 0
26// :12:17: error: expected 8 array elements; found 134// :12:17: error: expected 8 array elements; found 1
27// :16:17: error: expected 8 array elements; found 035// :16:17: error: expected 8 array elements; found 0
36// :20:19: error: expected 8 vector elements; found 4
37// :24:19: error: expected 8 array elements; found 4