authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-07 13:54:09-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-07 13:54:09-05:00
logf59cbd89e349fb3002500cb2a1d69ca5e6063338
treefd60d586a95f7d23cce8ee671e36eee7a14ede68
parent8c32d989c995f8675f1824fb084245b833b26223
parent85b0a4a8fd8a56c07e0377b02d33753fe205fe41
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11077 from mitchellh/array-init-ty

stage2: sentinel-terminated array initialization

5 files changed, 113 insertions(+), 19 deletions(-)

src/AstGen.zig+39-8
......@@ -1259,10 +1259,12 @@ fn arrayInitExpr(
12591259 const types: struct {
12601260 array: Zir.Inst.Ref,
12611261 elem: Zir.Inst.Ref,
1262 sentinel: Zir.Inst.Ref,
12621263 } = inst: {
12631264 if (array_init.ast.type_expr == 0) break :inst .{
12641265 .array = .none,
12651266 .elem = .none,
1267 .sentinel = .none,
12661268 };
12671269
12681270 infer: {
......@@ -1282,6 +1284,7 @@ fn arrayInitExpr(
12821284 break :inst .{
12831285 .array = array_type_inst,
12841286 .elem = elem_type,
1287 .sentinel = .none,
12851288 };
12861289 } else {
12871290 const sentinel = try comptimeExpr(gz, scope, .{ .ty = elem_type }, array_type.ast.sentinel);
......@@ -1297,6 +1300,7 @@ fn arrayInitExpr(
12971300 break :inst .{
12981301 .array = array_type_inst,
12991302 .elem = elem_type,
1303 .sentinel = sentinel,
13001304 };
13011305 }
13021306 }
......@@ -1307,6 +1311,7 @@ fn arrayInitExpr(
13071311 break :inst .{
13081312 .array = array_type_inst,
13091313 .elem = elem_type,
1314 .sentinel = .none,
13101315 };
13111316 };
13121317
......@@ -1319,25 +1324,25 @@ fn arrayInitExpr(
13191324 },
13201325 .ref => {
13211326 if (types.array != .none) {
1322 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, .array_init_ref);
1327 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, types.sentinel, true);
13231328 } else {
13241329 return arrayInitExprRlNone(gz, scope, node, array_init.ast.elements, .array_init_anon_ref);
13251330 }
13261331 },
13271332 .none => {
13281333 if (types.array != .none) {
1329 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, .array_init);
1334 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, types.sentinel, false);
13301335 } else {
13311336 return arrayInitExprRlNone(gz, scope, node, array_init.ast.elements, .array_init_anon);
13321337 }
13331338 },
13341339 .ty, .coerced_ty => |ty_inst| {
13351340 if (types.array != .none) {
1336 const result = try arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, .array_init);
1341 const result = try arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, types.elem, types.sentinel, false);
13371342 return rvalue(gz, rl, result, node);
13381343 } else {
13391344 const elem_type = try gz.addUnNode(.elem_type, ty_inst, node);
1340 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, elem_type, .array_init);
1345 return arrayInitExprRlTy(gz, scope, node, array_init.ast.elements, elem_type, types.sentinel, false);
13411346 }
13421347 },
13431348 .ptr => |ptr_inst| {
......@@ -1387,14 +1392,32 @@ fn arrayInitExprRlTy(
13871392 node: Ast.Node.Index,
13881393 elements: []const Ast.Node.Index,
13891394 elem_ty_inst: Zir.Inst.Ref,
1390 tag: Zir.Inst.Tag,
1395 sentinel: Zir.Inst.Ref,
1396 ref: bool,
13911397) InnerError!Zir.Inst.Ref {
13921398 const astgen = gz.astgen;
13931399
1400 const info: struct {
1401 len: usize,
1402 tag: Zir.Inst.Tag,
1403 } = blk: {
1404 if (sentinel != .none) {
1405 break :blk .{
1406 .len = elements.len + 1,
1407 .tag = if (ref) .array_init_sent_ref else .array_init_sent,
1408 };
1409 } else {
1410 break :blk .{
1411 .len = elements.len,
1412 .tag = if (ref) .array_init_ref else .array_init,
1413 };
1414 }
1415 };
1416
13941417 const payload_index = try addExtra(astgen, Zir.Inst.MultiOp{
1395 .operands_len = @intCast(u32, elements.len),
1418 .operands_len = @intCast(u32, info.len),
13961419 });
1397 var extra_index = try reserveExtra(astgen, elements.len);
1420 var extra_index = try reserveExtra(astgen, info.len);
13981421
13991422 const elem_rl: ResultLoc = .{ .ty = elem_ty_inst };
14001423 for (elements) |elem_init| {
......@@ -1402,7 +1425,13 @@ fn arrayInitExprRlTy(
14021425 astgen.extra.items[extra_index] = @enumToInt(elem_ref);
14031426 extra_index += 1;
14041427 }
1405 return try gz.addPlNodePayloadIndex(tag, node, payload_index);
1428
1429 if (sentinel != .none) {
1430 astgen.extra.items[extra_index] = @enumToInt(sentinel);
1431 extra_index += 1;
1432 }
1433
1434 return try gz.addPlNodePayloadIndex(info.tag, node, payload_index);
14061435}
14071436
14081437fn arrayInitExprRlPtr(
......@@ -2217,8 +2246,10 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
22172246 .struct_init_anon_ref,
22182247 .array_init,
22192248 .array_init_anon,
2249 .array_init_sent,
22202250 .array_init_ref,
22212251 .array_init_anon_ref,
2252 .array_init_sent_ref,
22222253 .union_init,
22232254 .field_type,
22242255 .field_type_ref,
src/Sema.zig+30-11
......@@ -712,8 +712,10 @@ fn analyzeBodyInner(
712712 .struct_init_ref => try sema.zirStructInit(block, inst, true),
713713 .struct_init_anon => try sema.zirStructInitAnon(block, inst, false),
714714 .struct_init_anon_ref => try sema.zirStructInitAnon(block, inst, true),
715 .array_init => try sema.zirArrayInit(block, inst, false),
716 .array_init_ref => try sema.zirArrayInit(block, inst, true),
715 .array_init => try sema.zirArrayInit(block, inst, false, false),
716 .array_init_sent => try sema.zirArrayInit(block, inst, false, true),
717 .array_init_ref => try sema.zirArrayInit(block, inst, true, false),
718 .array_init_sent_ref => try sema.zirArrayInit(block, inst, true, true),
717719 .array_init_anon => try sema.zirArrayInitAnon(block, inst, false),
718720 .array_init_anon_ref => try sema.zirArrayInitAnon(block, inst, true),
719721 .union_init => try sema.zirUnionInit(block, inst),
......@@ -11782,6 +11784,7 @@ fn zirArrayInit(
1178211784 block: *Block,
1178311785 inst: Zir.Inst.Index,
1178411786 is_ref: bool,
11787 is_sent: bool,
1178511788) CompileError!Air.Inst.Ref {
1178611789 const gpa = sema.gpa;
1178711790 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
......@@ -11797,22 +11800,38 @@ fn zirArrayInit(
1179711800 for (args) |arg, i| resolved_args[i] = sema.resolveInst(arg);
1179811801
1179911802 const elem_ty = sema.typeOf(resolved_args[0]);
11803 const array_ty = blk: {
11804 if (!is_sent) {
11805 break :blk try Type.Tag.array.create(sema.arena, .{
11806 .len = resolved_args.len,
11807 .elem_type = elem_ty,
11808 });
11809 }
1180011810
11801 const array_ty = try Type.Tag.array.create(sema.arena, .{
11802 .len = resolved_args.len,
11803 .elem_type = elem_ty,
11804 });
11811 const sentinel_ref = resolved_args[resolved_args.len - 1];
11812 const val = try sema.resolveConstValue(block, src, sentinel_ref);
11813 break :blk try Type.Tag.array_sentinel.create(sema.arena, .{
11814 .len = resolved_args.len - 1,
11815 .sentinel = val,
11816 .elem_type = elem_ty,
11817 });
11818 };
11819
11820 const elems = if (!is_sent)
11821 resolved_args
11822 else
11823 resolved_args[0 .. resolved_args.len - 1];
1180511824
11806 const opt_runtime_src: ?LazySrcLoc = for (resolved_args) |arg| {
11825 const opt_runtime_src: ?LazySrcLoc = for (elems) |arg| {
1180711826 const arg_src = src; // TODO better source location
1180811827 const comptime_known = try sema.isComptimeKnown(block, arg_src, arg);
1180911828 if (!comptime_known) break arg_src;
1181011829 } else null;
1181111830
1181211831 const runtime_src = opt_runtime_src orelse {
11813 const elem_vals = try sema.arena.alloc(Value, resolved_args.len);
11832 const elem_vals = try sema.arena.alloc(Value, elems.len);
1181411833
11815 for (resolved_args) |arg, i| {
11834 for (elems) |arg, i| {
1181611835 // We checked that all args are comptime above.
1181711836 elem_vals[i] = (sema.resolveMaybeUndefVal(block, src, arg) catch unreachable).?;
1181811837 }
......@@ -11839,7 +11858,7 @@ fn zirArrayInit(
1183911858 });
1184011859 const elem_ptr_ty_ref = try sema.addType(elem_ptr_ty);
1184111860
11842 for (resolved_args) |arg, i| {
11861 for (elems) |arg, i| {
1184311862 const index = try sema.addIntUnsigned(Type.usize, i);
1184411863 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
1184511864 _ = try block.addBinOp(.store, elem_ptr, arg);
......@@ -11847,7 +11866,7 @@ fn zirArrayInit(
1184711866 return alloc;
1184811867 }
1184911868
11850 return block.addAggregateInit(array_ty, resolved_args);
11869 return block.addAggregateInit(array_ty, elems);
1185111870}
1185211871
1185311872fn zirArrayInitAnon(
src/Zir.zig+12
......@@ -710,12 +710,20 @@ pub const Inst = struct {
710710 /// Array initialization syntax.
711711 /// Uses the `pl_node` field. Payload is `MultiOp`.
712712 array_init,
713 /// Array initialization with sentinel.
714 /// Uses the `pl_node` field. Payload is `MultiOp`.
715 /// Final op in MultiOp is the sentinel.
716 array_init_sent,
713717 /// Anonymous array initialization syntax.
714718 /// Uses the `pl_node` field. Payload is `MultiOp`.
715719 array_init_anon,
716720 /// Array initialization syntax, make the result a pointer.
717721 /// Uses the `pl_node` field. Payload is `MultiOp`.
718722 array_init_ref,
723 /// Array initialization with sentinel.
724 /// Uses the `pl_node` field. Payload is `MultiOp`.
725 /// Final op in MultiOp is the sentinel.
726 array_init_sent_ref,
719727 /// Anonymous array initialization syntax, make the result a pointer.
720728 /// Uses the `pl_node` field. Payload is `MultiOp`.
721729 array_init_anon_ref,
......@@ -1121,8 +1129,10 @@ pub const Inst = struct {
11211129 .struct_init_anon,
11221130 .struct_init_anon_ref,
11231131 .array_init,
1132 .array_init_sent,
11241133 .array_init_anon,
11251134 .array_init_ref,
1135 .array_init_sent_ref,
11261136 .array_init_anon_ref,
11271137 .union_init,
11281138 .field_type,
......@@ -1379,8 +1389,10 @@ pub const Inst = struct {
13791389 .struct_init_anon = .pl_node,
13801390 .struct_init_anon_ref = .pl_node,
13811391 .array_init = .pl_node,
1392 .array_init_sent = .pl_node,
13821393 .array_init_anon = .pl_node,
13831394 .array_init_ref = .pl_node,
1395 .array_init_sent_ref = .pl_node,
13841396 .array_init_anon_ref = .pl_node,
13851397 .union_init = .pl_node,
13861398 .type_info = .un_node,
src/print_zir.zig+24
......@@ -269,6 +269,10 @@ const Writer = struct {
269269 .array_init_anon_ref,
270270 => try self.writeArrayInit(stream, inst),
271271
272 .array_init_sent,
273 .array_init_sent_ref,
274 => try self.writeArrayInitSent(stream, inst),
275
272276 .slice_start => try self.writeSliceStart(stream, inst),
273277 .slice_end => try self.writeSliceEnd(stream, inst),
274278 .slice_sentinel => try self.writeSliceSentinel(stream, inst),
......@@ -2022,6 +2026,26 @@ const Writer = struct {
20222026 try self.writeSrc(stream, inst_data.src());
20232027 }
20242028
2029 fn writeArrayInitSent(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2030 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
2031
2032 const extra = self.code.extraData(Zir.Inst.MultiOp, inst_data.payload_index);
2033 const args = self.code.refSlice(extra.end, extra.data.operands_len);
2034 const sent = args[args.len - 1];
2035 const elems = args[0 .. args.len - 1];
2036
2037 try self.writeInstRef(stream, sent);
2038 try stream.writeAll(", ");
2039
2040 try stream.writeAll(".{");
2041 for (elems) |elem, i| {
2042 if (i != 0) try stream.writeAll(", ");
2043 try self.writeInstRef(stream, elem);
2044 }
2045 try stream.writeAll("}) ");
2046 try self.writeSrc(stream, inst_data.src());
2047 }
2048
20252049 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
20262050 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";
20272051 const safety_str = if (inst_data.safety) "safe" else "unsafe";
test/behavior/pointers.zig+8
......@@ -270,6 +270,14 @@ test "assign null directly to C pointer and test null equality" {
270270 comptime try expect((y1 orelse &othery) == y1);
271271}
272272
273test "array initialization types" {
274 const E = enum { A, B, C };
275 try expect(@TypeOf([_]u8{}) == [0]u8);
276 try expect(@TypeOf([_:0]u8{}) == [0:0]u8);
277 try expect(@TypeOf([_:.A]E{}) == [0:.A]E);
278 try expect(@TypeOf([_:0]u8{ 1, 2, 3 }) == [3:0]u8);
279}
280
273281test "null terminated pointer" {
274282 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
275283