Skip to content

Commit f930f8a

Browse files
committed
AstGen: Pop error trace for continue
PR ziglang#12837 handled control flow for break and return, but I forgot about `continue`. This is effectively another break, so we just need another `.restore_err_ret_index` ZIR instruction. Resolves ziglang#13618.
1 parent 7c527c6 commit f930f8a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/AstGen.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,11 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
20712071
if (break_tag == .break_inline) {
20722072
_ = try parent_gz.addUnNode(.check_comptime_control_flow, Zir.indexToRef(continue_block), node);
20732073
}
2074+
2075+
// As our last action before the continue, "pop" the error trace if needed
2076+
if (!gen_zir.force_comptime)
2077+
_ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always);
2078+
20742079
_ = try parent_gz.addBreak(break_tag, continue_block, .void_value);
20752080
return Zir.Inst.Ref.unreachable_value;
20762081
},

test/stack_traces.zig

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,57 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
151151
},
152152
});
153153

154+
cases.addCase(.{
155+
.name = "continue in while loop",
156+
.source =
157+
\\fn foo() !void {
158+
\\ return error.UhOh;
159+
\\}
160+
\\
161+
\\pub fn main() !void {
162+
\\ var i: usize = 0;
163+
\\ while (i < 3) : (i += 1) {
164+
\\ foo() catch continue;
165+
\\ }
166+
\\ return error.UnrelatedError;
167+
\\}
168+
,
169+
.Debug = .{
170+
.expect =
171+
\\error: UnrelatedError
172+
\\source.zig:10:5: [address] in main (test)
173+
\\ return error.UnrelatedError;
174+
\\ ^
175+
\\
176+
,
177+
},
178+
.ReleaseSafe = .{
179+
.exclude_os = .{
180+
.windows, // TODO
181+
.linux, // defeated by aggressive inlining
182+
},
183+
.expect =
184+
\\error: UnrelatedError
185+
\\source.zig:10:5: [address] in [function]
186+
\\ return error.UnrelatedError;
187+
\\ ^
188+
\\
189+
,
190+
},
191+
.ReleaseFast = .{
192+
.expect =
193+
\\error: UnrelatedError
194+
\\
195+
,
196+
},
197+
.ReleaseSmall = .{
198+
.expect =
199+
\\error: UnrelatedError
200+
\\
201+
,
202+
},
203+
});
204+
154205
cases.addCase(.{
155206
.name = "try return + handled catch/if-else",
156207
.source =

0 commit comments

Comments
 (0)