Commit 66520c8342

Veikka Tuominen <git@vexu.eu>
2023-04-04 17:42:07
Sema: validate array element types
Fixes the compiler crash part of #15175
1 parent 82a6acc
Changed files (2)
src
test
cases
src/Sema.zig
@@ -7819,6 +7819,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
     const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
     const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, "array length must be comptime-known");
     const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
+    try sema.validateArrayElemType(block, elem_type, elem_src);
     const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
 
     return sema.addType(array_ty);
@@ -7835,6 +7836,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
     const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
     const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, "array length must be comptime-known");
     const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
+    try sema.validateArrayElemType(block, elem_type, elem_src);
     const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
     const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
     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
     return sema.addType(array_ty);
 }
 
+fn validateArrayElemType(sema: *Sema, block: *Block, elem_type: Type, elem_src: LazySrcLoc) !void {
+    if (elem_type.zigTypeTag() == .Opaque) {
+        return sema.fail(block, elem_src, "array of opaque type '{}' not allowed", .{elem_type.fmt(sema.mod)});
+    } else if (elem_type.zigTypeTag() == .NoReturn) {
+        return sema.fail(block, elem_src, "array of 'noreturn' not allowed", .{});
+    }
+}
+
 fn zirAnyframeType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
     const tracy = trace(@src());
     defer tracy.end();
test/cases/compile_errors/invalid_array_elem_type.zig
@@ -0,0 +1,13 @@
+comptime {
+    _ = [1]anyopaque;
+}
+comptime {
+    _ = [1]noreturn;
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:12: error: array of opaque type 'anyopaque' not allowed
+// :5:12: error: array of 'noreturn' not allowed