iTranslated by AI
Zig v0.15.1 Release
Introduction
There's a Japanese saying that the heat and cold end by the equinox, and our beloved equinox is finally approaching. If it doesn't get cooler by the equinox, I plan to sue the Japan Meteorological Agency. I'm prepared to take it all the way to the Supreme Court. Speaking of things everyone loves, Zig 0.15.1 was released on August 19, 2025. For some reason, the 0.15.0 release was canceled, apparently.
Since Zig hasn't reached version 1.0 yet, this minor version update also includes numerous breaking changes. This article picks up the changes in 0.15.1 that caught my attention, focusing especially on the points I struggled with when updating my own project from 0.14.1. For a detailed list of changes, please refer to the release notes.
Removal of usingnamespace
Compared to C/C++, I think Zig is a language that is "easy to read when viewed in a text editor." This is reflected in one of Zig's zen: "Favor reading code over writing code." For example, it makes it immediately clear where a constant or variable comes from. Consider the following C++ code; it's not immediately obvious where the symbol do_something originates:
#include <a.hpp>
#include <b.hpp>
#include "saikyo.hpp"
void main() {
do_something();
}
You need to check all included files, and it's unclear where the header files exist (they could be in the same directory, in an include directory, or on a custom include path).
On the other hand, in Zig, the origin of a definition is clear:
const a = @import("a.zig");
const b = a.b;
const do_something = b.do_something;
fn main() void {
do_something();
}
In Zig, files are implicitly treated as structs, and symbols defined in a file can be referenced as members of the struct. This means that the definition of a symbol can be found by following a chain of struct member references, making it very easy to trace for both humans and machines.
However, up until 0.14.1, there was an exception: usingnamespace. This looks like black magic; it brings all members of a specified struct into the current scope:
const S = struct {
usingnamespace @import("std");
};
Because this undermined the traceability of symbols that Zig provides, it was removed in 0.15.1.
I personally used it as an umbrella code to export architecture-specific APIs in my custom OS, like this:
// arch.zig
pub usingnamespace switch (builtin.target.cpu.arch) {
.x86_64 => @import("arch/x86/arch.zig"),
else => unreachable,
};
// When using
const arch = @import("arch.zig");
In 0.15.1, this code requires an extra constant definition:
// arch.zig
pub const impl = switch (builtin.target.cpu.arch) {
.x86_64 => @import("arch/x86/arch.zig"),
else => unreachable,
};
// When using
const arch = @import("arch.zig").impl;
The release notes acknowledge that some uses of usingnamespace were legitimate, and provide a migration guide for each use case. If you're struggling with how to get rid of usingnamespace, please check it out.
Ziggify of std.os.uefi
Although there is no mention in the release notes, there are massive breaking changes under std.os.uefi. Many of these changes are part of an effort to make the APIs more Zig-like, moving away from directly using the C APIs defined in the UEFI Specification. For example, traditionally EFI APIs returned a Status value, and the actual return value was provided through an output parameter variable:
// https://github.com/ziglang/zig/blob/6d1f0eca773e688c802e441589495b7bde2f9e3f/lib/std/os/uefi/protocol/file.zig#L55-L57
pub fn read(self: *const File, buffer_size: *usize, buffer: [*]u8) Status {
return self._read(self, buffer_size, buffer);
}
In 0.15.1, this now returns an Error Union instead of Status, with the output being returned as a value:
// https://github.com/ziglang/zig/blob/cbc3c0dc594341faddb8841fb466254f2f6f3b35/lib/std/os/uefi/protocol/file.zig#L130-L140
pub fn read(self: *File, buffer: []u8) ReadError!usize {
var size: usize = buffer.len;
switch (self._read(self, &size, buffer.ptr)) {
...
}
}
The latter is much more Zig-like, which is great. Other changes include defining types that were previously simple integers as enum, and grouping complex input/output variable types into structs. Most of the UEFI API is affected. Personally, this change required the most effort when updating to 0.15.1. Note that since the UEFI platform was removed from the Tier support in the 0.14.0 release, there is no mention of this change in the release notes.
Integer Types Changed to enum
As seen in the UEFI API example earlier, several std APIs that previously took integer types like usize or u64 now take dedicated enum types. For example, the alignment parameter of std.mem.Allocator.alignedAlloc() has changed from u29 to Alignment:
pub fn alignedAlloc(
self: Allocator,
comptime T: type,
/// null means naturally aligned
comptime alignment: ?Alignment,
n: usize,
) Error![]align(if (alignment) |a| a.toByteUnits() else @alignOf(T)) T
Alignment is represented as a non-exhaustive enum:
pub const Alignment = enum(math.Log2Int(usize)) {
@"1" = 0,
@"2" = 1,
@"4" = 2,
@"8" = 3,
@"16" = 4,
@"32" = 5,
@"64" = 6,
_,
...
};
However, for some reason, it only defines values up to 64. Since alignedAlloc() accepts alignments less than 4096 in its implementation, it's a mystery why they stopped at 64. Being non-exhaustive, you can still pass 128 as an alignment like this:
allocator.alignedAlloc(u8, @enumFromInt(128), 100);
Cast Order Change by zig fmt
Previously, when using @alignCast() and @ptrCast() together, zig fmt enforced the following order of casts:
@alignCast(@ptrCast(pointer))
In 0.15.1, @alignCast comes first:
@ptrCast(@alignCast(pointer))
This is a trivial change, and the formatter handles it automatically, so it's not a big deal.
switch Now Allows Combining else/_ Prongs
Zig's enum supports non-exhaustive enums, which don't require listing all possible values:
pub const Color = enum(u8) {
red = 0,
blue = 1,
green = 2,
_, // <=== non-exhaustive
};
This allows converting any u8 value to Color using @enumFromInt(). For exhaustive enums, trying to convert an integer with no corresponding tag, like @as(Color, @enumFromInt(3)), results in an error.
Now, when handling enum with switch, you can use else and _. else represents "everything not explicitly listed," while _ catches "all values without a tag name":
switch (color) {
.red, .blue => {},
else => {}, // <== .green and all values without tag names
}
switch (color) {
.red, .blue => {},
_ => {}, // <== all values without tag names. .green is not caught, causing a compile error
}
Previously, you couldn't use both else and _ in a single switch. Starting from 0.15.1, this is now possible. This allows separate handling of "all other values with tag names" and "all values without tag names":
switch (color) {
.red, .blue => {},
else => {}, // <== green
_ => {}, // all values without tag names
}
This is a feature I've wanted for a while, so I'm genuinely pleased.
Clobber Syntax in Inline Assembly
Previously, inline assembly clobbers were written as strings, similar to C/C++:
asm volatile (
\\pushfq
\\pop %[rflags]
: [rflags] "=r" (-> u64),
:
: "memory", "cc", "rflags"
);
From 0.15.1, clobbers are specified using anonymous structs:
asm volatile (
\\pushfq
\\pop %[rflags]
: [rflags] "=r" (-> u64),
:
: .{ .memory = true, .cc = true, .rflags = true }
);
Since it's now typed, you can no longer accidentally specify a non-existent clobber. This change is handled automatically by zig fmt, making it very convenient. Helpful.
Native Backend for Debug Builds
This was already available in 0.14.1, but starting from 0.15.1, the native backend is used by default for Debug builds on x64 environments. The Zig project has long stated its desire to move away from LLVM, so this change was expected. Personally, I have mixed feelings about using completely different backends for Debug and Release builds. However, since the native backend is reportedly about 5x faster for build times, I'd like to use it if possible.
However, for low-level projects like a custom OS, the native backend won't compile immediately; my project shows nearly one hundred errors. There are particularly many syntax errors for unsupported constructs in inline assembly.
For now, if you want to continue using the LLVM backend, just specify .use_llvm in the options passed to addExecutable() etc.:
const surtr = b.addExecutable(.{
.name = "BOOTX64.EFI",
.root_module = b.createModule(.{
...
}),
.linkage = .static,
.use_llvm = true,
});
Unless critical issues are found, I plan to migrate to the native backend for both Debug and Release builds as soon as possible. After all, the default for Release builds will probably switch to the native backend eventually anyway.
Additional Format Specifiers
The following new format specifiers have been added:
-
{t}:@tagName()or@errorName() -
{b64}: Output as base64. Who uses that?
This means the previously convenient {?} specifier + Error Type can no longer be used:
std.log.debug("{?}", .{err});
In 0.15.1, you write it like this:
std.log.debug("{t}", .{err});
std.log.debug("{s}", .{@errorName(err)});
std.ArrayList Becomes Unmanaged
One of Zig's strengths is the absence of implicit memory allocation. Nearly all APIs in the standard library that require memory allocation take a std.mem.Allocator as a parameter and use it to allocate memory.
On the other hand, when creating a struct, there were previously two approaches. The first is Unmanaged, where struct methods that allocate memory always take an allocator parameter as a rule. The second is Managed, where the allocator is taken only once during struct initialization, stored as a member variable, and reused thereafter.
Previously, std.ArrayList took the second, Managed approach:
pub fn init(allocator: Allocator) Self {
return Self{
.items = &[_]T{},
.capacity = 0,
.allocator = allocator,
};
}
From 0.15.1, the first approach, Unmanaged, is the default. Additionally, the way it is exported from std has changed; instead of std.ArrayList, you now access it as std.array_list.Aligned. The old Managed type std.array_list.Managed still exists but is deprecated and slated for future removal, so it's best not to use it.
Old Managed code can be rewritten as follows:
const list: std.array_list.Aligned(u8, null) = .empty;
try list.append(allocator, some);
list.deinit(allocator);
Note that the non-Aligned type has been removed, so you need to pass an alignment as the second argument. If you don't care, passing null will automatically use @alignOf(@TypeOf(arg)) as the alignment. .empty is the equivalent of the old .init() function, defined as a constant on the Aligned struct. Here it is written using decl literals.
I have mixed feelings about Unmanaged becoming the default (or rather, the only method, considering deprecation). The release notes discuss the pros and cons, concluding that "the benefits of Managed didn't outweigh the downsides." However, passing the allocator every time also introduces the risk of passing the wrong allocator, which seems like a significant drawback. I'm not sure if the hassle and risk of passing the allocator every time is worth saving just 16 bytes. But maybe once you get used to it, it turns out to be better, or maybe not.
DoublyLinkedList Becomes Intrusive
The old std.DoublyLinkedList had first / last pointer members, which held pointers to Node types. Node is a header that wraps the target data type and carries prev/next pointers for list linkage:
pub fn DoublyLinkedList(comptime T: type) type {
return struct {
const Self = @This();
/// Node inside the linked list wrapping the actual data.
pub const Node = struct {
prev: ?*Node = null,
next: ?*Node = null,
data: T,
};
first: ?*Node = null,
last: ?*Node = null,
len: usize = 0,
...
};
}
In 0.15.1, Node no longer holds the target data type; it simply has prev and next pointers:
first: ?*Node = null,
last: ?*Node = null,
pub const Node = struct {
prev: ?*Node = null,
next: ?*Node = null,
};
This means the list header Node is now embedded directly into the target data type, making it an intrusive list:
const SomeData = struct {
head: DoublyLinkedList.Node,
data: u64,
};
It's roughly similar to Linux's list_head type. I had been wanting an intrusive linked list myself and had implemented one, so this change doesn't feel particularly odd.
However, since the DoublyLinkedList API returns Node instead of the data type, having to repeatedly use @fieldParentPtr() to get the data is a bit cumbersome. Also, the len field has been removed, replaced by a len() method. This len() traverses the entire list from start to end, taking O(N) time to count nodes. They probably wanted to save 8 bytes, but how did it come to this... Overall, 0.15.1 seems to have many changes that slim down structs in the std library while making users consciously manage the internal structure of those structs.
Minor Call Convention Fix
.Win64, which represented the Windows calling convention, has been changed to .winapi or .{.x86_64_win = .{}}. .winapi is a convenience constant that automatically selects the appropriate tag based on the target architecture:
pub const winapi: CallingConvention = switch (builtin.target.cpu.arch) {
.x86_64 => .{ .x86_64_win = .{} },
.x86 => .{ .x86_stdcall = .{} },
.aarch64 => .{ .aarch64_aapcs_win = .{} },
.thumb => .{ .arm_aapcs_vfp = .{} },
else => unreachable,
};
Writergate
All existing std.io.Reader/Writer have been removed. Truly interesting.
Instead, new types std.Io.Reader/Writer have been introduced. Notice the capital I. For migration from the old reader/writer, the old ones have been given an adaptToNewApi() method that creates a new reader/writer. But they will probably be removed at some point. Probably.
Honestly, I haven't used std.io.Reader/Writer much, so I don't know the detailed changes. From my experience migrating a few Reader/Writers in my own project to the new API, I'd say the method names in the new API's vtable are a bit unintuitive. For example, the most commonly used Writer method is called drain():
drain: *const fn (w: *Writer, data: []const []const u8, splat: usize) Error!usize,
Looking at the doc comment, it's a function that flushes when the internal buffer of the Writer can't hold the data, but I can't help thinking there could have been a better name. Since I've only implemented unbuffered Writers, I don't fully understand why data is a double array or what splat does.
Incidentally, with this change, the APIs in std.elf now take a std.fs.File.Reader as a parameter (previously they took anytype). fs.File.Reader internally holds a std.Io.Reader:
pub const Reader = struct {
file: File,
err: ?ReadError = null,
mode: Reader.Mode = .positional,
/// Tracks the true seek position in the file. To obtain the logical
/// position, use `logicalPos`.
pos: u64 = 0,
size: ?u64 = null,
size_err: ?SizeError = null,
seek_err: ?Reader.SeekError = null,
interface: std.Io.Reader,
...
};
As you can see from the File parameter, this can only be used on platforms with a POSIX implementation. This means that the stream-based ELF parser, which could previously be used in freestanding environments, can now only be used on POSIX. How did it come to this...
There is an API provided for parsing the entire ELF image by reading it all into a buffer first. However, the old stream-based parser that loaded and parsed only the necessary parts on demand can no longer be used in UEFI environments.
Outro
For better or worse, this release strongly reflects a prioritization of optimization and memory resources. The name "Writergate" seems to be coined in analogy to the Allocgate scandal in v0.9.0, where all APIs taking *Allocator as a parameter were replaced with those taking Allocator. I wasn't using Zig at the time of 0.9.0, so I don't know about it, and as for this Writergate, I haven't used Reader/Writer that much to truly feel the impact.
I think it's fine to let the breaking changes happen freely until the 1.0 release, and each time, based on the increasingly prominent Zig philosophy, decide whether to continue using the language. By the way, I also like hardtack and matcha.
Discussion
以下のリンクを本文に載せておくと救われる人が現れるかも。