authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-22 14:45:44+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-22 17:03:42+03:00
log8a488fcdb8982201bca91ce2f0c6316d3e471186
tree72e30edbb56e3de2a51930a93ff5710866ff1546
parent47de73980e9dbbf85fee754b579273a9b7b57c53

Sema: validate empty array init


2 files changed, 47 insertions(+), 6 deletions(-)

src/Sema.zig+20-6
...@@ -3875,9 +3875,15 @@ fn zirValidateArrayInit(...@@ -3875,9 +3875,15 @@ fn zirValidateArrayInit(
3875 const array_len = array_ty.arrayLen();3875 const array_len = array_ty.arrayLen();
38763876
3877 if (instrs.len != array_len) {3877 if (instrs.len != array_len) {
3878 return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{3878 if (array_ty.zigTypeTag() == .Array) {
3879 array_len, instrs.len,3879 return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{
3880 });3880 array_len, instrs.len,
3881 });
3882 } else {
3883 return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{
3884 array_len, instrs.len,
3885 });
3886 }
3881 }3887 }
38823888
3883 if ((is_comptime or block.is_comptime) and3889 if ((is_comptime or block.is_comptime) and
...@@ -14265,7 +14271,7 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE...@@ -14265,7 +14271,7 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1426514271
14266 switch (obj_ty.zigTypeTag()) {14272 switch (obj_ty.zigTypeTag()) {
14267 .Struct => return sema.structInitEmpty(block, obj_ty, src, src),14273 .Struct => return sema.structInitEmpty(block, obj_ty, src, src),
14268 .Array => return arrayInitEmpty(sema, obj_ty),14274 .Array, .Vector => return sema.arrayInitEmpty(block, src, obj_ty),
14269 .Void => return sema.addConstant(obj_ty, Value.void),14275 .Void => return sema.addConstant(obj_ty, Value.void),
14270 else => return sema.failWithArrayInitNotSupported(block, src, obj_ty),14276 else => return sema.failWithArrayInitNotSupported(block, src, obj_ty),
14271 }14277 }
...@@ -14290,7 +14296,15 @@ fn structInitEmpty(...@@ -14290,7 +14296,15 @@ fn structInitEmpty(
14290 return sema.finishStructInit(block, init_src, dest_src, field_inits, struct_ty, false);14296 return sema.finishStructInit(block, init_src, dest_src, field_inits, struct_ty, false);
14291}14297}
1429214298
14293fn arrayInitEmpty(sema: *Sema, obj_ty: Type) CompileError!Air.Inst.Ref {14299fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) CompileError!Air.Inst.Ref {
14300 const arr_len = obj_ty.arrayLen();
14301 if (arr_len != 0) {
14302 if (obj_ty.zigTypeTag() == .Array) {
14303 return sema.fail(block, src, "expected {d} array elements; found 0", .{arr_len});
14304 } else {
14305 return sema.fail(block, src, "expected {d} vector elements; found 0", .{arr_len});
14306 }
14307 }
14294 if (obj_ty.sentinel()) |sentinel| {14308 if (obj_ty.sentinel()) |sentinel| {
14295 const val = try Value.Tag.empty_array_sentinel.create(sema.arena, sentinel);14309 const val = try Value.Tag.empty_array_sentinel.create(sema.arena, sentinel);
14296 return sema.addConstant(obj_ty, val);14310 return sema.addConstant(obj_ty, val);
...@@ -20978,7 +20992,7 @@ fn coerceExtra(...@@ -20978,7 +20992,7 @@ fn coerceExtra(
20978 .Vector => return sema.coerceArrayLike(block, dest_ty, dest_ty_src, inst, inst_src),20992 .Vector => return sema.coerceArrayLike(block, dest_ty, dest_ty_src, inst, inst_src),
20979 .Struct => {20993 .Struct => {
20980 if (inst == .empty_struct) {20994 if (inst == .empty_struct) {
20981 return arrayInitEmpty(sema, dest_ty);20995 return sema.arrayInitEmpty(block, inst_src, dest_ty);
20982 }20996 }
20983 if (inst_ty.isTuple()) {20997 if (inst_ty.isTuple()) {
20984 return sema.coerceTupleToArray(block, dest_ty, dest_ty_src, inst, inst_src);20998 return sema.coerceTupleToArray(block, dest_ty, dest_ty_src, inst, inst_src);
test/cases/compile_errors/array_init_invalid_elem_count.zig created+27
...@@ -0,0 +1,27 @@
1const V = @Vector(8, u8);
2const A = [8]u8;
3comptime {
4 var v: V = V{1};
5 _ = v;
6}
7comptime {
8 var v: V = V{};
9 _ = v;
10}
11comptime {
12 var a: A = A{1};
13 _ = a;
14}
15comptime {
16 var a: A = A{};
17 _ = a;
18}
19
20// error
21// backend=stage2
22// target=native
23//
24// :4:17: error: expected 8 vector elements; found 1
25// :8:17: error: expected 8 vector elements; found 0
26// :12:17: error: expected 8 array elements; found 1
27// :16:17: error: expected 8 array elements; found 0