From f466fa1dab934552738a93e00fca765d9e834076 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Tue, 6 Feb 2024 01:17:39 +0000 Subject: [PATCH] chore: Add secondary message for UnknownLoopBound error (#4268) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description ## Problem\* Resolves #3687 ## Summary\* This simply adds a secondary message to clarify that using a slice length will lead to the `Could not determine loop bound at compile-time` error. I was thinking of disabling this earlier during type check but we don't necessarily know that we will have a slice until codegen as arrays and slices are polymorphic with one another. I also thought about making a separate runtime error to check during loop unrolling but we are not necessarily going to still have the `ArrayLen` call codegen'd in SSA and will instead have the call's results for the loop condition, so this isn't a basic check and some more logic would be needed. The best option for a specific error check seems to be during `codegen_for` before we codegen the end range. We can check whether we are making a slice ArrayLen call for the end range. These changes all felt a bit much as the error is quite clear except for this one error case and thus a secondary message felt sufficient. If we would like the error to be more specific for the failure case then I can look at adding logic that triggers an error for a slice length loop bound. An example of the error now: Screenshot 2024-02-05 at 5 10 39 PM ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- compiler/noirc_evaluator/src/errors.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/noirc_evaluator/src/errors.rs b/compiler/noirc_evaluator/src/errors.rs index 73b6e671bd5..020a807cb67 100644 --- a/compiler/noirc_evaluator/src/errors.rs +++ b/compiler/noirc_evaluator/src/errors.rs @@ -160,6 +160,17 @@ impl RuntimeError { noirc_errors::Span::inclusive(0, 0) ) } + RuntimeError::UnknownLoopBound { .. } => { + let primary_message = self.to_string(); + let location = + self.call_stack().back().expect("Expected RuntimeError to have a location"); + + Diagnostic::simple_error( + primary_message, + "If attempting to fetch the length of a slice, try converting to an array. Slices only use dynamic lengths.".to_string(), + location.span, + ) + } _ => { let message = self.to_string(); let location =