mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
This change extends the "lifetime" of the error return trace associated
with an error to continue throughout the block of a `const` variable
that it is assigned to.
This is necessary to support patterns like this one in test_runner.zig:
```zig
const result = foo();
if (result) |_| {
// ... success logic
} else |err| {
// `foo()` should be included in the error trace here
return error.TestFailed;
}
```
To make this happen, the majority of the error return trace popping logic
needed to move into Sema, since `const x = foo();` cannot be examined
syntactically to determine whether it modifies the error return trace. We
also have to make sure not to delete pertinent block information before it
makes it to Sema, so that Sema can pop/restore around blocks correctly.
* Why do this only for `const` and not `var`? *
There is room to relax things for `var`, but only a little bit. We could
do the same thing we do for const and keep the error trace alive for the
remainder of the block where the *assignment* happens. Any wider scope
would violate the stack discipline for traces, so it's not viable.
In the end, I decided the most consistent behavior for the user is just
to kill all error return traces assigned to a mutable `var`.
|
||
|---|---|---|
| .. | ||
| behavior | ||
| c_abi | ||
| cases | ||
| link | ||
| src | ||
| stage2 | ||
| standalone | ||
| assemble_and_link.zig | ||
| behavior.zig | ||
| cases.zig | ||
| cli.zig | ||
| compare_output.zig | ||
| compile_errors.zig | ||
| gen_h.zig | ||
| link.zig | ||
| run_translated_c.zig | ||
| stack_traces.zig | ||
| standalone.zig | ||
| tests.zig | ||
| translate_c.zig | ||