ziglang/zig
reduce
Active contributors: andrewrk
Purpose
lib/compiler/reduce.zig (~17 KB) is a Zig source minimizer. It backs zig reduce. Given an input .zig file plus an interestingness script (a program that exits 0 when the file still reproduces the bug), it shrinks the source while preserving the property — the same idea as creduce/cvise for C, but for Zig.
How to use it
zig reduce --interesting ./check.sh -- input.zig
# After many iterations, input.zig is rewritten to a smaller form
# that still satisfies ./check.sh.The "interestingness" script is anything that returns 0 to mean "still reproduces the bug" and non-zero otherwise. A typical script invokes zig with the failure-causing flags and greps the output for the bug signature.
Implementation sketch
The reducer walks the AST (via lib/std/zig/Ast.zig) and applies a battery of transformations:
- Removing top-level declarations.
- Replacing function bodies with
unreachableor trivial returns. - Removing block statements, if branches, loops.
- Inlining single-use bindings.
- Replacing complex literals with simpler ones.
After each transformation candidate, it re-runs the interestingness script. Successful shrinks are kept; unsuccessful ones are reverted.
Integration points
std.zig.Ast— the AST it edits.std.zig.Parse— re-parsing after each transformation.std.Process— running the interestingness script.
Key source files
| File | Purpose |
|---|---|
lib/compiler/reduce.zig |
The reducer. |
lib/std/zig/Ast.zig |
AST reader/writer used for transformations. |
See Debugging for when to reach for zig reduce.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.