authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-07 22:28:24-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-07 22:28:24-08:00
log3176fdc0b921b36ee7d8346ef302d0817b980cfa
tree213a34bd1618ff429ec54efbc6652b2ee9940ff9
parent2115d7d1beeba32f975d8a032e5f5068e96e74f4
parent01b48e938198a206b95ed06f3a7e530e859c1cbc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #18470 from castholm/typeInfo-sentinels

Make `@typeInfo` return null-terminated strings

9 files changed, 46 insertions(+), 40 deletions(-)

deps/aro/aro/Attribute.zig+1-1
......@@ -645,7 +645,7 @@ pub const Arguments = blk: {
645645 var union_fields: [decls.len]ZigType.UnionField = undefined;
646646 for (decls, &union_fields) |decl, *field| {
647647 field.* = .{
648 .name = decl.name,
648 .name = decl.name ++ "",
649649 .type = @field(attributes, decl.name),
650650 .alignment = 0,
651651 };
lib/std/builtin.zig+5-5
......@@ -314,7 +314,7 @@ pub const Type = union(enum) {
314314 /// This data structure is used by the Zig language code generation and
315315 /// therefore must be kept in sync with the compiler implementation.
316316 pub const StructField = struct {
317 name: []const u8,
317 name: [:0]const u8,
318318 type: type,
319319 default_value: ?*const anyopaque,
320320 is_comptime: bool,
......@@ -348,7 +348,7 @@ pub const Type = union(enum) {
348348 /// This data structure is used by the Zig language code generation and
349349 /// therefore must be kept in sync with the compiler implementation.
350350 pub const Error = struct {
351 name: []const u8,
351 name: [:0]const u8,
352352 };
353353
354354 /// This data structure is used by the Zig language code generation and
......@@ -358,7 +358,7 @@ pub const Type = union(enum) {
358358 /// This data structure is used by the Zig language code generation and
359359 /// therefore must be kept in sync with the compiler implementation.
360360 pub const EnumField = struct {
361 name: []const u8,
361 name: [:0]const u8,
362362 value: comptime_int,
363363 };
364364
......@@ -374,7 +374,7 @@ pub const Type = union(enum) {
374374 /// This data structure is used by the Zig language code generation and
375375 /// therefore must be kept in sync with the compiler implementation.
376376 pub const UnionField = struct {
377 name: []const u8,
377 name: [:0]const u8,
378378 type: type,
379379 alignment: comptime_int,
380380 };
......@@ -436,7 +436,7 @@ pub const Type = union(enum) {
436436 /// This data structure is used by the Zig language code generation and
437437 /// therefore must be kept in sync with the compiler implementation.
438438 pub const Declaration = struct {
439 name: []const u8,
439 name: [:0]const u8,
440440 };
441441};
442442
lib/std/enums.zig+1-1
......@@ -14,7 +14,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
1414 var fields: []const StructField = &[_]StructField{};
1515 for (std.meta.fields(E)) |field| {
1616 fields = fields ++ &[_]StructField{.{
17 .name = field.name,
17 .name = field.name ++ "",
1818 .type = Data,
1919 .default_value = if (field_default) |d| @as(?*const anyopaque, @ptrCast(&d)) else null,
2020 .is_comptime = false,
lib/std/io.zig+1-1
......@@ -635,7 +635,7 @@ pub fn PollFiles(comptime StreamEnum: type) type {
635635 var struct_fields: [enum_fields.len]std.builtin.Type.StructField = undefined;
636636 for (&struct_fields, enum_fields) |*struct_field, enum_field| {
637637 struct_field.* = .{
638 .name = enum_field.name,
638 .name = enum_field.name ++ "",
639639 .type = fs.File,
640640 .default_value = null,
641641 .is_comptime = false,
lib/std/meta.zig+3-3
......@@ -556,7 +556,7 @@ pub fn FieldEnum(comptime T: type) type {
556556 var decls = [_]std.builtin.Type.Declaration{};
557557 inline for (field_infos, 0..) |field, i| {
558558 enumFields[i] = .{
559 .name = field.name,
559 .name = field.name ++ "",
560560 .value = i,
561561 };
562562 }
......@@ -628,7 +628,7 @@ pub fn DeclEnum(comptime T: type) type {
628628 var enumDecls: [fieldInfos.len]std.builtin.Type.EnumField = undefined;
629629 var decls = [_]std.builtin.Type.Declaration{};
630630 inline for (fieldInfos, 0..) |field, i| {
631 enumDecls[i] = .{ .name = field.name, .value = i };
631 enumDecls[i] = .{ .name = field.name ++ "", .value = i };
632632 }
633633 return @Type(.{
634634 .Enum = .{
......@@ -1015,7 +1015,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
10151015 @setEvalBranchQuota(10_000);
10161016 var num_buf: [128]u8 = undefined;
10171017 tuple_fields[i] = .{
1018 .name = std.fmt.bufPrint(&num_buf, "{d}", .{i}) catch unreachable,
1018 .name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,
10191019 .type = T,
10201020 .default_value = null,
10211021 .is_comptime = false,
src/InternPool.zig+1-1
......@@ -5315,7 +5315,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
53155315
53165316 try ip.extra.ensureUnusedCapacity(
53175317 gpa,
5318 @typeInfo(Tag.Aggregate).Struct.fields.len + @as(usize, @intCast(len_including_sentinel)),
5318 @typeInfo(Tag.Aggregate).Struct.fields.len + @as(usize, @intCast(len_including_sentinel + 1)),
53195319 );
53205320 ip.items.appendAssumeCapacity(.{
53215321 .tag = .aggregate,
src/Sema.zig+31-25
......@@ -17259,10 +17259,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1725917259 const vals = try sema.arena.alloc(InternPool.Index, names.len);
1726017260 for (vals, 0..) |*field_val, i| {
1726117261 // TODO: write something like getCoercedInts to avoid needing to dupe
17262 const name = try sema.arena.dupe(u8, ip.stringToSlice(names.get(ip)[i]));
17262 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(names.get(ip)[i]));
1726317263 const name_val = v: {
1726417264 const new_decl_ty = try mod.arrayType(.{
1726517265 .len = name.len,
17266 .sentinel = .zero_u8,
1726617267 .child = .u8_type,
1726717268 });
1726817269 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17270,17 +17271,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1727017271 .storage = .{ .bytes = name },
1727117272 } });
1727217273 break :v try mod.intern(.{ .ptr = .{
17273 .ty = .slice_const_u8_type,
17274 .ty = .slice_const_u8_sentinel_0_type,
1727417275 .addr = .{ .anon_decl = .{
1727517276 .val = new_decl_val,
17276 .orig_ty = .slice_const_u8_type,
17277 .orig_ty = .slice_const_u8_sentinel_0_type,
1727717278 } },
1727817279 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1727917280 } });
1728017281 };
1728117282
1728217283 const error_field_fields = .{
17283 // name: []const u8,
17284 // name: [:0]const u8,
1728417285 name_val,
1728517286 };
1728617287 field_val.* = try mod.intern(.{ .aggregate = .{
......@@ -17387,10 +17388,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1738717388 else
1738817389 (try mod.intValue(Type.comptime_int, i)).toIntern();
1738917390 // TODO: write something like getCoercedInts to avoid needing to dupe
17390 const name = try sema.arena.dupe(u8, ip.stringToSlice(enum_type.names.get(ip)[i]));
17391 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(enum_type.names.get(ip)[i]));
1739117392 const name_val = v: {
1739217393 const new_decl_ty = try mod.arrayType(.{
1739317394 .len = name.len,
17395 .sentinel = .zero_u8,
1739417396 .child = .u8_type,
1739517397 });
1739617398 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17398,17 +17400,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1739817400 .storage = .{ .bytes = name },
1739917401 } });
1740017402 break :v try mod.intern(.{ .ptr = .{
17401 .ty = .slice_const_u8_type,
17403 .ty = .slice_const_u8_sentinel_0_type,
1740217404 .addr = .{ .anon_decl = .{
1740317405 .val = new_decl_val,
17404 .orig_ty = .slice_const_u8_type,
17406 .orig_ty = .slice_const_u8_sentinel_0_type,
1740517407 } },
1740617408 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1740717409 } });
1740817410 };
1740917411
1741017412 const enum_field_fields = .{
17411 // name: []const u8,
17413 // name: [:0]const u8,
1741217414 name_val,
1741317415 // value: comptime_int,
1741417416 value_val,
......@@ -17512,10 +17514,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1751217514
1751317515 for (union_field_vals, 0..) |*field_val, i| {
1751417516 // TODO: write something like getCoercedInts to avoid needing to dupe
17515 const name = try sema.arena.dupe(u8, ip.stringToSlice(union_obj.field_names.get(ip)[i]));
17517 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(union_obj.field_names.get(ip)[i]));
1751617518 const name_val = v: {
1751717519 const new_decl_ty = try mod.arrayType(.{
1751817520 .len = name.len,
17521 .sentinel = .zero_u8,
1751917522 .child = .u8_type,
1752017523 });
1752117524 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17523,10 +17526,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1752317526 .storage = .{ .bytes = name },
1752417527 } });
1752517528 break :v try mod.intern(.{ .ptr = .{
17526 .ty = .slice_const_u8_type,
17529 .ty = .slice_const_u8_sentinel_0_type,
1752717530 .addr = .{ .anon_decl = .{
1752817531 .val = new_decl_val,
17529 .orig_ty = .slice_const_u8_type,
17532 .orig_ty = .slice_const_u8_sentinel_0_type,
1753017533 } },
1753117534 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1753217535 } });
......@@ -17539,7 +17542,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1753917542
1754017543 const field_ty = union_obj.field_types.get(ip)[i];
1754117544 const union_field_fields = .{
17542 // name: []const u8,
17545 // name: [:0]const u8,
1754317546 name_val,
1754417547 // type: type,
1754517548 field_ty,
......@@ -17658,11 +17661,12 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1765817661 // TODO: write something like getCoercedInts to avoid needing to dupe
1765917662 const bytes = if (tuple.names.len != 0)
1766017663 // https://github.com/ziglang/zig/issues/15709
17661 try sema.arena.dupe(u8, ip.stringToSlice(ip.indexToKey(ty.toIntern()).anon_struct_type.names.get(ip)[i]))
17664 try sema.arena.dupeZ(u8, ip.stringToSlice(ip.indexToKey(ty.toIntern()).anon_struct_type.names.get(ip)[i]))
1766217665 else
17663 try std.fmt.allocPrint(sema.arena, "{d}", .{i});
17666 try std.fmt.allocPrintZ(sema.arena, "{d}", .{i});
1766417667 const new_decl_ty = try mod.arrayType(.{
1766517668 .len = bytes.len,
17669 .sentinel = .zero_u8,
1766617670 .child = .u8_type,
1766717671 });
1766817672 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17670,10 +17674,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1767017674 .storage = .{ .bytes = bytes },
1767117675 } });
1767217676 break :v try mod.intern(.{ .ptr = .{
17673 .ty = .slice_const_u8_type,
17677 .ty = .slice_const_u8_sentinel_0_type,
1767417678 .addr = .{ .anon_decl = .{
1767517679 .val = new_decl_val,
17676 .orig_ty = .slice_const_u8_type,
17680 .orig_ty = .slice_const_u8_sentinel_0_type,
1767717681 } },
1767817682 .len = (try mod.intValue(Type.usize, bytes.len)).toIntern(),
1767917683 } });
......@@ -17685,7 +17689,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1768517689 const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null;
1768617690 const default_val_ptr = try sema.optRefValue(opt_default_val);
1768717691 const struct_field_fields = .{
17688 // name: []const u8,
17692 // name: [:0]const u8,
1768917693 name_val,
1769017694 // type: type,
1769117695 field_ty,
......@@ -17713,7 +17717,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1771317717 for (struct_field_vals, 0..) |*field_val, i| {
1771417718 // TODO: write something like getCoercedInts to avoid needing to dupe
1771517719 const name = if (struct_type.fieldName(ip, i).unwrap()) |name_nts|
17716 try sema.arena.dupe(u8, ip.stringToSlice(name_nts))
17720 try sema.arena.dupeZ(u8, ip.stringToSlice(name_nts))
1771717721 else
1771817722 try std.fmt.allocPrintZ(sema.arena, "{d}", .{i});
1771917723 const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]);
......@@ -17722,6 +17726,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1772217726 const name_val = v: {
1772317727 const new_decl_ty = try mod.arrayType(.{
1772417728 .len = name.len,
17729 .sentinel = .zero_u8,
1772517730 .child = .u8_type,
1772617731 });
1772717732 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17729,10 +17734,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1772917734 .storage = .{ .bytes = name },
1773017735 } });
1773117736 break :v try mod.intern(.{ .ptr = .{
17732 .ty = .slice_const_u8_type,
17737 .ty = .slice_const_u8_sentinel_0_type,
1773317738 .addr = .{ .anon_decl = .{
1773417739 .val = new_decl_val,
17735 .orig_ty = .slice_const_u8_type,
17740 .orig_ty = .slice_const_u8_sentinel_0_type,
1773617741 } },
1773717742 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1773817743 } });
......@@ -17750,7 +17755,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1775017755 };
1775117756
1775217757 const struct_field_fields = .{
17753 // name: []const u8,
17758 // name: [:0]const u8,
1775417759 name_val,
1775517760 // type: type,
1775617761 field_ty.toIntern(),
......@@ -17957,9 +17962,10 @@ fn typeInfoNamespaceDecls(
1795717962 if (decl.kind != .named or !decl.is_pub) continue;
1795817963 const name_val = v: {
1795917964 // TODO: write something like getCoercedInts to avoid needing to dupe
17960 const name = try sema.arena.dupe(u8, ip.stringToSlice(decl.name));
17965 const name = try sema.arena.dupeZ(u8, ip.stringToSlice(decl.name));
1796117966 const new_decl_ty = try mod.arrayType(.{
1796217967 .len = name.len,
17968 .sentinel = .zero_u8,
1796317969 .child = .u8_type,
1796417970 });
1796517971 const new_decl_val = try mod.intern(.{ .aggregate = .{
......@@ -17967,9 +17973,9 @@ fn typeInfoNamespaceDecls(
1796717973 .storage = .{ .bytes = name },
1796817974 } });
1796917975 break :v try mod.intern(.{ .ptr = .{
17970 .ty = .slice_const_u8_type,
17976 .ty = .slice_const_u8_sentinel_0_type,
1797117977 .addr = .{ .anon_decl = .{
17972 .orig_ty = .slice_const_u8_type,
17978 .orig_ty = .slice_const_u8_sentinel_0_type,
1797317979 .val = new_decl_val,
1797417980 } },
1797517981 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
......@@ -17977,7 +17983,7 @@ fn typeInfoNamespaceDecls(
1797717983 };
1797817984
1797917985 const fields = .{
17980 //name: []const u8,
17986 //name: [:0]const u8,
1798117987 name_val,
1798217988 };
1798317989 try decl_vals.append(try mod.intern(.{ .aggregate = .{
src/value.zig+1-1
......@@ -4050,7 +4050,7 @@ pub const Value = struct {
40504050 const tags = @typeInfo(Tag).Enum.fields;
40514051 var fields: [tags.len]std.builtin.Type.StructField = undefined;
40524052 for (&fields, tags) |*field, t| field.* = .{
4053 .name = t.name,
4053 .name = t.name ++ "",
40544054 .type = *@field(Tag, t.name).Type(),
40554055 .default_value = null,
40564056 .is_comptime = false,
test/behavior/type.zig+2-2
......@@ -549,7 +549,7 @@ test "Type.Fn" {
549549
550550test "reified struct field name from optional payload" {
551551 comptime {
552 const m_name: ?[1]u8 = "a".*;
552 const m_name: ?[1:0]u8 = "a".*;
553553 if (m_name) |*name| {
554554 const T = @Type(.{ .Struct = .{
555555 .layout = .Auto,
......@@ -711,7 +711,7 @@ test "struct field names sliced at comptime from larger string" {
711711 while (it.next()) |name| {
712712 fields = fields ++ &[_]Type.StructField{.{
713713 .alignment = 0,
714 .name = name,
714 .name = name ++ "",
715715 .type = usize,
716716 .default_value = null,
717717 .is_comptime = false,