From 3af8dc5bdd50a1042c3851f1b038945f6f6c42f5 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Sun, 3 Nov 2024 23:56:45 +0100 Subject: [PATCH] fix `continue` in finalize-less loops --- crates/dash_compiler/src/builder.rs | 3 ++- crates/dash_compiler/src/lib.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/dash_compiler/src/builder.rs b/crates/dash_compiler/src/builder.rs index 9024acb4..d23d1222 100755 --- a/crates/dash_compiler/src/builder.rs +++ b/crates/dash_compiler/src/builder.rs @@ -22,7 +22,8 @@ pub enum Label { LoopEnd { loop_id: usize, }, - LoopIncrement { + /// The end of an iteration. For `for` loops, this points to the increment code. For other loops, this aliases the condition. + LoopIterationEnd { loop_id: usize, }, SwitchCaseCondition { diff --git a/crates/dash_compiler/src/lib.rs b/crates/dash_compiler/src/lib.rs index 677dd47b..ed4cb18f 100644 --- a/crates/dash_compiler/src/lib.rs +++ b/crates/dash_compiler/src/lib.rs @@ -1043,6 +1043,8 @@ impl<'interner> Visitor> for FunctionCompiler<'interner> { ib.current_function_mut() .add_global_label(Label::LoopCondition { loop_id }); + ib.current_function_mut() + .add_global_label(Label::LoopIterationEnd { loop_id }); ib.accept_expr(condition)?; ib.build_jmpfalsep(Label::LoopEnd { loop_id }, false); @@ -1071,6 +1073,8 @@ impl<'interner> Visitor> for FunctionCompiler<'interner> { ib.accept(*body)?; + ib.current_function_mut() + .add_global_label(Label::LoopIterationEnd { loop_id }); ib.accept_expr(condition)?; ib.build_jmptruep(Label::LoopCondition { loop_id }, false); @@ -1968,7 +1972,7 @@ impl<'interner> Visitor> for FunctionCompiler<'interner> { // Increment ib.current_function_mut() - .add_global_label(Label::LoopIncrement { loop_id }); + .add_global_label(Label::LoopIterationEnd { loop_id }); if let Some(finalizer) = finalizer { ib.accept_expr(finalizer)?; ib.build_pop(); @@ -2143,7 +2147,7 @@ impl<'interner> Visitor> for FunctionCompiler<'interner> { match breakable { Breakable::Loop { loop_id, label: _ } => { - ib.build_jmp(Label::LoopIncrement { loop_id }, false); + ib.build_jmp(Label::LoopIterationEnd { loop_id }, false); } Breakable::Switch { .. } => { // TODO: make it possible to use `continue` in loops even if its used in a switch