From 426f2955cbe4f086581d05eea7d06c47e0491195 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 18 Sep 2024 10:22:41 -0300 Subject: [PATCH] fix: don't crash on untyped global used as array length (#6076) # Description ## Problem Resolves #6046 ## Summary Another case of an unhandled`DefinitionId::dummy()`. ## Additional Context ## Documentation Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../noirc_frontend/src/elaborator/types.rs | 23 +++++++++++-------- .../Nargo.toml | 7 ++++++ .../src/main.nr | 2 ++ 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 test_programs/compile_failure/global_without_a_type_used_as_array_length/Nargo.toml create mode 100644 test_programs/compile_failure/global_without_a_type_used_as_array_length/src/main.nr diff --git a/compiler/noirc_frontend/src/elaborator/types.rs b/compiler/noirc_frontend/src/elaborator/types.rs index c800b3b2c33..bb1161650c3 100644 --- a/compiler/noirc_frontend/src/elaborator/types.rs +++ b/compiler/noirc_frontend/src/elaborator/types.rs @@ -655,18 +655,21 @@ impl<'context> Elaborator<'context> { int.try_into_u128().ok_or(Some(ResolverError::IntegerTooLarge { span })) } HirExpression::Ident(ident, _) => { - let definition = self.interner.definition(ident.id); - match definition.kind { - DefinitionKind::Global(global_id) => { - let let_statement = self.interner.get_global_let_statement(global_id); - if let Some(let_statement) = let_statement { - let expression = let_statement.expression; - self.try_eval_array_length_id_with_fuel(expression, span, fuel - 1) - } else { - Err(Some(ResolverError::InvalidArrayLengthExpr { span })) + if let Some(definition) = self.interner.try_definition(ident.id) { + match definition.kind { + DefinitionKind::Global(global_id) => { + let let_statement = self.interner.get_global_let_statement(global_id); + if let Some(let_statement) = let_statement { + let expression = let_statement.expression; + self.try_eval_array_length_id_with_fuel(expression, span, fuel - 1) + } else { + Err(Some(ResolverError::InvalidArrayLengthExpr { span })) + } } + _ => Err(Some(ResolverError::InvalidArrayLengthExpr { span })), } - _ => Err(Some(ResolverError::InvalidArrayLengthExpr { span })), + } else { + Err(Some(ResolverError::InvalidArrayLengthExpr { span })) } } HirExpression::Infix(infix) => { diff --git a/test_programs/compile_failure/global_without_a_type_used_as_array_length/Nargo.toml b/test_programs/compile_failure/global_without_a_type_used_as_array_length/Nargo.toml new file mode 100644 index 00000000000..b2c83a26988 --- /dev/null +++ b/test_programs/compile_failure/global_without_a_type_used_as_array_length/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "global_without_a_type_used_as_array_length" +type = "bin" +authors = [""] +compiler_version = ">=0.33.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_failure/global_without_a_type_used_as_array_length/src/main.nr b/test_programs/compile_failure/global_without_a_type_used_as_array_length/src/main.nr new file mode 100644 index 00000000000..9edda231dec --- /dev/null +++ b/test_programs/compile_failure/global_without_a_type_used_as_array_length/src/main.nr @@ -0,0 +1,2 @@ +global BAR = OOPS; +global X: [Field; BAR] = [];