authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-27 12:24:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-27 12:24:13-04:00
log1ccf6a2c9e7b42214be07185467e7ae7029a0aa5
tree68b55a9364267c7ee98f42a2f70f30eecb03fabf
parent6cd3995754adcdcb1873c3fbfc0ec98ba95b68b5
signaturelock-open Commit is signed but in an unrecognized format.

compile error for using slice as array init expr type

when there are more than 0 elements. closes #2764

2 files changed, 16 insertions(+), 3 deletions(-)

src/ir.cpp+6-2
......@@ -17280,7 +17280,11 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1728017280 ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);
1728117281 if (type_is_invalid(actual_array_type))
1728217282 return ira->codegen->invalid_instruction;
17283 assert(actual_array_type->id == ZigTypeIdArray);
17283 if (actual_array_type->id != ZigTypeIdArray) {
17284 ir_add_error(ira, elem_ptr_instruction->init_array_type,
17285 buf_sprintf("expected array type or [_], found slice"));
17286 return ira->codegen->invalid_instruction;
17287 }
1728417288
1728517289 ConstExprValue *array_init_val = create_const_vals(1);
1728617290 array_init_val->special = ConstValSpecialStatic;
......@@ -19575,7 +19579,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1957519579 size_t elem_count = instruction->item_count;
1957619580
1957719581 if (is_slice(container_type)) {
19578 ir_add_error(ira, &instruction->base,
19582 ir_add_error(ira, instruction->container_type,
1957919583 buf_sprintf("expected array type or [_], found slice"));
1958019584 return ira->codegen->invalid_instruction;
1958119585 }
test/compile_errors.zig+10-1
......@@ -2,13 +2,22 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "slice passed as array init type with elems",
7 \\export fn entry() void {
8 \\ const x = []u8{1, 2};
9 \\}
10 ,
11 "tmp.zig:2:15: error: expected array type or [_], found slice",
12 );
13
514 cases.add(
615 "slice passed as array init type",
716 \\export fn entry() void {
817 \\ const x = []u8{};
918 \\}
1019 ,
11 "tmp.zig:2:19: error: expected array type or [_], found slice",
20 "tmp.zig:2:15: error: expected array type or [_], found slice",
1221 );
1322
1423 cases.add(