authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-08-09 18:47:55-07:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-08-13 13:55:57+01:00
log8e02f9f70df97891aabfce615a1ef43434bc5c1f
tree4fae570ff474dbeb7cdafa3fd1840d3e71de3df7
parent6e90ce25364b02555a3ca46013f85b2e80e98705

sema: strip `@splat` operand result type before checking it


5 files changed, 33 insertions(+), 14 deletions(-)

lib/std/zig/AstGen.zig+2-3
......@@ -2697,7 +2697,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
26972697 .array_type_sentinel,
26982698 .elem_type,
26992699 .indexable_ptr_elem_type,
2700 .vec_arr_elem_type,
2700 .splat_op_result_ty,
27012701 .vector_type,
27022702 .indexable_ptr_len,
27032703 .anyframe_type,
......@@ -9526,10 +9526,9 @@ fn builtinCall(
95269526 });
95279527 return rvalue(gz, ri, result, node);
95289528 },
9529
95309529 .splat => {
95319530 const result_type = try ri.rl.resultTypeForCast(gz, node, builtin_name);
9532 const elem_type = try gz.addUnNode(.vec_arr_elem_type, result_type, node);
9531 const elem_type = try gz.addUnNode(.splat_op_result_ty, result_type, node);
95339532 const scalar = try expr(gz, scope, .{ .rl = .{ .ty = elem_type } }, params[0]);
95349533 const result = try gz.addPlNode(.splat, node, Zir.Inst.Bin{
95359534 .lhs = result_type,
lib/std/zig/Zir.zig+10-6
......@@ -273,9 +273,13 @@ pub const Inst = struct {
273273 /// element type. Emits a compile error if the type is not an indexable pointer.
274274 /// Uses the `un_node` field.
275275 indexable_ptr_elem_type,
276 /// Given a vector or array type, returns its element type.
276 /// Given a vector or array type, strips off any error unions or
277 /// optionals layered on top and returns its element type.
278 ///
279 /// `!?[N]T` -> `T`
280 ///
277281 /// Uses the `un_node` field.
278 vec_arr_elem_type,
282 splat_op_result_ty,
279283 /// Given a pointer to an indexable object, returns the len property. This is
280284 /// used by for loops. This instruction also emits a for-loop specific compile
281285 /// error if the indexable object is not indexable.
......@@ -1098,7 +1102,7 @@ pub const Inst = struct {
10981102 .vector_type,
10991103 .elem_type,
11001104 .indexable_ptr_elem_type,
1101 .vec_arr_elem_type,
1105 .splat_op_result_ty,
11021106 .indexable_ptr_len,
11031107 .anyframe_type,
11041108 .as_node,
......@@ -1395,7 +1399,7 @@ pub const Inst = struct {
13951399 .vector_type,
13961400 .elem_type,
13971401 .indexable_ptr_elem_type,
1398 .vec_arr_elem_type,
1402 .splat_op_result_ty,
13991403 .indexable_ptr_len,
14001404 .anyframe_type,
14011405 .as_node,
......@@ -1630,7 +1634,7 @@ pub const Inst = struct {
16301634 .vector_type = .pl_node,
16311635 .elem_type = .un_node,
16321636 .indexable_ptr_elem_type = .un_node,
1633 .vec_arr_elem_type = .un_node,
1637 .splat_op_result_ty = .un_node,
16341638 .indexable_ptr_len = .un_node,
16351639 .anyframe_type = .un_node,
16361640 .as_node = .pl_node,
......@@ -4173,7 +4177,7 @@ fn findTrackableInner(
41734177 .vector_type,
41744178 .elem_type,
41754179 .indexable_ptr_elem_type,
4176 .vec_arr_elem_type,
4180 .splat_op_result_ty,
41774181 .indexable_ptr_len,
41784182 .anyframe_type,
41794183 .as_node,
src/Sema.zig+7-4
......@@ -1197,7 +1197,7 @@ fn analyzeBodyInner(
11971197 .elem_val_imm => try sema.zirElemValImm(block, inst),
11981198 .elem_type => try sema.zirElemType(block, inst),
11991199 .indexable_ptr_elem_type => try sema.zirIndexablePtrElemType(block, inst),
1200 .vec_arr_elem_type => try sema.zirVecArrElemType(block, inst),
1200 .splat_op_result_ty => try sema.zirSplatOpResultType(block, inst),
12011201 .enum_literal => try sema.zirEnumLiteral(block, inst),
12021202 .decl_literal => try sema.zirDeclLiteral(block, inst, true),
12031203 .decl_literal_no_coerce => try sema.zirDeclLiteral(block, inst, false),
......@@ -2139,7 +2139,7 @@ fn genericPoisonReason(sema: *Sema, block: *Block, ref: Zir.Inst.Ref) GenericPoi
21392139 const bin = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;
21402140 cur = bin.lhs;
21412141 },
2142 .indexable_ptr_elem_type, .vec_arr_elem_type => {
2142 .indexable_ptr_elem_type, .splat_op_result_ty => {
21432143 const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
21442144 cur = un_node.operand;
21452145 },
......@@ -7945,11 +7945,14 @@ fn zirIndexablePtrElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
79457945 return Air.internedToRef(elem_ty.toIntern());
79467946}
79477947
7948fn zirVecArrElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7948fn zirSplatOpResultType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
79497949 const pt = sema.pt;
79507950 const zcu = pt.zcu;
79517951 const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
7952 const vec_ty = try sema.resolveTypeOrPoison(block, LazySrcLoc.unneeded, un_node.operand) orelse return .generic_poison_type;
7952
7953 const raw_ty = try sema.resolveTypeOrPoison(block, LazySrcLoc.unneeded, un_node.operand) orelse return .generic_poison_type;
7954 const vec_ty = raw_ty.optEuBaseType(zcu);
7955
79537956 switch (vec_ty.zigTypeTag(zcu)) {
79547957 .array, .vector => {},
79557958 else => return sema.fail(block, block.nodeOffset(un_node.src_node), "expected array or vector type, found '{f}'", .{vec_ty.fmt(pt)}),
src/print_zir.zig+1-1
......@@ -192,7 +192,7 @@ const Writer = struct {
192192 .alloc_comptime_mut,
193193 .elem_type,
194194 .indexable_ptr_elem_type,
195 .vec_arr_elem_type,
195 .splat_op_result_ty,
196196 .indexable_ptr_len,
197197 .anyframe_type,
198198 .bit_not,
test/behavior/array.zig+13
......@@ -1112,3 +1112,16 @@ test "sentinel of runtime-known array initialization is populated" {
11121112 try expect(elems[0] == 42);
11131113 try expect(elems[1] == 123);
11141114}
1115
1116test "splat with an error union or optional result type" {
1117 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1118
1119 const S = struct {
1120 fn doTest(T: type) !?T {
1121 return @splat(1);
1122 }
1123 };
1124
1125 _ = try S.doTest(@Vector(4, u32));
1126 _ = try S.doTest([4]u32);
1127}