core: Fix restoration of loop target in try statement
old_loop_target is only assigned if ctx.loop_target is overwritten, meaning that if ctx.loop_target is never overwritten, ctx.loop_target will always be overwritten to None. We fix this by only restoring from old_loop_target if we previously assigned to old_loop_target.
This commit is contained in:
parent
c1a001ec88
commit
e97ceb0da9
|
@ -789,8 +789,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>(
|
||||||
ctx.outer_catch_clauses = old_clauses;
|
ctx.outer_catch_clauses = old_clauses;
|
||||||
ctx.unwind_target = old_unwind;
|
ctx.unwind_target = old_unwind;
|
||||||
ctx.return_target = old_return;
|
ctx.return_target = old_return;
|
||||||
ctx.loop_target = old_loop_target;
|
ctx.loop_target = old_loop_target.or(ctx.loop_target).take();
|
||||||
old_loop_target = None;
|
|
||||||
|
|
||||||
let old_unwind = if !finalbody.is_empty() {
|
let old_unwind = if !finalbody.is_empty() {
|
||||||
let final_landingpad = ctx.ctx.append_basic_block(current_fun, "try.catch.final");
|
let final_landingpad = ctx.ctx.append_basic_block(current_fun, "try.catch.final");
|
||||||
|
@ -911,7 +910,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>(
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.unwind_target = old_unwind;
|
ctx.unwind_target = old_unwind;
|
||||||
ctx.loop_target = old_loop_target;
|
ctx.loop_target = old_loop_target.or(ctx.loop_target).take();
|
||||||
ctx.return_target = old_return;
|
ctx.return_target = old_return;
|
||||||
|
|
||||||
ctx.builder.position_at_end(landingpad);
|
ctx.builder.position_at_end(landingpad);
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
# Break within try statement within a loop
|
||||||
|
# Taken from https://book.pythontips.com/en/latest/for_-_else.html
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_int32(x: int32, newline: bool=True):
|
||||||
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_float64(x: float, newline: bool=True):
|
||||||
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_str(x: str, newline: bool=True):
|
||||||
|
...
|
||||||
|
|
||||||
|
def run() -> int32:
|
||||||
|
for n in range(2, 10):
|
||||||
|
for x in range(2, n):
|
||||||
|
try:
|
||||||
|
if n % x == 0:
|
||||||
|
output_int32(n, newline=False)
|
||||||
|
output_str(" equals ", newline=False)
|
||||||
|
output_int32(x, newline=False)
|
||||||
|
output_str(" * ", newline=False)
|
||||||
|
output_float64(n / x)
|
||||||
|
except: # Assume this is intended to catch x == 0
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# loop fell through without finding a factor
|
||||||
|
output_int32(n, newline=False)
|
||||||
|
output_str(" is a prime number")
|
||||||
|
|
||||||
|
return 0
|
Loading…
Reference in New Issue