{"article":{"slug":"partial-operator-precedence","title":"Partial Operator Precedence","subtitle":null,"summary":"Zig implements a decade-old proposal so ambiguous operator combinations require parentheses, cutting the precedence rules programmers need to memorize.","content_type":"changelog","language":"en","canonical_url":"https://ziglang.org/devlog/2026/#2026-10-24","author":{"name":"Ryan Liptak","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Zig Programming Language","url":"https://ziglang.org/","listing_slug":null,"listing":null},"topics":[{"name":"Programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"Systems Programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"},{"name":"Open Source","slug":"open-source","url":"https://listedarticles.com/topics/open-source"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":758,"reading_minutes":3,"published_at":"2026-10-24T00:00:00.000Z","added_at":"2026-11-05T11:11:31.600Z","updated_at":"2026-11-05T11:11:31.600Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/partial-operator-precedence","markdown_url":"https://listedarticles.com/articles/partial-operator-precedence.md","example":false,"citation":"Ryan Liptak, Zig Programming Language. \"Partial Operator Precedence.\" 24 Oct 2026. https://ziglang.org/devlog/2026/#2026-10-24 (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://ziglang.org/devlog/2026/#2026-10-24"},"body_markdown":"# Partial Operator Precedence\n\nOctober 24, 2026\n\n        # Partial Operator Precedence\n\n        Author: Ryan Liptak\n\nAs of yesterday, the second oldest open proposal for the Zig language was [issue #114: require parentheses sometimes to disambiguate confusing operator precedence](https://github.com/ziglang/zig/issues/114), opened February 8, 2016. Ten years later, that proposal [has been implemented](https://codeberg.org/ziglang/zig/pulls/30160) in the master branch version of Zig thanks to contributor [CMDRZero](https://cmdrzero.github.io/).\n\nThis means that some combinations of operators no longer have a defined precedence, and for those that don’t, the compiler will now require parentheses:\n\n```zig\nconst a = x orelse y == z;\n\n```\n\n```zig\ntest.zig:1:22: error: ambiguous operator precedence with operators ... orelse ... == ...; use parentheses to disambiguate\nconst a = x orelse y == z;\n                     ^~\ntest.zig:1:13: note: previous ambiguous operator here\nconst a = x orelse y == z;\n            ^~~~~~\ntest.zig:1:22: note: in zig 0.17.x, this was evaluated as (... orelse ...) == ...\n\n```\n\nHowever, common cases can remain parenthesis-free:\n\n```zig\nconst a = x + y * z;\n\n```\n\nThe idea behind this is to reduce the number of rules a programmer needs to know when reading and writing Zig code. For example, no matter how experienced you are with Zig, it’s unlikely that you’d be able to explain the evaluation order of the following expression without looking up the precedence rules:\n\n```zig\na & b + c * d >> e | f orelse g() catch h + i == j or k and l\n\n```\n\nWith the now-required parentheses added (while maintaining the same evaluation order as the Zig 0.17.x precedence rules), that expression would look like this:\n\n```zig\n((((a & ((b + c * d) >> e)) | f) orelse g()) catch (h + i)) == j or (k and l);\n\n```\n\nStill quite hairy, and not something I’d like to see in the wild, but nonetheless it’s understandable without knowing any precedence rules beyond arithmetic and comparison. Outside of this contrived example, though, the expressions that will now require parentheses are relatively rare (353 lines needed changes out of the million+ lines of code in the Zig codebase).\n\n## What you need to know\n\nIn practice, going forward, you will only need to remember the rules around operator combinations that still have a defined/unambiguous precedence. For example, when reading or writing code involving arithmetic operators, you’ll only need to remember the following precedence table:\n\n```zig\n* / *% *| %\n+ - +% -% +| -|\n== != < > <= >=\nand or\n\n```\n\nYou might notice that there isn’t anything too surprising there, and that lack of surprise was a (subjective) guiding principle for what should and should not require parentheses. As an example, while % has unambiguous precedence with regards to addition/subtraction operators, parentheses must be used when mixing % with multiplication/division operators, and with itself. However, this, and other ambiguous operator combinations, aren’t something you’ll need to commit to memory. Any (future) code you read will already have parentheses to disambiguate, and for any code you write, the compiler will let you know when you need parentheses.\n\nFor the full explanation, and to see the relevant precedence tables pertaining to other operators, see [the updated Precedence section of the language reference](https://ziglang.org/documentation/master/#Precedence).\n\n## Upgrading existing code\n\nThere are two possible approaches here:\n\n- Manually fix each “ambiguous operator precedence” error as they are surfaced by the compiler (this might be a good opportunity to try out [incremental compilation](https://ziglang.org/download/0.17.0/release-notes.html#Incremental-Compilation) if it’s supported on your platform)\n- Use zig fmt --upgrade (the --upgrade flag was added for this purpose). This will cause zig fmt to add parentheses automatically, maintaining the evaluation order from the previous precedence rules (note: the --upgrade flag will be removed after one release cycle)\n\nMy personal recommendation would be option 1, as the process of redetermining the intended evaluation order for these uncommon precedence scenarios can occasionally serve to uncover bugs ([here’s an example of that](https://github.com/Vexu/arocc/pull/1117)).\n\nIf you go with option 2, I’d still recommend perusing the diff afterwards to ensure that nothing looks unusual, or is different than you expected/intended. A potentially common example of that is something like a orelse b() catch c which previously evaluated to (a orelse b()) catch c, but changing that to a orelse (b() catch c) likely makes more sense, even if the end result is unaffected. Also worth noting that in some scenarios, redundant parentheses will be added by zig fmt --upgrade (e.g. you might see something like a orelse ((b + c) - d) where the parentheses around b + c are unnecessary), so avoiding that will take a bit of manual cleanup.\n\nThat’s about it. I’m hopeful that this change will make bugs stemming from misremembered precedence rules much less likely, and that it will put some attention onto any of those bugs that may currently exist.\n\nThanks again to CMDRZero for the implementation, and to Isaac Freund for help with the corresponding grammar changes.","body_html":"<h1 id=\"partial-operator-precedence\">Partial Operator Precedence</h1>\n<p>October 24, 2026</p>\n<pre><code>    # Partial Operator Precedence\n\n    Author: Ryan Liptak</code></pre>\n<p>As of yesterday, the second oldest open proposal for the Zig language was <a href=\"https://github.com/ziglang/zig/issues/114\" rel=\"nofollow ugc noopener\">issue #114: require parentheses sometimes to disambiguate confusing operator precedence</a>, opened February 8, 2016. Ten years later, that proposal <a href=\"https://codeberg.org/ziglang/zig/pulls/30160\" rel=\"nofollow ugc noopener\">has been implemented</a> in the master branch version of Zig thanks to contributor <a href=\"https://cmdrzero.github.io/\" rel=\"nofollow ugc noopener\">CMDRZero</a>.</p>\n<p>This means that some combinations of operators no longer have a defined precedence, and for those that don’t, the compiler will now require parentheses:</p>\n<pre><code class=\"language-zig\">const a = x orelse y == z;\n</code></pre>\n<pre><code class=\"language-zig\">test.zig:1:22: error: ambiguous operator precedence with operators ... orelse ... == ...; use parentheses to disambiguate\nconst a = x orelse y == z;\n                     ^~\ntest.zig:1:13: note: previous ambiguous operator here\nconst a = x orelse y == z;\n            ^~~~~~\ntest.zig:1:22: note: in zig 0.17.x, this was evaluated as (... orelse ...) == ...\n</code></pre>\n<p>However, common cases can remain parenthesis-free:</p>\n<pre><code class=\"language-zig\">const a = x + y * z;\n</code></pre>\n<p>The idea behind this is to reduce the number of rules a programmer needs to know when reading and writing Zig code. For example, no matter how experienced you are with Zig, it’s unlikely that you’d be able to explain the evaluation order of the following expression without looking up the precedence rules:</p>\n<pre><code class=\"language-zig\">a &amp; b + c * d &gt;&gt; e | f orelse g() catch h + i == j or k and l\n</code></pre>\n<p>With the now-required parentheses added (while maintaining the same evaluation order as the Zig 0.17.x precedence rules), that expression would look like this:</p>\n<pre><code class=\"language-zig\">((((a &amp; ((b + c * d) &gt;&gt; e)) | f) orelse g()) catch (h + i)) == j or (k and l);\n</code></pre>\n<p>Still quite hairy, and not something I’d like to see in the wild, but nonetheless it’s understandable without knowing any precedence rules beyond arithmetic and comparison. Outside of this contrived example, though, the expressions that will now require parentheses are relatively rare (353 lines needed changes out of the million+ lines of code in the Zig codebase).</p>\n<h2 id=\"what-you-need-to-know\">What you need to know</h2>\n<p>In practice, going forward, you will only need to remember the rules around operator combinations that still have a defined/unambiguous precedence. For example, when reading or writing code involving arithmetic operators, you’ll only need to remember the following precedence table:</p>\n<pre><code class=\"language-zig\">* / *% *| %\n+ - +% -% +| -|\n== != &lt; &gt; &lt;= &gt;=\nand or\n</code></pre>\n<p>You might notice that there isn’t anything too surprising there, and that lack of surprise was a (subjective) guiding principle for what should and should not require parentheses. As an example, while % has unambiguous precedence with regards to addition/subtraction operators, parentheses must be used when mixing % with multiplication/division operators, and with itself. However, this, and other ambiguous operator combinations, aren’t something you’ll need to commit to memory. Any (future) code you read will already have parentheses to disambiguate, and for any code you write, the compiler will let you know when you need parentheses.</p>\n<p>For the full explanation, and to see the relevant precedence tables pertaining to other operators, see <a href=\"https://ziglang.org/documentation/master/#Precedence\" rel=\"nofollow ugc noopener\">the updated Precedence section of the language reference</a>.</p>\n<h2 id=\"upgrading-existing-code\">Upgrading existing code</h2>\n<p>There are two possible approaches here:</p>\n<ul><li>Manually fix each “ambiguous operator precedence” error as they are surfaced by the compiler (this might be a good opportunity to try out <a href=\"https://ziglang.org/download/0.17.0/release-notes.html#Incremental-Compilation\" rel=\"nofollow ugc noopener\">incremental compilation</a> if it’s supported on your platform)</li><li>Use zig fmt --upgrade (the --upgrade flag was added for this purpose). This will cause zig fmt to add parentheses automatically, maintaining the evaluation order from the previous precedence rules (note: the --upgrade flag will be removed after one release cycle)</li></ul>\n<p>My personal recommendation would be option 1, as the process of redetermining the intended evaluation order for these uncommon precedence scenarios can occasionally serve to uncover bugs (<a href=\"https://github.com/Vexu/arocc/pull/1117\" rel=\"nofollow ugc noopener\">here’s an example of that</a>).</p>\n<p>If you go with option 2, I’d still recommend perusing the diff afterwards to ensure that nothing looks unusual, or is different than you expected/intended. A potentially common example of that is something like a orelse b() catch c which previously evaluated to (a orelse b()) catch c, but changing that to a orelse (b() catch c) likely makes more sense, even if the end result is unaffected. Also worth noting that in some scenarios, redundant parentheses will be added by zig fmt --upgrade (e.g. you might see something like a orelse ((b + c) - d) where the parentheses around b + c are unnecessary), so avoiding that will take a bit of manual cleanup.</p>\n<p>That’s about it. I’m hopeful that this change will make bugs stemming from misremembered precedence rules much less likely, and that it will put some attention onto any of those bugs that may currently exist.</p>\n<p>Thanks again to CMDRZero for the implementation, and to Isaac Freund for help with the corresponding grammar changes.</p>","headings":[{"level":1,"text":"Partial Operator Precedence","id":"partial-operator-precedence"},{"level":2,"text":"What you need to know","id":"what-you-need-to-know"},{"level":2,"text":"Upgrading existing code","id":"upgrading-existing-code"}]}}