forgot to add defer handling to unrolled loops

Signed-off-by: Jakub Doka <jakub.doka2@gmail.com>
This commit is contained in:
Jakub Doka 2024-12-16 13:27:15 +01:00
parent b2be007ef0
commit 07d4fe416a
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143

View file

@ -2328,6 +2328,7 @@ pub enum CtLoopState {
enum Loop { enum Loop {
Comptime { Comptime {
state: Option<(CtLoopState, Pos)>, state: Option<(CtLoopState, Pos)>,
defer_base: usize,
}, },
Runtime { Runtime {
node: Nid, node: Nid,
@ -3951,7 +3952,9 @@ impl<'a> Codegen<'a> {
Expr::Loop { unrolled: true, body, pos } => { Expr::Loop { unrolled: true, body, pos } => {
let mut loop_fuel = 100; let mut loop_fuel = 100;
self.ci.loops.push(Loop::Comptime { state: None }); self.ci
.loops
.push(Loop::Comptime { state: None, defer_base: self.ci.defers.len() });
loop { loop {
if loop_fuel == 0 { if loop_fuel == 0 {
@ -3964,7 +3967,7 @@ impl<'a> Codegen<'a> {
let terminated = self.expr(body).is_none(); let terminated = self.expr(body).is_none();
let Some(&Loop::Comptime { state }) = self.ci.loops.last() else { let Some(&Loop::Comptime { state, .. }) = self.ci.loops.last() else {
unreachable!() unreachable!()
}; };
@ -5119,8 +5122,8 @@ impl<'a> Codegen<'a> {
return None; return None;
}; };
match loob { match *loob {
&mut Loop::Comptime { state: Some((_, prev)) } => { Loop::Comptime { state: Some((_, prev)), .. } => {
self.error( self.error(
pos, pos,
"reached multiple control flow keywords inside an unrolled loop, \ "reached multiple control flow keywords inside an unrolled loop, \
@ -5128,10 +5131,11 @@ impl<'a> Codegen<'a> {
); );
self.error(prev, "previous reachable control flow found here"); self.error(prev, "previous reachable control flow found here");
} }
Loop::Comptime { state: state @ None } => { Loop::Comptime { state: ref mut state @ None, defer_base } => {
*state = Some(([CtLoopState::Continued, CtLoopState::Terminated][id], pos)) *state = Some(([CtLoopState::Continued, CtLoopState::Terminated][id], pos));
self.gen_defers(defer_base)?;
} }
&mut Loop::Runtime { defer_base, .. } => { Loop::Runtime { defer_base, .. } => {
self.gen_defers(defer_base)?; self.gen_defers(defer_base)?;
let Loop::Runtime { ctrl: lctrl, ctrl_scope, scope, .. } = let Loop::Runtime { ctrl: lctrl, ctrl_scope, scope, .. } =