special-case the lowering of ... catch(err) switch(err)
and if (...) |p| ... else |err| switch(err)
#11957
Labels
accepted
This proposal is planned.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
frontend
Tokenization, parsing, AstGen, Sema, and Liveness.
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
This proposal is mainly concerned about the self-hosted compiler implementation, however it also has some implications for the language specification, discussed at the end.
The idea here is to special-case switching on an error, and lower it in a more compact way:
Status quo ZIR looks something like this. Note the double block; one for the catch and one for the switch:
After this proposal it would be one block whose operand is the error union directly. The "ok" prong is for the non-error case.
That example is specific to the pattern
catch |err| switch (err)
If-error syntax could additionally take advantage of this:
This would lower to the exact same ZIR.
Language Specification Implications
In both of these code examples, this would make the success expression and the error expressions peers. This has implications for peer type resolution as well as result location coercion. It prevents this compile error:
because the success expression participates in the type resolution, and the error expressions end up coercing to a type capable of storing a runtime value.
This is the main motivation for this proposal.
Additional Benefits
This pattern is extremely common in Zig source code, and this change reduces the amount of data passing through most phases of the compiler pipeline, which should be a small performance benefit.
Additionally, the respective AIR code will contain a single switch_br rather than a cond_br and a switch_br, likely resulting in improved machine code of debug builds.
The text was updated successfully, but these errors were encountered: