Skip to content

Commit

Permalink
fix: Resolve globals in types (#1043)
Browse files Browse the repository at this point in the history
Fix resolving globals in types
  • Loading branch information
jfecher authored Mar 28, 2023
1 parent 63f958b commit 2badf14
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,17 @@ impl<'a> Resolver<'a> {
args: Vec<UnresolvedType>,
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() {
Expand Down Expand Up @@ -405,6 +405,23 @@ impl<'a> Resolver<'a> {
}
}

fn lookup_generic_or_global_type(&mut self, path: &Path) -> Option<Type> {
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<UnresolvedTypeExpression>,
Expand All @@ -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, _) => {
Expand Down

0 comments on commit 2badf14

Please sign in to comment.