diff --git a/crates/noirc_frontend/src/hir/resolution/resolver.rs b/crates/noirc_frontend/src/hir/resolution/resolver.rs index 709751e8176..9d2d7f0ac9f 100644 --- a/crates/noirc_frontend/src/hir/resolution/resolver.rs +++ b/crates/noirc_frontend/src/hir/resolution/resolver.rs @@ -360,17 +360,17 @@ impl<'a> Resolver<'a> { args: Vec, new_variables: &mut Generics, ) -> Type { + if args.is_empty() { + if let Some(typ) = self.lookup_generic_or_global_type(&path) { + return typ; + } + } + // Check if the path is a type variable first. We currently disallow generics on type // variables since we do not support higher-kinded types. if path.segments.len() == 1 { let name = &path.last_segment().0.contents; - if args.is_empty() { - if let Some((name, var, _)) = self.find_generic(name) { - return Type::NamedGeneric(var.clone(), name.clone()); - } - } - if name == SELF_TYPE_NAME { if let Some(self_type) = self.self_type.clone() { if !args.is_empty() { @@ -405,6 +405,23 @@ impl<'a> Resolver<'a> { } } + fn lookup_generic_or_global_type(&mut self, path: &Path) -> Option { + if path.segments.len() == 1 { + let name = &path.last_segment().0.contents; + if let Some((name, var, _)) = self.find_generic(name) { + return Some(Type::NamedGeneric(var.clone(), name.clone())); + } + } + + // If we cannot find a local generic of the same name, try to look up a global + match self.path_resolver.resolve(self.def_maps, path.clone()) { + Ok(ModuleDefId::GlobalId(id)) => { + Some(Type::Constant(self.eval_global_as_array_length(id))) + } + _ => None, + } + } + fn resolve_array_size( &mut self, length: Option, @@ -428,22 +445,10 @@ impl<'a> Resolver<'a> { fn convert_expression_type(&mut self, length: UnresolvedTypeExpression) -> Type { match length { UnresolvedTypeExpression::Variable(path) => { - if path.segments.len() == 1 { - let name = &path.last_segment().0.contents; - if let Some((name, var, _)) = self.find_generic(name) { - return Type::NamedGeneric(var.clone(), name.clone()); - } - } - - // If we cannot find a local generic of the same name, try to look up a global - if let Ok(ModuleDefId::GlobalId(id)) = - self.path_resolver.resolve(self.def_maps, path.clone()) - { - Type::Constant(self.eval_global_as_array_length(id)) - } else { + self.lookup_generic_or_global_type(&path).unwrap_or_else(|| { self.push_err(ResolverError::NoSuchNumericTypeVariable { path }); Type::Constant(0) - } + }) } UnresolvedTypeExpression::Constant(int, _) => Type::Constant(int), UnresolvedTypeExpression::BinaryOperation(lhs, op, rhs, _) => {