Skip to content

Commit 1ccf6a2

Browse files
committed
compile error for using slice as array init expr type
when there are more than 0 elements. closes #2764
1 parent 6cd3995 commit 1ccf6a2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/ir.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub 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(

0 commit comments

Comments
 (0)