Fixing the Zig compiler

After writing my previous article, I found that someone had already fixed the Zig compiler using this simple trick that the Zig zealots hate, but it isn’t updated to the latest version. That’s okay though, because the changes are small enough that we could set it up ourselves.

Install some prerequisites to be able to build the Zig compiler, here using Ubuntu Jammy:

apt install lsb-release wget software-properties-common gnupg
bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
apt install cmake clang clang-tools libclang-17-dev liblld-17 liblld-17-dev

Clone Zig, and edit the relevant source file

git clone https://github.com/ziglang/zig.git
cd zig
vim src/AstGen.zig +2929

You want to go to the definition of the checkUsed function, which is currently on line 2929:

fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void {
    const astgen = gz.astgen;

    var scope = inner_scope;
    while (scope != outer_scope) {

To make the least amount of changes, dummy the whole thing out.

fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void {
    if (true) {
        return;
    }
    const astgen = gz.astgen;

    var scope = inner_scope;
    while (scope != outer_scope) {

That’s all you need. On to compiling:

mkdir build
cd build
cmake .. 
make install

The binary will end up as build/stage3/bin/zig, so you may symlink this as zig-sloppy or whatever you want.

Try to run the following file which would make the standard compiler explode:

const std = @import("std");

pub fn main() !void {
    const unused = 42;
    std.debug.print("Unused variable above!\n", .{});
    do_stuff(39);
}

fn do_stuff(asdf: i32) void {
    std.debug.print("We don't use that parameter here!\n!", .{});
}

Congratulations, you have now implemented your own --sloppy mode for Zig. From following this short article you have been able to accomplish what the Zig devs have been unable to for years.


Comments