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...@@ -17280,7 +17280,11 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
17280 ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);17280 ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);
17281 if (type_is_invalid(actual_array_type))17281 if (type_is_invalid(actual_array_type))
17282 return ira->codegen->invalid_instruction;17282 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
17285 ConstExprValue *array_init_val = create_const_vals(1);17289 ConstExprValue *array_init_val = create_const_vals(1);
17286 array_init_val->special = ConstValSpecialStatic;17290 array_init_val->special = ConstValSpecialStatic;
...@@ -19575,7 +19579,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -19575,7 +19579,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
19575 size_t elem_count = instruction->item_count;19579 size_t elem_count = instruction->item_count;
1957619580
19577 if (is_slice(container_type)) {19581 if (is_slice(container_type)) {
19578 ir_add_error(ira, &instruction->base,19582 ir_add_error(ira, instruction->container_type,
19579 buf_sprintf("expected array type or [_], found slice"));19583 buf_sprintf("expected array type or [_], found slice"));
19580 return ira->codegen->invalid_instruction;19584 return ira->codegen->invalid_instruction;
19581 }19585 }
test/compile_errors.zig+10-1
...@@ -2,13 +2,22 @@ const tests = @import("tests.zig");...@@ -2,13 +2,22 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.add(14 cases.add(
6 "slice passed as array init type",15 "slice passed as array init type",
7 \\export fn entry() void {16 \\export fn entry() void {
8 \\ const x = []u8{};17 \\ const x = []u8{};
9 \\}18 \\}
10 ,19 ,
11 "tmp.zig:2:19: error: expected array type or [_], found slice",20 "tmp.zig:2:15: error: expected array type or [_], found slice",
12 );21 );
1322
14 cases.add(23 cases.add(