Skip to content

std.os reorganization; new usingnamespace semantics #9618

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

Merged
merged 28 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3deda15
std.os reorganization, avoiding `usingnamespace`
andrewrk Aug 24, 2021
1bbfcb9
std: reorganize std.c to eliminate `usingnamespace`
andrewrk Aug 25, 2021
c09ba87
std.os.linux: remove the "bits" namespace altogether
andrewrk Aug 25, 2021
7f03cfe
std.os: more reorganization efforts
andrewrk Aug 25, 2021
b781ef4
std.os: fix FILENO constants mapped to wrong values
andrewrk Aug 25, 2021
7884d84
std.os.windows: reorg to avoid `usingnamespace`
andrewrk Aug 26, 2021
c05a20f
std: reorganization that allows new usingnamespace semantics
andrewrk Aug 28, 2021
332eafe
stage2: first pass at implementing usingnamespace
andrewrk Aug 28, 2021
ca21cad
move syntax tests out of behavior tests
andrewrk Aug 30, 2021
a2ff3a1
std, compiler-rt: remove test names where applicable
andrewrk Aug 30, 2021
e41e75a
stage2: update for new usingnamespace semantics
andrewrk Aug 31, 2021
3940a1b
rename std.zig.ast to std.zig.Ast; use top-level fields
andrewrk Aug 31, 2021
cca5704
std: fix regressions from this branch
andrewrk Aug 31, 2021
89dd2b7
translate-c: emit compileError for undefined identifiers in macros
ehaas Aug 31, 2021
8d2acff
disable slow scrypt tests
andrewrk Aug 31, 2021
df589ee
translate-c: improve handling of undefined identifiers
ehaas Aug 31, 2021
f5553bf
translate-c: Only consider public decls in isBuiltinDefined
ehaas Aug 31, 2021
f4d3d29
docgen: syntax blocks don't actually run the code
andrewrk Sep 1, 2021
feec4b0
langref: new usingnamespace semantics
andrewrk Sep 1, 2021
057f0fe
std.os fixes to get the test suite passing again
andrewrk Sep 1, 2021
f8dd4b1
std.os reorg: more fixes caught by CI
andrewrk Sep 1, 2021
5a4cc24
std: dirent is not part of posix
andrewrk Sep 1, 2021
1a492d5
re-apply a commit dropped in this branch due to conflicts
andrewrk Sep 1, 2021
0932b0d
std.os reorg: regression fixes to stack_t, and std.Thread
andrewrk Sep 1, 2021
c0e84e3
translate-c: translate valueless macros as empty strings
ehaas Sep 1, 2021
bbf4c25
translate-c: rename import_builtin to import_c_builtin
ehaas Sep 1, 2021
21171fd
AstGen: update std.zig.{ast,Ast}
andrewrk Sep 1, 2021
2264fca
fix regression on linux with kernel_timespec
andrewrk Sep 2, 2021
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
17 changes: 7 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/crypto/siphash.zig"
"${CMAKE_SOURCE_DIR}/lib/std/debug.zig"
"${CMAKE_SOURCE_DIR}/lib/std/dwarf.zig"
"${CMAKE_SOURCE_DIR}/lib/std/dwarf_bits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/AT.zig"
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/OP.zig"
"${CMAKE_SOURCE_DIR}/lib/std/dwarf/TAG.zig"
"${CMAKE_SOURCE_DIR}/lib/std/elf.zig"
"${CMAKE_SOURCE_DIR}/lib/std/event.zig"
"${CMAKE_SOURCE_DIR}/lib/std/event/batch.zig"
Expand Down Expand Up @@ -424,18 +426,13 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/meta/trait.zig"
"${CMAKE_SOURCE_DIR}/lib/std/multi_array_list.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/errno/generic.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/netlink.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/prctl.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/securebits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/x86_64.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/errno/generic.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/io_uring.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/windows.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/bits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/ntstatus.zig"
"${CMAKE_SOURCE_DIR}/lib/std/os/windows/win32error.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Progress.zig"
Expand Down Expand Up @@ -532,7 +529,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/time.zig"
"${CMAKE_SOURCE_DIR}/lib/std/unicode.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/ast.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/Ast.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/cross_target.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/parse.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig"
Expand Down
2 changes: 1 addition & 1 deletion doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ fn genHtml(
try tokenizeAndPrint(allocator, tokenizer, out, code.source_token);
try out.writeAll("</pre>");

if (!do_code_tests) {
if (!do_code_tests or code.is_inline) {
continue;
}

Expand Down
27 changes: 8 additions & 19 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" {

{#header_open|usingnamespace#}
<p>
{#syntax#}usingnamespace{#endsyntax#} is a declaration that imports all the public declarations of
the operand, which must be a {#link|struct#}, {#link|union#}, or {#link|enum#}, into the current scope:
{#syntax#}usingnamespace{#endsyntax#} is a declaration that mixes all the public
declarations of the operand, which must be a {#link|struct#}, {#link|union#}, {#link|enum#},
or {#link|opaque#}, into the namespace:
</p>
{#code_begin|test|usingnamespace#}
usingnamespace @import("std");

test "using std namespace" {
try testing.expect(true);
}
{#code_end#}
<p>
{#syntax#}usingnamespace{#endsyntax#} can also be used in containers:
</p>
{#code_begin|test|usingnamespace_inside_struct#}
test "using namespace inside struct" {
const L = struct {
usingnamespace struct {
pub fn f() void {}
};
const S = struct {
usingnamespace @import("std");
};
L.f();
try S.testing.expect(true);
}
{#code_end#}
<p>
Instead of the above pattern, it is generally recommended to explicitly alias individual declarations.
However, {#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
{#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
API of a file or package. For example, one might have <code>c.zig</code> with all of the
{#link|C imports|Import from C Header File#}:
</p>
Expand All @@ -5858,6 +5846,7 @@ pub usingnamespace @cImport({
</p>
{#header_close#}


{#header_open|comptime#}
<p>
Zig places importance on the concept of whether an expression is known at compile-time.
Expand Down
18 changes: 9 additions & 9 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ const PosixThreadImpl = struct {
.openbsd => {
var count: c_int = undefined;
var count_size: usize = @sizeOf(c_int);
const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE };
const mib = [_]c_int{ os.CTL.HW, os.system.HW_NCPUONLINE };
os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
error.NameTooLong, error.UnknownName => unreachable,
else => |e| return e,
Expand Down Expand Up @@ -877,8 +877,8 @@ const LinuxThreadImpl = struct {
const mapped = os.mmap(
null,
map_bytes,
os.PROT_NONE,
os.MAP_PRIVATE | os.MAP_ANONYMOUS,
os.PROT.NONE,
os.MAP.PRIVATE | os.MAP.ANONYMOUS,
-1,
0,
) catch |err| switch (err) {
Expand All @@ -893,7 +893,7 @@ const LinuxThreadImpl = struct {
// map everything but the guard page as read/write
os.mprotect(
mapped[guard_offset..],
os.PROT_READ | os.PROT_WRITE,
os.PROT.READ | os.PROT.WRITE,
) catch |err| switch (err) {
error.AccessDenied => unreachable,
else => |e| return e,
Expand Down Expand Up @@ -923,10 +923,10 @@ const LinuxThreadImpl = struct {
.thread = .{ .mapped = mapped },
};

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

switch (linux.getErrno(linux.clone(
Instance.entryFn,
Expand Down Expand Up @@ -978,7 +978,7 @@ const LinuxThreadImpl = struct {

switch (linux.getErrno(linux.futex_wait(
&self.thread.child_tid.value,
linux.FUTEX_WAIT,
linux.FUTEX.WAIT,
tid,
null,
))) {
Expand Down
6 changes: 3 additions & 3 deletions lib/std/Thread/Futex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const LinuxFutex = struct {

switch (linux.getErrno(linux.futex_wait(
@ptrCast(*const i32, ptr),
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT,
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT,
@bitCast(i32, expect),
ts_ptr,
))) {
Expand All @@ -159,7 +159,7 @@ const LinuxFutex = struct {
fn wake(ptr: *const Atomic(u32), num_waiters: u32) void {
switch (linux.getErrno(linux.futex_wake(
@ptrCast(*const i32, ptr),
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE,
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAKE,
std.math.cast(i32, num_waiters) catch std.math.maxInt(i32),
))) {
.SUCCESS => {}, // successful wake up
Expand Down Expand Up @@ -352,7 +352,7 @@ const PosixFutex = struct {
var ts_ptr: ?*const std.os.timespec = null;
if (timeout) |timeout_ns| {
ts_ptr = &ts;
std.os.clock_gettime(std.os.CLOCK_REALTIME, &ts) catch unreachable;
std.os.clock_gettime(std.os.CLOCK.REALTIME, &ts) catch unreachable;
ts.tv_sec += @intCast(@TypeOf(ts.tv_sec), timeout_ns / std.time.ns_per_s);
ts.tv_nsec += @intCast(@TypeOf(ts.tv_nsec), timeout_ns % std.time.ns_per_s);
if (ts.tv_nsec >= std.time.ns_per_s) {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Thread/Mutex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub const AtomicMutex = struct {
.linux => {
switch (linux.getErrno(linux.futex_wait(
@ptrCast(*const i32, &m.state),
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT,
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT,
@enumToInt(new_state),
null,
))) {
Expand All @@ -155,7 +155,7 @@ pub const AtomicMutex = struct {
.linux => {
switch (linux.getErrno(linux.futex_wake(
@ptrCast(*const i32, &m.state),
linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE,
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAKE,
1,
))) {
.SUCCESS => {},
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Thread/ResetEvent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub const PosixEvent = struct {
pub fn timedWait(ev: *PosixEvent, timeout_ns: u64) TimedWaitResult {
var ts: os.timespec = undefined;
var timeout_abs = timeout_ns;
os.clock_gettime(os.CLOCK_REALTIME, &ts) catch return .timed_out;
os.clock_gettime(os.CLOCK.REALTIME, &ts) catch return .timed_out;
timeout_abs += @intCast(u64, ts.tv_sec) * time.ns_per_s;
timeout_abs += @intCast(u64, ts.tv_nsec);
ts.tv_sec = @intCast(@TypeOf(ts.tv_sec), @divFloor(timeout_abs, time.ns_per_s));
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Thread/StaticResetEvent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub const AtomicEvent = struct {
_ = wake_count;
const waiting = std.math.maxInt(i32); // wake_count
const ptr = @ptrCast(*const i32, waiters);
const rc = linux.futex_wake(ptr, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, waiting);
const rc = linux.futex_wake(ptr, linux.FUTEX.WAKE | linux.FUTEX.PRIVATE_FLAG, waiting);
assert(linux.getErrno(rc) == .SUCCESS);
}

Expand All @@ -213,7 +213,7 @@ pub const AtomicEvent = struct {
return;
const expected = @intCast(i32, waiting);
const ptr = @ptrCast(*const i32, waiters);
const rc = linux.futex_wait(ptr, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, expected, ts_ptr);
const rc = linux.futex_wait(ptr, linux.FUTEX.WAIT | linux.FUTEX.PRIVATE_FLAG, expected, ts_ptr);
switch (linux.getErrno(rc)) {
.SUCCESS => continue,
.TIMEDOUT => return error.TimedOut,
Expand Down
28 changes: 15 additions & 13 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,23 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
mem.copy(T, self.items[old_len..], items);
}

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

/// Initializes a Writer which will append to the list.
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
/// Initializes a Writer which will append to the list.
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}

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

/// Append a value to the list `n` times.
/// Allocates more memory as necessary.
Expand Down
2 changes: 1 addition & 1 deletion lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const code_model = builtin.code_model;
/// used rather than `explicit_subsystem`.
/// On non-Windows targets, this is `null`.
pub const subsystem: ?std.Target.SubSystem = blk: {
if (@hasDecl(builtin, "explicit_subsystem")) break :blk explicit_subsystem;
if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem;
switch (os.tag) {
.windows => {
if (is_test) {
Expand Down
Loading