Skip to content

Change field names in @typeInfo and std.builtin.Type to be zero terminated #16330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub const Type = union(enum) {
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const StructField = struct {
name: []const u8,
name: [:0]const u8,
type: type,
default_value: ?*const anyopaque,
is_comptime: bool,
Expand Down Expand Up @@ -347,7 +347,7 @@ pub const Type = union(enum) {
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Error = struct {
name: []const u8,
name: [:0]const u8,
};

/// This data structure is used by the Zig language code generation and
Expand All @@ -357,7 +357,7 @@ pub const Type = union(enum) {
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const EnumField = struct {
name: []const u8,
name: [:0]const u8,
value: comptime_int,
};

Expand All @@ -373,7 +373,7 @@ pub const Type = union(enum) {
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const UnionField = struct {
name: []const u8,
name: [:0]const u8,
type: type,
alignment: comptime_int,
};
Expand Down
37 changes: 21 additions & 16 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16837,13 +16837,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const vals = try sema.arena.alloc(InternPool.Index, ty.errorSetNames(mod).len);
for (vals, 0..) |*field_val, i| {
// TODO: write something like getCoercedInts to avoid needing to dupe
const name = try sema.arena.dupe(u8, ip.stringToSlice(ty.errorSetNames(mod)[i]));
const name = try sema.arena.dupeZ(u8, ip.stringToSlice(ty.errorSetNames(mod)[i]));
const name_val = v: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
const new_decl_ty = try mod.arrayType(.{
.len = name.len,
.child = .u8_type,
.sentinel = .zero_u8,
});
const new_decl = try anon_decl.finish(
new_decl_ty,
Expand All @@ -16854,14 +16855,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.none, // default alignment
);
break :v try mod.intern(.{ .ptr = .{
.ty = .slice_const_u8_type,
.ty = .slice_const_u8_sentinel_0_type,
.addr = .{ .decl = new_decl },
.len = (try mod.intValue(Type.usize, name.len)).toIntern(),
} });
};

const error_field_fields = .{
// name: []const u8,
// name: [:0]const u8,
name_val,
};
field_val.* = try mod.intern(.{ .aggregate = .{
Expand Down Expand Up @@ -16973,13 +16974,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.storage = .{ .u64 = @as(u64, @intCast(i)) },
} });
// TODO: write something like getCoercedInts to avoid needing to dupe
const name = try sema.arena.dupe(u8, ip.stringToSlice(enum_type.names[i]));
const name = try sema.arena.dupeZ(u8, ip.stringToSlice(enum_type.names[i]));
const name_val = v: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
const new_decl_ty = try mod.arrayType(.{
.len = name.len,
.child = .u8_type,
.sentinel = .zero_u8,
});
const new_decl = try anon_decl.finish(
new_decl_ty,
Expand All @@ -16990,14 +16992,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.none, // default alignment
);
break :v try mod.intern(.{ .ptr = .{
.ty = .slice_const_u8_type,
.ty = .slice_const_u8_sentinel_0_type,
.addr = .{ .decl = new_decl },
.len = (try mod.intValue(Type.usize, name.len)).toIntern(),
} });
};

const enum_field_fields = .{
// name: []const u8,
// name: [:0]const u8,
name_val,
// value: comptime_int,
value_val,
Expand Down Expand Up @@ -17111,13 +17113,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
for (union_field_vals, 0..) |*field_val, i| {
const field = union_fields.values()[i];
// TODO: write something like getCoercedInts to avoid needing to dupe
const name = try sema.arena.dupe(u8, ip.stringToSlice(union_fields.keys()[i]));
const name = try sema.arena.dupeZ(u8, ip.stringToSlice(union_fields.keys()[i]));
const name_val = v: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
const new_decl_ty = try mod.arrayType(.{
.len = name.len,
.child = .u8_type,
.sentinel = .zero_u8,
});
const new_decl = try anon_decl.finish(
new_decl_ty,
Expand All @@ -17128,7 +17131,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.none, // default alignment
);
break :v try mod.intern(.{ .ptr = .{
.ty = .slice_const_u8_type,
.ty = .slice_const_u8_sentinel_0_type,
.addr = .{ .decl = new_decl },
.len = (try mod.intValue(Type.usize, name.len)).toIntern(),
} });
Expand All @@ -17140,7 +17143,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
};

const union_field_fields = .{
// name: []const u8,
// name: [:0]const u8,
name_val,
// type: type,
field.ty.toIntern(),
Expand Down Expand Up @@ -17271,12 +17274,13 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
// TODO: write something like getCoercedInts to avoid needing to dupe
const bytes = if (tuple.names.len != 0)
// https://github.com/ziglang/zig/issues/15709
try sema.arena.dupe(u8, ip.stringToSlice(ip.indexToKey(struct_ty.toIntern()).anon_struct_type.names[i]))
try sema.arena.dupeZ(u8, ip.stringToSlice(ip.indexToKey(struct_ty.toIntern()).anon_struct_type.names[i]))
else
try std.fmt.allocPrint(sema.arena, "{d}", .{i});
try std.fmt.allocPrintZ(sema.arena, "{d}", .{i});
const new_decl_ty = try mod.arrayType(.{
.len = bytes.len,
.child = .u8_type,
.sentinel = .zero_u8,
});
const new_decl = try anon_decl.finish(
new_decl_ty,
Expand All @@ -17287,7 +17291,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.none, // default alignment
);
break :v try mod.intern(.{ .ptr = .{
.ty = .slice_const_u8_type,
.ty = .slice_const_u8_sentinel_0_type,
.addr = .{ .decl = new_decl },
.len = (try mod.intValue(Type.usize, bytes.len)).toIntern(),
} });
Expand All @@ -17297,7 +17301,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const opt_default_val = if (is_comptime) field_val.toValue() else null;
const default_val_ptr = try sema.optRefValue(block, field_ty.toType(), opt_default_val);
const struct_field_fields = .{
// name: []const u8,
// name: [:0]const u8,
name_val,
// type: type,
field_ty,
Expand Down Expand Up @@ -17327,13 +17331,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
struct_obj.fields.values(),
) |*field_val, name_nts, field| {
// TODO: write something like getCoercedInts to avoid needing to dupe
const name = try sema.arena.dupe(u8, ip.stringToSlice(name_nts));
const name = try sema.arena.dupeZ(u8, ip.stringToSlice(name_nts));
const name_val = v: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
const new_decl_ty = try mod.arrayType(.{
.len = name.len,
.child = .u8_type,
.sentinel = .zero_u8,
});
const new_decl = try anon_decl.finish(
new_decl_ty,
Expand All @@ -17344,7 +17349,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
.none, // default alignment
);
break :v try mod.intern(.{ .ptr = .{
.ty = .slice_const_u8_type,
.ty = .slice_const_u8_sentinel_0_type,
.addr = .{ .decl = new_decl },
.len = (try mod.intValue(Type.usize, name.len)).toIntern(),
} });
Expand All @@ -17358,7 +17363,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const alignment = field.alignment(mod, layout);

const struct_field_fields = .{
// name: []const u8,
// name: [:0]const u8,
name_val,
// type: type,
field.ty.toIntern(),
Expand Down
6 changes: 0 additions & 6 deletions stage1/zig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3178,12 +3178,6 @@ zig_bitCast_float(f64, uint64_t)
zig_bitCast_float(f80, zig_u128)
zig_bitCast_float(f128, zig_u128)

#define zig_cast_f16
#define zig_cast_f32
#define zig_cast_f64
#define zig_cast_f80
#define zig_cast_f128

#define zig_convert_builtin(ExternResType, ResType, operation, ExternArgType, ArgType, version) \
zig_extern ExternResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ExternArgType); \
Expand Down
Binary file modified stage1/zig1.wasm
Binary file not shown.