Skip to content

Commit

Permalink
fix: proper cleanup when breaking from comptime loop on error
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jan 20, 2025
1 parent ed9977a commit 3153058
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
let (end, _) = get_index(self, for_.end_range)?;
let was_in_loop = std::mem::replace(&mut self.in_loop, true);

let mut result = Ok(Value::Unit);

for i in start..end {
self.push_scope();
self.current_scope_mut().insert(for_.identifier.id, make_value(i));
Expand All @@ -1732,20 +1734,24 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
Ok(_) => (),
Err(InterpreterError::Break) => break,
Err(InterpreterError::Continue) => continue,
Err(other) => return Err(other),
Err(error) => {
result = Err(error);
break;
}
}

self.pop_scope();
}

self.in_loop = was_in_loop;
Ok(Value::Unit)
result
}

fn evaluate_loop(&mut self, expr: ExprId) -> IResult<Value> {
let was_in_loop = std::mem::replace(&mut self.in_loop, true);
let in_lsp = self.elaborator.interner.is_in_lsp_mode();
let mut counter = 0;
let mut result = Ok(Value::Unit);

loop {
self.push_scope();
Expand All @@ -1754,20 +1760,24 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
Ok(_) => (),
Err(InterpreterError::Break) => break,
Err(InterpreterError::Continue) => continue,
Err(other) => return Err(other),
Err(error) => {
result = Err(error);
break;
}
}

self.pop_scope();

counter += 1;
if in_lsp && counter == 10_000 {
let location = self.elaborator.interner.expr_location(&expr);
return Err(InterpreterError::LoopHaltedForUiResponsiveness { location });
result = Err(InterpreterError::LoopHaltedForUiResponsiveness { location });
break;
}
}

self.in_loop = was_in_loop;
Ok(Value::Unit)
result
}

fn evaluate_break(&mut self, id: StmtId) -> IResult<Value> {
Expand Down

0 comments on commit 3153058

Please sign in to comment.