builtin.mode is deprecated: read builtin.optimize
CI checks the runnable examples on this page every night, and they currently run on Zig 0.18.0-dev.1272+2ff397f75. Nothing checks the plain code blocks, and they may not compile now.
On 25 September, five files in this guide changed. All five read a value from @import("builtin").
The code still compiled. But Zig master marked the names in those files as deprecated.
The build of this guide fails on a deprecated name. Thus the guide must change a deprecated name before Zig removes the name. This post shows the change.
The old names
A lot of Zig code has this shape:
const builtin = @import("builtin");
const safety_on = switch (builtin.mode) {
.debug, .safe => true,
.fast, .small => false,
};
const arch = builtin.cpu.arch;
const os = builtin.os.tag;
Three names in this code are now deprecated: builtin.mode, builtin.cpu and builtin.os.
The new names
The name for the build mode is builtin.optimize.
The target is one value, builtin.target. The CPU and the OS are fields of that value.
const builtin = @import("builtin");
const safety_on = switch (builtin.optimize) {
.debug, .safe => true,
.fast, .small => false,
};
const arch = builtin.target.cpu.arch;
const os = builtin.target.os.tag;
The switch did not change. Only the name in front of the switch changed.
The full list:
| Old | New |
|---|---|
builtin.mode | builtin.optimize |
builtin.cpu | builtin.target.cpu |
builtin.os | builtin.target.os |
builtin.abi | builtin.target.abi |
builtin.object_format | builtin.target.ofmt |
Examine your own compiler
You do not have to trust this post.
The compiler generates the builtin module for each build, and Zig can show the module to you:
zig build-exe --show-builtin
On Zig 0.17.0-dev.2056, these are the important lines:
/// Deprecated; to be removed in 0.18.0. Use `target.cpu` instead.
pub const cpu: std.Target.Cpu = .{
/// Deprecated, to be removed after 0.18.0
pub const mode = optimize;
pub const optimize: std.lang.Optimize = .debug;
Thus mode is now only a second name for optimize. For this reason, the old code still compiles today. The old code will not compile when Zig removes the old names.
If your compiler shows no optimize line, your compiler is older than this change. In that case, builtin.mode is still the correct name.
The new form, in a program that runs
This program is one of the five files that changed.
The program runs in your browser. Each night, CI examines the program against Zig master. Thus the program stays correct when this post is old.
const std = @import("std");
const builtin = @import("builtin");
pub fn main(init: std.process.Init) !void {
var buf: [256]u8 = undefined;
var fw = std.Io.File.stdout().writerStreaming(init.io, &buf);
const out = &fw.interface;
try out.print("cpu arch: {t}\n", .{builtin.target.cpu.arch});
try out.print("os tag: {t}\n", .{builtin.target.os.tag});
try out.print("pointer: {d} bits\n", .{@bitSizeOf(usize)});
try out.flush();
}Why the problem shows late
The compiler gives no warning when you use these names. The comment in the --show-builtin output is the only notice.
Thus a project can use builtin.mode for a long time and nothing fails. Then, on the day that Zig removes the name, all of those uses fail at the same time.
On each build, this repository examines every snippet and every chapter for deprecated names. How this guide is verified describes the other checks.
The Build Modes chapter now uses builtin.optimize. The chapter also shows that -O ReleaseFast and -O fast make the same build.