NewsCrawler.org

Zig now requires parentheses for ambiguous operator precedence

Zig's second oldest open language proposal, issue #114 from February 8, 2016, has been implemented in the master branch by contributor CMDRZero, ten years after it was filed. Some operator combinations, such as `orelse`[1] mixed with `==`, no longer have a defined precedence, and the compiler requires parentheses: `x orelse y == z` now fails with an "ambiguous operator precedence" error, while common cases like `x + y * z` remain parenthesis-free. The goal is to reduce the number of precedence rules a programmer needs to remember, and only 353 lines needed changes out of the million+ lines of code in the Zig codebase.

Existing code can be fixed manually — the author recommends this, since redetermining the intended evaluation order can occasionally uncover bugs — or with the new --upgrade flag of zig fmt[2], which adds parentheses while preserving the evaluation order of Zig 0.17.x. The flag will be removed after one release cycle, can insert redundant parentheses, and rewrites `a orelse b() catch c` from `(a orelse b()) catch c` to `a orelse (b() catch c)`, so the resulting diff is worth reviewing. Isaac Freund helped with the corresponding grammar changes.

Zig also adds support for specifying version and paths for naked packages[3] — packages without a build.zig.zon[4] manifest, typically maintained by third parties outside the Zig ecosystem — next to the URL. Their old hashes were brittle because they included every fetched file without a filter, so stray hidden files could change the hash and symlinks unsupported on some OS made fetching fail. The dependency name now enters the hash only when a version is provided, letting users opt in, and an updated zlib example shrinks from 4.3M to 1.2M. A new consistency check requires that the same package, identified by fingerprint, with an equal semantic version have an identical hash, otherwise there is a package ownership dispute, an unbumped version after changes, or a dependency on unreleased changes.

Read the original ziglang.orgDiscussion

Discussion

Commenters welcome the change and point to prior art: one first saw the idea in Pony and adopted it for RCL after a production SQL incident caused by unexpected or/and precedence, and another had filed a similar, well-received proposal for Hare.

Sources

Published: First seen: Читать по-русски

Open as a page