Commit 1ccf6a2c9e

Andrew Kelley <andrew@ziglang.org>
2019-06-27 18:24:13
compile error for using slice as array init expr type
when there are more than 0 elements. closes #2764
1 parent 6cd3995
Changed files (2)
src/ir.cpp
@@ -17280,7 +17280,11 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
                     ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);
                     if (type_is_invalid(actual_array_type))
                         return ira->codegen->invalid_instruction;
-                    assert(actual_array_type->id == ZigTypeIdArray);
+                    if (actual_array_type->id != ZigTypeIdArray) {
+                        ir_add_error(ira, elem_ptr_instruction->init_array_type,
+                            buf_sprintf("expected array type or [_], found slice"));
+                        return ira->codegen->invalid_instruction;
+                    }
 
                     ConstExprValue *array_init_val = create_const_vals(1);
                     array_init_val->special = ConstValSpecialStatic;
@@ -19575,7 +19579,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
     size_t elem_count = instruction->item_count;
 
     if (is_slice(container_type)) {
-        ir_add_error(ira, &instruction->base,
+        ir_add_error(ira, instruction->container_type,
             buf_sprintf("expected array type or [_], found slice"));
         return ira->codegen->invalid_instruction;
     }
test/compile_errors.zig
@@ -2,13 +2,22 @@ const tests = @import("tests.zig");
 const builtin = @import("builtin");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "slice passed as array init type with elems",
+        \\export fn entry() void {
+        \\    const x = []u8{1, 2};
+        \\}
+    ,
+        "tmp.zig:2:15: error: expected array type or [_], found slice",
+    );
+
     cases.add(
         "slice passed as array init type",
         \\export fn entry() void {
         \\    const x = []u8{};
         \\}
     ,
-        "tmp.zig:2:19: error: expected array type or [_], found slice",
+        "tmp.zig:2:15: error: expected array type or [_], found slice",
     );
 
     cases.add(