Skip to content

Commit 594271f

Browse files
authored
Merge pull request #9618 from ziglang/std-os-reorg
std.os reorganization; new `usingnamespace` semantics
2 parents 81e2034 + 2264fca commit 594271f

File tree

217 files changed

+26068
-26899
lines changed

Some content is hidden

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

217 files changed

+26068
-26899
lines changed

CMakeLists.txt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ set(ZIG_STAGE2_SOURCES
363363
"${CMAKE_SOURCE_DIR}/lib/std/crypto/siphash.zig"
364364
"${CMAKE_SOURCE_DIR}/lib/std/debug.zig"
365365
"${CMAKE_SOURCE_DIR}/lib/std/dwarf.zig"
366-
"${CMAKE_SOURCE_DIR}/lib/std/dwarf_bits.zig"
366+
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/AT.zig"
367+
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/OP.zig"
368+
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/TAG.zig"
367369
"${CMAKE_SOURCE_DIR}/lib/std/elf.zig"
368370
"${CMAKE_SOURCE_DIR}/lib/std/event.zig"
369371
"${CMAKE_SOURCE_DIR}/lib/std/event/batch.zig"
@@ -424,18 +426,13 @@ set(ZIG_STAGE2_SOURCES
424426
"${CMAKE_SOURCE_DIR}/lib/std/meta/trait.zig"
425427
"${CMAKE_SOURCE_DIR}/lib/std/multi_array_list.zig"
426428
"${CMAKE_SOURCE_DIR}/lib/std/os.zig"
427-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits.zig"
428-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux.zig"
429-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/errno/generic.zig"
430-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/netlink.zig"
431-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/prctl.zig"
432-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/securebits.zig"
433-
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/x86_64.zig"
429+
"${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig"
430+
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/errno/generic.zig"
431+
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig"
434432
"${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig"
435433
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/io_uring.zig"
436434
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig"
437435
"${CMAKE_SOURCE_DIR}/lib/std/os/windows.zig"
438-
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/bits.zig"
439436
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/ntstatus.zig"
440437
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/win32error.zig"
441438
"${CMAKE_SOURCE_DIR}/lib/std/Progress.zig"
@@ -532,7 +529,7 @@ set(ZIG_STAGE2_SOURCES
532529
"${CMAKE_SOURCE_DIR}/lib/std/time.zig"
533530
"${CMAKE_SOURCE_DIR}/lib/std/unicode.zig"
534531
"${CMAKE_SOURCE_DIR}/lib/std/zig.zig"
535-
"${CMAKE_SOURCE_DIR}/lib/std/zig/ast.zig"
532+
"${CMAKE_SOURCE_DIR}/lib/std/zig/Ast.zig"
536533
"${CMAKE_SOURCE_DIR}/lib/std/zig/cross_target.zig"
537534
"${CMAKE_SOURCE_DIR}/lib/std/zig/parse.zig"
538535
"${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig"

doc/docgen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ fn genHtml(
11001100
try tokenizeAndPrint(allocator, tokenizer, out, code.source_token);
11011101
try out.writeAll("</pre>");
11021102

1103-
if (!do_code_tests) {
1103+
if (!do_code_tests or code.is_inline) {
11041104
continue;
11051105
}
11061106

doc/langref.html.in

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" {
58125812

58135813
{#header_open|usingnamespace#}
58145814
<p>
5815-
{#syntax#}usingnamespace{#endsyntax#} is a declaration that imports all the public declarations of
5816-
the operand, which must be a {#link|struct#}, {#link|union#}, or {#link|enum#}, into the current scope:
5815+
{#syntax#}usingnamespace{#endsyntax#} is a declaration that mixes all the public
5816+
declarations of the operand, which must be a {#link|struct#}, {#link|union#}, {#link|enum#},
5817+
or {#link|opaque#}, into the namespace:
58175818
</p>
58185819
{#code_begin|test|usingnamespace#}
5819-
usingnamespace @import("std");
5820-
58215820
test "using std namespace" {
5822-
try testing.expect(true);
5823-
}
5824-
{#code_end#}
5825-
<p>
5826-
{#syntax#}usingnamespace{#endsyntax#} can also be used in containers:
5827-
</p>
5828-
{#code_begin|test|usingnamespace_inside_struct#}
5829-
test "using namespace inside struct" {
5830-
const L = struct {
5831-
usingnamespace struct {
5832-
pub fn f() void {}
5833-
};
5821+
const S = struct {
5822+
usingnamespace @import("std");
58345823
};
5835-
L.f();
5824+
try S.testing.expect(true);
58365825
}
58375826
{#code_end#}
58385827
<p>
5839-
Instead of the above pattern, it is generally recommended to explicitly alias individual declarations.
5840-
However, {#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
5828+
{#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
58415829
API of a file or package. For example, one might have <code>c.zig</code> with all of the
58425830
{#link|C imports|Import from C Header File#}:
58435831
</p>
@@ -5858,6 +5846,7 @@ pub usingnamespace @cImport({
58585846
</p>
58595847
{#header_close#}
58605848

5849+
58615850
{#header_open|comptime#}
58625851
<p>
58635852
Zig places importance on the concept of whether an expression is known at compile-time.

lib/std/Thread.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ const PosixThreadImpl = struct {
558558
.openbsd => {
559559
var count: c_int = undefined;
560560
var count_size: usize = @sizeOf(c_int);
561-
const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE };
561+
const mib = [_]c_int{ os.CTL.HW, os.system.HW_NCPUONLINE };
562562
os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
563563
error.NameTooLong, error.UnknownName => unreachable,
564564
else => |e| return e,
@@ -877,8 +877,8 @@ const LinuxThreadImpl = struct {
877877
const mapped = os.mmap(
878878
null,
879879
map_bytes,
880-
os.PROT_NONE,
881-
os.MAP_PRIVATE | os.MAP_ANONYMOUS,
880+
os.PROT.NONE,
881+
os.MAP.PRIVATE | os.MAP.ANONYMOUS,
882882
-1,
883883
0,
884884
) catch |err| switch (err) {
@@ -893,7 +893,7 @@ const LinuxThreadImpl = struct {
893893
// map everything but the guard page as read/write
894894
os.mprotect(
895895
mapped[guard_offset..],
896-
os.PROT_READ | os.PROT_WRITE,
896+
os.PROT.READ | os.PROT.WRITE,
897897
) catch |err| switch (err) {
898898
error.AccessDenied => unreachable,
899899
else => |e| return e,
@@ -923,10 +923,10 @@ const LinuxThreadImpl = struct {
923923
.thread = .{ .mapped = mapped },
924924
};
925925

926-
const flags: u32 = os.CLONE_THREAD | os.CLONE_DETACHED |
927-
os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES |
928-
os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
929-
os.CLONE_SIGHAND | os.CLONE_SYSVSEM | os.CLONE_SETTLS;
926+
const flags: u32 = linux.CLONE.THREAD | linux.CLONE.DETACHED |
927+
linux.CLONE.VM | linux.CLONE.FS | linux.CLONE.FILES |
928+
linux.CLONE.PARENT_SETTID | linux.CLONE.CHILD_CLEARTID |
929+
linux.CLONE.SIGHAND | linux.CLONE.SYSVSEM | linux.CLONE.SETTLS;
930930

931931
switch (linux.getErrno(linux.clone(
932932
Instance.entryFn,
@@ -978,7 +978,7 @@ const LinuxThreadImpl = struct {
978978

979979
switch (linux.getErrno(linux.futex_wait(
980980
&self.thread.child_tid.value,
981-
linux.FUTEX_WAIT,
981+
linux.FUTEX.WAIT,
982982
tid,
983983
null,
984984
))) {

lib/std/Thread/Futex.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const LinuxFutex = struct {
142142

143143
switch (linux.getErrno(linux.futex_wait(
144144
@ptrCast(*const i32, ptr),
145-
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT,
145+
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT,
146146
@bitCast(i32, expect),
147147
ts_ptr,
148148
))) {
@@ -159,7 +159,7 @@ const LinuxFutex = struct {
159159
fn wake(ptr: *const Atomic(u32), num_waiters: u32) void {
160160
switch (linux.getErrno(linux.futex_wake(
161161
@ptrCast(*const i32, ptr),
162-
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE,
162+
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAKE,
163163
std.math.cast(i32, num_waiters) catch std.math.maxInt(i32),
164164
))) {
165165
.SUCCESS => {}, // successful wake up
@@ -352,7 +352,7 @@ const PosixFutex = struct {
352352
var ts_ptr: ?*const std.os.timespec = null;
353353
if (timeout) |timeout_ns| {
354354
ts_ptr = &ts;
355-
std.os.clock_gettime(std.os.CLOCK_REALTIME, &ts) catch unreachable;
355+
std.os.clock_gettime(std.os.CLOCK.REALTIME, &ts) catch unreachable;
356356
ts.tv_sec += @intCast(@TypeOf(ts.tv_sec), timeout_ns / std.time.ns_per_s);
357357
ts.tv_nsec += @intCast(@TypeOf(ts.tv_nsec), timeout_ns % std.time.ns_per_s);
358358
if (ts.tv_nsec >= std.time.ns_per_s) {

lib/std/Thread/Mutex.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub const AtomicMutex = struct {
133133
.linux => {
134134
switch (linux.getErrno(linux.futex_wait(
135135
@ptrCast(*const i32, &m.state),
136-
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT,
136+
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT,
137137
@enumToInt(new_state),
138138
null,
139139
))) {
@@ -155,7 +155,7 @@ pub const AtomicMutex = struct {
155155
.linux => {
156156
switch (linux.getErrno(linux.futex_wake(
157157
@ptrCast(*const i32, &m.state),
158-
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE,
158+
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAKE,
159159
1,
160160
))) {
161161
.SUCCESS => {},

lib/std/Thread/ResetEvent.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub const PosixEvent = struct {
152152
pub fn timedWait(ev: *PosixEvent, timeout_ns: u64) TimedWaitResult {
153153
var ts: os.timespec = undefined;
154154
var timeout_abs = timeout_ns;
155-
os.clock_gettime(os.CLOCK_REALTIME, &ts) catch return .timed_out;
155+
os.clock_gettime(os.CLOCK.REALTIME, &ts) catch return .timed_out;
156156
timeout_abs += @intCast(u64, ts.tv_sec) * time.ns_per_s;
157157
timeout_abs += @intCast(u64, ts.tv_nsec);
158158
ts.tv_sec = @intCast(@TypeOf(ts.tv_sec), @divFloor(timeout_abs, time.ns_per_s));

lib/std/Thread/StaticResetEvent.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub const AtomicEvent = struct {
194194
_ = wake_count;
195195
const waiting = std.math.maxInt(i32); // wake_count
196196
const ptr = @ptrCast(*const i32, waiters);
197-
const rc = linux.futex_wake(ptr, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, waiting);
197+
const rc = linux.futex_wake(ptr, linux.FUTEX.WAKE | linux.FUTEX.PRIVATE_FLAG, waiting);
198198
assert(linux.getErrno(rc) == .SUCCESS);
199199
}
200200

@@ -213,7 +213,7 @@ pub const AtomicEvent = struct {
213213
return;
214214
const expected = @intCast(i32, waiting);
215215
const ptr = @ptrCast(*const i32, waiters);
216-
const rc = linux.futex_wait(ptr, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, expected, ts_ptr);
216+
const rc = linux.futex_wait(ptr, linux.FUTEX.WAIT | linux.FUTEX.PRIVATE_FLAG, expected, ts_ptr);
217217
switch (linux.getErrno(rc)) {
218218
.SUCCESS => continue,
219219
.TIMEDOUT => return error.TimedOut,

lib/std/array_list.zig

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,23 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
236236
mem.copy(T, self.items[old_len..], items);
237237
}
238238

239-
pub usingnamespace if (T != u8) struct {} else struct {
240-
pub const Writer = std.io.Writer(*Self, error{OutOfMemory}, appendWrite);
239+
pub const Writer = if (T != u8)
240+
@compileError("The Writer interface is only defined for ArrayList(u8) " ++
241+
"but the given type is ArrayList(" ++ @typeName(T) ++ ")")
242+
else
243+
std.io.Writer(*Self, error{OutOfMemory}, appendWrite);
241244

242-
/// Initializes a Writer which will append to the list.
243-
pub fn writer(self: *Self) Writer {
244-
return .{ .context = self };
245-
}
245+
/// Initializes a Writer which will append to the list.
246+
pub fn writer(self: *Self) Writer {
247+
return .{ .context = self };
248+
}
246249

247-
/// Same as `append` except it returns the number of bytes written, which is always the same
248-
/// as `m.len`. The purpose of this function existing is to match `std.io.Writer` API.
249-
fn appendWrite(self: *Self, m: []const u8) !usize {
250-
try self.appendSlice(m);
251-
return m.len;
252-
}
253-
};
250+
/// Same as `append` except it returns the number of bytes written, which is always the same
251+
/// as `m.len`. The purpose of this function existing is to match `std.io.Writer` API.
252+
fn appendWrite(self: *Self, m: []const u8) !usize {
253+
try self.appendSlice(m);
254+
return m.len;
255+
}
254256

255257
/// Append a value to the list `n` times.
256258
/// Allocates more memory as necessary.

lib/std/builtin.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub const code_model = builtin.code_model;
2727
/// used rather than `explicit_subsystem`.
2828
/// On non-Windows targets, this is `null`.
2929
pub const subsystem: ?std.Target.SubSystem = blk: {
30-
if (@hasDecl(builtin, "explicit_subsystem")) break :blk explicit_subsystem;
30+
if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem;
3131
switch (os.tag) {
3232
.windows => {
3333
if (is_test) {

0 commit comments

Comments
 (0)