From 8a488fcdb8982201bca91ce2f0c6316d3e471186 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 22 Jul 2022 14:45:44 +0300 Subject: [PATCH] Sema: validate empty array init --- src/Sema.zig | 26 +++++++++++++----- .../array_init_invalid_elem_count.zig | 27 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 test/cases/compile_errors/array_init_invalid_elem_count.zig diff --git a/src/Sema.zig b/src/Sema.zig index 18a09b826b..a5b1a18eaa 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -3875,9 +3875,15 @@ fn zirValidateArrayInit( const array_len = array_ty.arrayLen(); if (instrs.len != array_len) { - return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{ - array_len, instrs.len, - }); + if (array_ty.zigTypeTag() == .Array) { + return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{ + array_len, instrs.len, + }); + } else { + return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{ + array_len, instrs.len, + }); + } } if ((is_comptime or block.is_comptime) and @@ -14265,7 +14271,7 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE switch (obj_ty.zigTypeTag()) { .Struct => return sema.structInitEmpty(block, obj_ty, src, src), - .Array => return arrayInitEmpty(sema, obj_ty), + .Array, .Vector => return sema.arrayInitEmpty(block, src, obj_ty), .Void => return sema.addConstant(obj_ty, Value.void), else => return sema.failWithArrayInitNotSupported(block, src, obj_ty), } @@ -14290,7 +14296,15 @@ fn structInitEmpty( return sema.finishStructInit(block, init_src, dest_src, field_inits, struct_ty, false); } -fn arrayInitEmpty(sema: *Sema, obj_ty: Type) CompileError!Air.Inst.Ref { +fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) CompileError!Air.Inst.Ref { + const arr_len = obj_ty.arrayLen(); + if (arr_len != 0) { + if (obj_ty.zigTypeTag() == .Array) { + return sema.fail(block, src, "expected {d} array elements; found 0", .{arr_len}); + } else { + return sema.fail(block, src, "expected {d} vector elements; found 0", .{arr_len}); + } + } if (obj_ty.sentinel()) |sentinel| { const val = try Value.Tag.empty_array_sentinel.create(sema.arena, sentinel); return sema.addConstant(obj_ty, val); @@ -20978,7 +20992,7 @@ fn coerceExtra( .Vector => return sema.coerceArrayLike(block, dest_ty, dest_ty_src, inst, inst_src), .Struct => { if (inst == .empty_struct) { - return arrayInitEmpty(sema, dest_ty); + return sema.arrayInitEmpty(block, inst_src, dest_ty); } if (inst_ty.isTuple()) { return sema.coerceTupleToArray(block, dest_ty, dest_ty_src, inst, inst_src); diff --git a/test/cases/compile_errors/array_init_invalid_elem_count.zig b/test/cases/compile_errors/array_init_invalid_elem_count.zig new file mode 100644 index 0000000000..d8df149d18 --- /dev/null +++ b/test/cases/compile_errors/array_init_invalid_elem_count.zig @@ -0,0 +1,27 @@ +const V = @Vector(8, u8); +const A = [8]u8; +comptime { + var v: V = V{1}; + _ = v; +} +comptime { + var v: V = V{}; + _ = v; +} +comptime { + var a: A = A{1}; + _ = a; +} +comptime { + var a: A = A{}; + _ = a; +} + +// error +// backend=stage2 +// target=native +// +// :4:17: error: expected 8 vector elements; found 1 +// :8:17: error: expected 8 vector elements; found 0 +// :12:17: error: expected 8 array elements; found 1 +// :16:17: error: expected 8 array elements; found 0