authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-04-04 18:42:07+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-04-05 14:45:56+03:00
log66520c8342193827f703274aabfff6ad8656aee1
tree6bd4322474760cb853d612ee200989d3a65ec0fd
parent82a6acca93683fa7272976879f338a0e66e3f82b

Sema: validate array element types

Fixes the compiler crash part of #15175

2 files changed, 23 insertions(+), 0 deletions(-)

src/Sema.zig+10
......@@ -7819,6 +7819,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
78197819 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
78207820 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, "array length must be comptime-known");
78217821 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
7822 try sema.validateArrayElemType(block, elem_type, elem_src);
78227823 const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
78237824
78247825 return sema.addType(array_ty);
......@@ -7835,6 +7836,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
78357836 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
78367837 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, "array length must be comptime-known");
78377838 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
7839 try sema.validateArrayElemType(block, elem_type, elem_src);
78387840 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
78397841 const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
78407842 const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel, "array sentinel value must be comptime-known");
......@@ -7843,6 +7845,14 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
78437845 return sema.addType(array_ty);
78447846}
78457847
7848fn validateArrayElemType(sema: *Sema, block: *Block, elem_type: Type, elem_src: LazySrcLoc) !void {
7849 if (elem_type.zigTypeTag() == .Opaque) {
7850 return sema.fail(block, elem_src, "array of opaque type '{}' not allowed", .{elem_type.fmt(sema.mod)});
7851 } else if (elem_type.zigTypeTag() == .NoReturn) {
7852 return sema.fail(block, elem_src, "array of 'noreturn' not allowed", .{});
7853 }
7854}
7855
78467856fn zirAnyframeType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
78477857 const tracy = trace(@src());
78487858 defer tracy.end();
test/cases/compile_errors/invalid_array_elem_type.zig created+13
......@@ -0,0 +1,13 @@
1comptime {
2 _ = [1]anyopaque;
3}
4comptime {
5 _ = [1]noreturn;
6}
7
8// error
9// backend=stage2
10// target=native
11//
12// :2:12: error: array of opaque type 'anyopaque' not allowed
13// :5:12: error: array of 'noreturn' not allowed