Commit 835a1f7f0c

Veikka Tuominen <git@vexu.eu>
2022-11-18 12:41:20
Sema: fix missing error on mismatched array init count
Closes #13582
1 parent 034507e
Changed files (2)
src
test
src/Sema.zig
@@ -4288,29 +4288,42 @@ fn zirValidateArrayInit(
     const array_ty = sema.typeOf(array_ptr).childType();
     const array_len = array_ty.arrayLen();
 
-    if (instrs.len != array_len and array_ty.isTuple()) {
-        const struct_obj = array_ty.castTag(.tuple).?.data;
-        var root_msg: ?*Module.ErrorMsg = null;
-        errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
+    if (instrs.len != array_len) switch (array_ty.zigTypeTag()) {
+        .Struct => {
+            const struct_obj = array_ty.castTag(.tuple).?.data;
+            var root_msg: ?*Module.ErrorMsg = null;
+            errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
 
-        for (struct_obj.values) |default_val, i| {
-            if (i < instrs.len) continue;
+            for (struct_obj.values) |default_val, i| {
+                if (i < instrs.len) continue;
 
-            if (default_val.tag() == .unreachable_value) {
-                const template = "missing tuple field with index {d}";
-                if (root_msg) |msg| {
-                    try sema.errNote(block, init_src, msg, template, .{i});
-                } else {
-                    root_msg = try sema.errMsg(block, init_src, template, .{i});
+                if (default_val.tag() == .unreachable_value) {
+                    const template = "missing tuple field with index {d}";
+                    if (root_msg) |msg| {
+                        try sema.errNote(block, init_src, msg, template, .{i});
+                    } else {
+                        root_msg = try sema.errMsg(block, init_src, template, .{i});
+                    }
                 }
             }
-        }
 
-        if (root_msg) |msg| {
-            root_msg = null;
-            return sema.failWithOwnedErrorMsg(msg);
-        }
-    }
+            if (root_msg) |msg| {
+                root_msg = null;
+                return sema.failWithOwnedErrorMsg(msg);
+            }
+        },
+        .Array => {
+            return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{
+                array_len, instrs.len,
+            });
+        },
+        .Vector => {
+            return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{
+                array_len, instrs.len,
+            });
+        },
+        else => unreachable,
+    };
 
     if ((is_comptime or block.is_comptime) and
         (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
test/cases/compile_errors/array_init_invalid_elem_count.zig
@@ -16,6 +16,14 @@ comptime {
     var a: A = A{};
     _ = a;
 }
+pub export fn entry1() void {
+    var bla: V = .{ 1, 2, 3, 4 };
+    _ = bla;
+}
+pub export fn entry2() void {
+    var bla: A = .{ 1, 2, 3, 4 };
+    _ = bla;
+}
 
 // error
 // backend=stage2
@@ -25,3 +33,5 @@ comptime {
 // :8:17: error: expected 8 vector elements; found 0
 // :12:17: error: expected 8 array elements; found 1
 // :16:17: error: expected 8 array elements; found 0
+// :20:19: error: expected 8 vector elements; found 4
+// :24:19: error: expected 8 array elements; found 4