From 766741b0da073e73ff51fe03935fe61c2e917d8c Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Tue, 15 Oct 2024 17:03:14 +0100 Subject: [PATCH 1/3] Better error message when reaching recursion limit during inlining --- .../noirc_evaluator/src/ssa/opt/inlining.rs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/inlining.rs b/compiler/noirc_evaluator/src/ssa/opt/inlining.rs index 7843c55da65..0c11a8bee9a 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/inlining.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/inlining.rs @@ -291,13 +291,14 @@ impl InlineContext { ) -> Vec { self.recursion_level += 1; + let source_function = &ssa.functions[&id]; + if self.recursion_level > RECURSION_LIMIT { panic!( - "Attempted to recur more than {RECURSION_LIMIT} times during function inlining." + "Attempted to recur more than {RECURSION_LIMIT} times during inlining function '{}': {}", source_function.name(), source_function ); } - let source_function = &ssa.functions[&id]; let mut context = PerFunctionContext::new(self, source_function); let parameters = source_function.parameters(); @@ -967,4 +968,28 @@ mod test { let main = ssa.main(); assert_eq!(main.reachable_blocks().len(), 4); } + + #[test] + #[should_panic( + expected = "Attempted to recur more than 1000 times during inlining function 'main': acir(inline) fn main f0 {" + )] + fn unconditional_recursion() { + // fn main f1 { + // b0(): + // call f1() + // return + // } + let main_id = Id::test_new(0); + let mut builder = FunctionBuilder::new("main".into(), main_id); + + let main = builder.import_function(main_id); + let results = builder.insert_call(main, Vec::new(), vec![]).to_vec(); + builder.terminate_with_return(results); + + let ssa = builder.finish(); + assert_eq!(ssa.functions.len(), 1); + + let inlined = ssa.inline_functions(); + assert_eq!(inlined.functions.len(), 0); + } } From 42bbc100caa3f9a87d8c67b1438de3ec43510441 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Tue, 15 Oct 2024 17:04:48 +0100 Subject: [PATCH 2/3] Test to show that unconditional recursion is not a compile error --- compiler/noirc_frontend/src/tests.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index b2800717d90..0b92bd94abb 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -3389,3 +3389,14 @@ fn arithmetic_generics_rounding_fail() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); } + +#[test] +fn unconditional_recursion() { + let src = r#" + fn main() { + main() + } + "#; + // It would be nice if it issued a warning. + assert_no_errors(src); +} From 314e942a453585017c508313246e04f505928a12 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 16 Oct 2024 18:43:32 +0100 Subject: [PATCH 3/3] Remove test from the frontend --- compiler/noirc_frontend/src/tests.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 0b92bd94abb..b2800717d90 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -3389,14 +3389,3 @@ fn arithmetic_generics_rounding_fail() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); } - -#[test] -fn unconditional_recursion() { - let src = r#" - fn main() { - main() - } - "#; - // It would be nice if it issued a warning. - assert_no_errors(src); -}