Skip to content

Commit 426f295

Browse files
authored
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.
1 parent 19eef30 commit 426f295

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

  • compiler/noirc_frontend/src/elaborator
  • test_programs/compile_failure/global_without_a_type_used_as_array_length

compiler/noirc_frontend/src/elaborator/types.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -655,18 +655,21 @@ impl<'context> Elaborator<'context> {
655655
int.try_into_u128().ok_or(Some(ResolverError::IntegerTooLarge { span }))
656656
}
657657
HirExpression::Ident(ident, _) => {
658-
let definition = self.interner.definition(ident.id);
659-
match definition.kind {
660-
DefinitionKind::Global(global_id) => {
661-
let let_statement = self.interner.get_global_let_statement(global_id);
662-
if let Some(let_statement) = let_statement {
663-
let expression = let_statement.expression;
664-
self.try_eval_array_length_id_with_fuel(expression, span, fuel - 1)
665-
} else {
666-
Err(Some(ResolverError::InvalidArrayLengthExpr { span }))
658+
if let Some(definition) = self.interner.try_definition(ident.id) {
659+
match definition.kind {
660+
DefinitionKind::Global(global_id) => {
661+
let let_statement = self.interner.get_global_let_statement(global_id);
662+
if let Some(let_statement) = let_statement {
663+
let expression = let_statement.expression;
664+
self.try_eval_array_length_id_with_fuel(expression, span, fuel - 1)
665+
} else {
666+
Err(Some(ResolverError::InvalidArrayLengthExpr { span }))
667+
}
667668
}
669+
_ => Err(Some(ResolverError::InvalidArrayLengthExpr { span })),
668670
}
669-
_ => Err(Some(ResolverError::InvalidArrayLengthExpr { span })),
671+
} else {
672+
Err(Some(ResolverError::InvalidArrayLengthExpr { span }))
670673
}
671674
}
672675
HirExpression::Infix(infix) => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "global_without_a_type_used_as_array_length"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = ">=0.33.0"
6+
7+
[dependencies]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
global BAR = OOPS;
2+
global X: [Field; BAR] = [];

0 commit comments

Comments
 (0)