Skip to content

Commit 4d8c24c

Browse files
authored
Merge pull request #22505 from mlugg/easier-modify-builtin
std.builtin.Type renames, and make it easier to modify std.builtin
2 parents 133abde + 9804cc8 commit 4d8c24c

File tree

92 files changed

+1178
-947
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1178
-947
lines changed

bootstrap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ int main(int argc, char **argv) {
141141
"pub const skip_non_native = false;\n"
142142
"pub const force_gpa = false;\n"
143143
"pub const dev = .core;\n"
144+
"pub const value_interpret_mode = .direct;\n"
144145
, zig_version);
145146
if (written < 100)
146147
panic("unable to write to config.zig file");

build.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const fs = std.fs;
99
const InstallDirectoryOptions = std.Build.InstallDirectoryOptions;
1010
const assert = std.debug.assert;
1111
const DevEnv = @import("src/dev.zig").Env;
12+
const ValueInterpretMode = enum { direct, by_name };
1213

1314
const zig_version: std.SemanticVersion = .{ .major = 0, .minor = 14, .patch = 0 };
1415
const stack_size = 46 * 1024 * 1024;
@@ -177,6 +178,7 @@ pub fn build(b: *std.Build) !void {
177178
const strip = b.option(bool, "strip", "Omit debug information");
178179
const valgrind = b.option(bool, "valgrind", "Enable valgrind integration");
179180
const pie = b.option(bool, "pie", "Produce a Position Independent Executable");
181+
const value_interpret_mode = b.option(ValueInterpretMode, "value-interpret-mode", "How the compiler translates between 'std.builtin' types and its internal datastructures") orelse .direct;
180182
const value_tracing = b.option(bool, "value-tracing", "Enable extra state tracking to help troubleshoot bugs in the compiler (using the std.debug.Trace API)") orelse false;
181183

182184
const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
@@ -234,6 +236,7 @@ pub fn build(b: *std.Build) !void {
234236
exe_options.addOption(bool, "llvm_has_xtensa", llvm_has_xtensa);
235237
exe_options.addOption(bool, "force_gpa", force_gpa);
236238
exe_options.addOption(DevEnv, "dev", b.option(DevEnv, "dev", "Build a compiler with a reduced feature set for development of specific features") orelse if (only_c) .bootstrap else .full);
239+
exe_options.addOption(ValueInterpretMode, "value_interpret_mode", value_interpret_mode);
237240

238241
if (link_libc) {
239242
exe.root_module.link_libc = true;
@@ -620,6 +623,23 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
620623
exe_options.addOption(bool, "value_tracing", false);
621624
exe_options.addOption(DevEnv, "dev", .bootstrap);
622625

626+
// zig1 chooses to interpret values by name. The tradeoff is as follows:
627+
//
628+
// * We lose a small amount of performance. This is essentially irrelevant for zig1.
629+
//
630+
// * We lose the ability to perform trivial renames on certain `std.builtin` types without
631+
// zig1.wasm updates. For instance, we cannot rename an enum from PascalCase fields to
632+
// snake_case fields without an update.
633+
//
634+
// * We gain the ability to add and remove fields to and from `std.builtin` types without
635+
// zig1.wasm updates. For instance, we can add a new tag to `CallingConvention` without
636+
// an update.
637+
//
638+
// Because field renames only happen when we apply a breaking change to the language (which
639+
// is becoming progressively rarer), but tags may be added to or removed from target-dependent
640+
// types over time in response to new targets coming into use, we gain more than we lose here.
641+
exe_options.addOption(ValueInterpretMode, "value_interpret_mode", .by_name);
642+
623643
const run_opt = b.addSystemCommand(&.{
624644
"wasm-opt",
625645
"-Oz",

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ fn putValue(config_header: *ConfigHeader, field_name: []const u8, comptime T: ty
144144
.pointer => |ptr| {
145145
switch (@typeInfo(ptr.child)) {
146146
.array => |array| {
147-
if (ptr.size == .One and array.child == u8) {
147+
if (ptr.size == .one and array.child == u8) {
148148
try config_header.values.put(field_name, .{ .string = v });
149149
return;
150150
}
151151
},
152152
.int => {
153-
if (ptr.size == .Slice and ptr.child == u8) {
153+
if (ptr.size == .slice and ptr.child == u8) {
154154
try config_header.values.put(field_name, .{ .string = v });
155155
return;
156156
}

lib/std/Build/Step/Options.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn printType(options: *Options, out: anytype, comptime T: type, value: T, indent
172172
return;
173173
},
174174
.pointer => |p| {
175-
if (p.size != .Slice) {
175+
if (p.size != .slice) {
176176
@compileError("Non-slice pointers are not yet supported in build options");
177177
}
178178

@@ -318,9 +318,7 @@ fn printStruct(options: *Options, out: anytype, comptime T: type, comptime val:
318318
try out.print(" {p_}: {s}", .{ std.zig.fmtId(field.name), type_name });
319319
}
320320

321-
if (field.default_value != null) {
322-
const default_value = @as(*field.type, @ptrCast(@alignCast(@constCast(field.default_value.?)))).*;
323-
321+
if (field.defaultValue()) |default_value| {
324322
try out.writeAll(" = ");
325323
switch (@typeInfo(@TypeOf(default_value))) {
326324
.@"enum" => try out.print(".{s},\n", .{@tagName(default_value)}),

lib/std/Progress.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ fn maybeUpdateSize(resize_flag: bool) void {
13661366
}
13671367
}
13681368

1369-
fn handleSigWinch(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.C) void {
1369+
fn handleSigWinch(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.c) void {
13701370
_ = info;
13711371
_ = ctx_ptr;
13721372
assert(sig == posix.SIG.WINCH);

lib/std/Random.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fillFn: *const fn (ptr: *anyopaque, buf: []u8) void,
3535
pub fn init(pointer: anytype, comptime fillFn: fn (ptr: @TypeOf(pointer), buf: []u8) void) Random {
3636
const Ptr = @TypeOf(pointer);
3737
assert(@typeInfo(Ptr) == .pointer); // Must be a pointer
38-
assert(@typeInfo(Ptr).pointer.size == .One); // Must be a single-item pointer
38+
assert(@typeInfo(Ptr).pointer.size == .one); // Must be a single-item pointer
3939
assert(@typeInfo(@typeInfo(Ptr).pointer.child) == .@"struct"); // Must point to a struct
4040
const gen = struct {
4141
fn fill(ptr: *anyopaque, buf: []u8) void {

lib/std/builtin.zig

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,24 @@ pub const Type = union(enum) {
607607

608608
/// The type of the sentinel is the element type of the pointer, which is
609609
/// the value of the `child` field in this struct. However there is no way
610-
/// to refer to that type here, so we use pointer to `anyopaque`.
611-
sentinel: ?*const anyopaque,
610+
/// to refer to that type here, so we use `*const anyopaque`.
611+
/// See also: `sentinel`
612+
sentinel_ptr: ?*const anyopaque,
613+
614+
/// Loads the pointer type's sentinel value from `sentinel_ptr`.
615+
/// Returns `null` if the pointer type has no sentinel.
616+
pub inline fn sentinel(comptime ptr: Pointer) ?ptr.child {
617+
const sp: *const ptr.child = @ptrCast(@alignCast(ptr.sentinel_ptr orelse return null));
618+
return sp.*;
619+
}
612620

613621
/// This data structure is used by the Zig language code generation and
614622
/// therefore must be kept in sync with the compiler implementation.
615623
pub const Size = enum(u2) {
616-
One,
617-
Many,
618-
Slice,
619-
C,
624+
one,
625+
many,
626+
slice,
627+
c,
620628
};
621629
};
622630

@@ -628,8 +636,16 @@ pub const Type = union(enum) {
628636

629637
/// The type of the sentinel is the element type of the array, which is
630638
/// the value of the `child` field in this struct. However there is no way
631-
/// to refer to that type here, so we use pointer to `anyopaque`.
632-
sentinel: ?*const anyopaque,
639+
/// to refer to that type here, so we use `*const anyopaque`.
640+
/// See also: `sentinel`.
641+
sentinel_ptr: ?*const anyopaque,
642+
643+
/// Loads the array type's sentinel value from `sentinel_ptr`.
644+
/// Returns `null` if the array type has no sentinel.
645+
pub inline fn sentinel(comptime arr: Array) ?arr.child {
646+
const sp: *const arr.child = @ptrCast(@alignCast(arr.sentinel_ptr orelse return null));
647+
return sp.*;
648+
}
633649
};
634650

635651
/// This data structure is used by the Zig language code generation and
@@ -645,9 +661,20 @@ pub const Type = union(enum) {
645661
pub const StructField = struct {
646662
name: [:0]const u8,
647663
type: type,
648-
default_value: ?*const anyopaque,
664+
/// The type of the default value is the type of this struct field, which
665+
/// is the value of the `type` field in this struct. However there is no
666+
/// way to refer to that type here, so we use `*const anyopaque`.
667+
/// See also: `defaultValue`.
668+
default_value_ptr: ?*const anyopaque,
649669
is_comptime: bool,
650670
alignment: comptime_int,
671+
672+
/// Loads the field's default value from `default_value_ptr`.
673+
/// Returns `null` if the field has no default value.
674+
pub inline fn defaultValue(comptime sf: StructField) ?sf.type {
675+
const dp: *const sf.type = @ptrCast(@alignCast(sf.default_value_ptr orelse return null));
676+
return dp.*;
677+
}
651678
};
652679

653680
/// This data structure is used by the Zig language code generation and

lib/std/c.zig

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,48 +2733,48 @@ pub const Sigaction = switch (native_os) {
27332733
=> if (builtin.target.isMusl())
27342734
linux.Sigaction
27352735
else if (builtin.target.ptrBitWidth() == 64) extern struct {
2736-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2737-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2736+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2737+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
27382738

27392739
flags: c_uint,
27402740
handler: extern union {
27412741
handler: ?handler_fn,
27422742
sigaction: ?sigaction_fn,
27432743
},
27442744
mask: sigset_t,
2745-
restorer: ?*const fn () callconv(.C) void = null,
2745+
restorer: ?*const fn () callconv(.c) void = null,
27462746
} else extern struct {
2747-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2748-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2747+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2748+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
27492749

27502750
flags: c_uint,
27512751
handler: extern union {
27522752
handler: ?handler_fn,
27532753
sigaction: ?sigaction_fn,
27542754
},
27552755
mask: sigset_t,
2756-
restorer: ?*const fn () callconv(.C) void = null,
2756+
restorer: ?*const fn () callconv(.c) void = null,
27572757
__resv: [1]c_int = .{0},
27582758
},
27592759
.s390x => if (builtin.abi == .gnu) extern struct {
2760-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2761-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2760+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2761+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
27622762

27632763
handler: extern union {
27642764
handler: ?handler_fn,
27652765
sigaction: ?sigaction_fn,
27662766
},
27672767
__glibc_reserved0: c_int = 0,
27682768
flags: c_uint,
2769-
restorer: ?*const fn () callconv(.C) void = null,
2769+
restorer: ?*const fn () callconv(.c) void = null,
27702770
mask: sigset_t,
27712771
} else linux.Sigaction,
27722772
else => linux.Sigaction,
27732773
},
27742774
.emscripten => emscripten.Sigaction,
27752775
.netbsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
2776-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2777-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2776+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2777+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
27782778

27792779
handler: extern union {
27802780
handler: ?handler_fn,
@@ -2784,8 +2784,8 @@ pub const Sigaction = switch (native_os) {
27842784
flags: c_uint,
27852785
},
27862786
.dragonfly, .freebsd => extern struct {
2787-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2788-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2787+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2788+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
27892789

27902790
/// signal handler
27912791
handler: extern union {
@@ -2798,8 +2798,8 @@ pub const Sigaction = switch (native_os) {
27982798
mask: sigset_t,
27992799
},
28002800
.solaris, .illumos => extern struct {
2801-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2802-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2801+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2802+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
28032803

28042804
/// signal options
28052805
flags: c_uint,
@@ -2812,8 +2812,8 @@ pub const Sigaction = switch (native_os) {
28122812
mask: sigset_t,
28132813
},
28142814
.haiku => extern struct {
2815-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2816-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2815+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2816+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
28172817

28182818
/// signal handler
28192819
handler: extern union {
@@ -2831,8 +2831,8 @@ pub const Sigaction = switch (native_os) {
28312831
userdata: *allowzero anyopaque = undefined,
28322832
},
28332833
.openbsd => extern struct {
2834-
pub const handler_fn = *align(1) const fn (i32) callconv(.C) void;
2835-
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.C) void;
2834+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
2835+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
28362836

28372837
/// signal handler
28382838
handler: extern union {
@@ -6410,7 +6410,7 @@ pub const EAI = switch (native_os) {
64106410
else => void,
64116411
};
64126412

6413-
pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int;
6413+
pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.c) c_int;
64146414

64156415
pub const Stat = switch (native_os) {
64166416
.linux => switch (native_arch) {
@@ -9396,7 +9396,7 @@ pub extern "c" fn futimens(fd: fd_t, times: *const [2]timespec) c_int;
93969396
pub extern "c" fn pthread_create(
93979397
noalias newthread: *pthread_t,
93989398
noalias attr: ?*const pthread_attr_t,
9399-
start_routine: *const fn (?*anyopaque) callconv(.C) ?*anyopaque,
9399+
start_routine: *const fn (?*anyopaque) callconv(.c) ?*anyopaque,
94009400
noalias arg: ?*anyopaque,
94019401
) E;
94029402
pub extern "c" fn pthread_attr_init(attr: *pthread_attr_t) E;
@@ -9408,13 +9408,13 @@ pub extern "c" fn pthread_self() pthread_t;
94089408
pub extern "c" fn pthread_join(thread: pthread_t, arg_return: ?*?*anyopaque) E;
94099409
pub extern "c" fn pthread_detach(thread: pthread_t) E;
94109410
pub extern "c" fn pthread_atfork(
9411-
prepare: ?*const fn () callconv(.C) void,
9412-
parent: ?*const fn () callconv(.C) void,
9413-
child: ?*const fn () callconv(.C) void,
9411+
prepare: ?*const fn () callconv(.c) void,
9412+
parent: ?*const fn () callconv(.c) void,
9413+
child: ?*const fn () callconv(.c) void,
94149414
) c_int;
94159415
pub extern "c" fn pthread_key_create(
94169416
key: *pthread_key_t,
9417-
destructor: ?*const fn (value: *anyopaque) callconv(.C) void,
9417+
destructor: ?*const fn (value: *anyopaque) callconv(.c) void,
94189418
) E;
94199419
pub extern "c" fn pthread_key_delete(key: pthread_key_t) E;
94209420
pub extern "c" fn pthread_getspecific(key: pthread_key_t) ?*anyopaque;
@@ -9530,12 +9530,12 @@ pub extern "c" fn pthread_cond_signal(cond: *pthread_cond_t) E;
95309530
pub extern "c" fn pthread_cond_broadcast(cond: *pthread_cond_t) E;
95319531
pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) E;
95329532

9533-
pub extern "c" fn pthread_rwlock_destroy(rwl: *pthread_rwlock_t) callconv(.C) E;
9534-
pub extern "c" fn pthread_rwlock_rdlock(rwl: *pthread_rwlock_t) callconv(.C) E;
9535-
pub extern "c" fn pthread_rwlock_wrlock(rwl: *pthread_rwlock_t) callconv(.C) E;
9536-
pub extern "c" fn pthread_rwlock_tryrdlock(rwl: *pthread_rwlock_t) callconv(.C) E;
9537-
pub extern "c" fn pthread_rwlock_trywrlock(rwl: *pthread_rwlock_t) callconv(.C) E;
9538-
pub extern "c" fn pthread_rwlock_unlock(rwl: *pthread_rwlock_t) callconv(.C) E;
9533+
pub extern "c" fn pthread_rwlock_destroy(rwl: *pthread_rwlock_t) callconv(.c) E;
9534+
pub extern "c" fn pthread_rwlock_rdlock(rwl: *pthread_rwlock_t) callconv(.c) E;
9535+
pub extern "c" fn pthread_rwlock_wrlock(rwl: *pthread_rwlock_t) callconv(.c) E;
9536+
pub extern "c" fn pthread_rwlock_tryrdlock(rwl: *pthread_rwlock_t) callconv(.c) E;
9537+
pub extern "c" fn pthread_rwlock_trywrlock(rwl: *pthread_rwlock_t) callconv(.c) E;
9538+
pub extern "c" fn pthread_rwlock_unlock(rwl: *pthread_rwlock_t) callconv(.c) E;
95399539

95409540
pub const pthread_t = *opaque {};
95419541
pub const FILE = opaque {};

lib/std/c/darwin.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ pub const MACH_MSG_TYPE = enum(mach_msg_type_name_t) {
379379
};
380380

381381
extern "c" var mach_task_self_: mach_port_t;
382-
pub fn mach_task_self() callconv(.C) mach_port_t {
382+
pub fn mach_task_self() callconv(.c) mach_port_t {
383383
return mach_task_self_;
384384
}
385385

@@ -873,7 +873,7 @@ pub const DISPATCH_TIME_FOREVER = ~@as(dispatch_time_t, 0);
873873
pub extern "c" fn dispatch_time(when: dispatch_time_t, delta: i64) dispatch_time_t;
874874

875875
const dispatch_once_t = usize;
876-
const dispatch_function_t = fn (?*anyopaque) callconv(.C) void;
876+
const dispatch_function_t = fn (?*anyopaque) callconv(.c) void;
877877
pub extern fn dispatch_once_f(
878878
predicate: *dispatch_once_t,
879879
context: ?*anyopaque,

lib/std/c/dragonfly.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub const E = enum(u16) {
156156

157157
pub const BADSIG = SIG.ERR;
158158

159-
pub const sig_t = *const fn (i32) callconv(.C) void;
159+
pub const sig_t = *const fn (i32) callconv(.c) void;
160160

161161
pub const cmsghdr = extern struct {
162162
len: socklen_t,

0 commit comments

Comments
 (0)