Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve globals in types #1043

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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