Skip to content

Commit

Permalink
fix: allow empty loop headers (#6736)
Browse files Browse the repository at this point in the history
Co-authored-by: jfecher <jake@aztecprotocol.com>
Co-authored-by: Akosh Farkash <aakoshh@gmail.com>
Co-authored-by: Tom French <tom@tomfren.ch>
  • Loading branch information
4 people authored Dec 9, 2024
1 parent 1b6e26b commit 332ba79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/unrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ impl Loop {
pub(super) fn get_const_upper_bound(&self, function: &Function) -> Option<FieldElement> {
let block = &function.dfg[self.header];
let instructions = block.instructions();
if instructions.is_empty() {
// If the loop condition is constant time, the loop header will be
// simplified to a simple jump.
return None;
}
assert_eq!(
instructions.len(),
1,
Expand Down
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_6734/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_6734"
type = "bin"
authors = [""]

[dependencies]
24 changes: 24 additions & 0 deletions test_programs/execution_success/regression_6734/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://github.com/noir-lang/noir/issues/6734

pub fn sub_array_extended<let SRC_LEN: u32, let DST_LEN: u32>(
src: [Field; SRC_LEN],
offset: u32,
) -> [Field; DST_LEN] {
let available_elements_to_copy = SRC_LEN - offset;
let elements_to_copy = if DST_LEN > available_elements_to_copy {
available_elements_to_copy
} else {
DST_LEN
};

let mut dst: [Field; DST_LEN] = std::mem::zeroed();
for i in 0..elements_to_copy {
dst[i] = src[i + offset];
}

dst
}

unconstrained fn main() {
assert_eq(sub_array_extended([], 0), []);
}

0 comments on commit 332ba79

Please sign in to comment.