Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper cleanup when breaking from comptime loop on error #7125

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -258,7 +258,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 261 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -1052,7 +1052,7 @@
}

/// Generate matches for bit shifting, which in Noir only accepts `u8` for RHS.
macro_rules! match_bitshift {

Check warning on line 1055 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(($lhs_value:ident as $lhs:ident $op:literal $rhs_value:ident as $rhs:ident) => $expr:expr) => {
match_values! {
($lhs_value as $lhs $op $rhs_value as $rhs) {
Expand Down Expand Up @@ -1122,10 +1122,10 @@
BinaryOpKind::Xor => match_bitwise! {
(lhs_value as lhs "^" rhs_value as rhs) => lhs ^ rhs
},
BinaryOpKind::ShiftRight => match_bitshift! {

Check warning on line 1125 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(lhs_value as lhs ">>" rhs_value as rhs) => lhs.checked_shr(rhs.into())
},
BinaryOpKind::ShiftLeft => match_bitshift! {

Check warning on line 1128 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(lhs_value as lhs "<<" rhs_value as rhs) => lhs.checked_shl(rhs.into())
},
BinaryOpKind::Modulo => match_integer! {
Expand Down Expand Up @@ -1724,6 +1724,8 @@
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 @@
Ok(_) => (),
Err(InterpreterError::Break) => break,
Err(InterpreterError::Continue) => continue,
Err(other) => return Err(other),
Err(error) => {
result = Err(error);
break;
}
}

self.pop_scope();
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@
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
Loading