diff --git a/compiler/noirc_frontend/src/elaborator/types.rs b/compiler/noirc_frontend/src/elaborator/types.rs index b296c4f1805..ae2bb942f48 100644 --- a/compiler/noirc_frontend/src/elaborator/types.rs +++ b/compiler/noirc_frontend/src/elaborator/types.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, collections::BTreeMap, rc::Rc}; +use std::{borrow::Cow, rc::Rc}; use acvm::acir::AcirField; use iter_extended::vecmap; @@ -25,7 +25,7 @@ use crate::{ HirBinaryOp, HirCallExpression, HirExpression, HirLiteral, HirMemberAccess, HirMethodReference, HirPrefixExpression, TraitMethod, }, - function::{FuncMeta, Parameters}, + function::FuncMeta, stmt::HirStatement, traits::{NamedType, ResolvedTraitBound, Trait, TraitConstraint}, }, @@ -34,8 +34,7 @@ use crate::{ TraitImplKind, TraitMethodId, }, token::SecondaryAttribute, - Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeBindings, TypeVariable, - UnificationError, + Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeBindings, UnificationError, }; use super::{lints, path_resolution::PathResolutionItem, Elaborator}; @@ -1725,110 +1724,6 @@ impl<'context> Elaborator<'context> { } } - pub fn find_numeric_generics( - parameters: &Parameters, - return_type: &Type, - ) -> Vec<(String, TypeVariable)> { - let mut found = BTreeMap::new(); - for (_, parameter, _) in ¶meters.0 { - Self::find_numeric_generics_in_type(parameter, &mut found); - } - Self::find_numeric_generics_in_type(return_type, &mut found); - found.into_iter().collect() - } - - fn find_numeric_generics_in_type(typ: &Type, found: &mut BTreeMap) { - match typ { - Type::FieldElement - | Type::Integer(_, _) - | Type::Bool - | Type::Unit - | Type::Error - | Type::TypeVariable(_) - | Type::Constant(..) - | Type::NamedGeneric(_, _) - | Type::Quoted(_) - | Type::Forall(_, _) => (), - - Type::CheckedCast { from, to } => { - Self::find_numeric_generics_in_type(from, found); - Self::find_numeric_generics_in_type(to, found); - } - - Type::TraitAsType(_, _, args) => { - for arg in &args.ordered { - Self::find_numeric_generics_in_type(arg, found); - } - for arg in &args.named { - Self::find_numeric_generics_in_type(&arg.typ, found); - } - } - - Type::Array(length, element_type) => { - if let Type::NamedGeneric(type_variable, name) = length.as_ref() { - found.insert(name.to_string(), type_variable.clone()); - } - Self::find_numeric_generics_in_type(element_type, found); - } - - Type::Slice(element_type) => { - Self::find_numeric_generics_in_type(element_type, found); - } - - Type::Tuple(fields) => { - for field in fields { - Self::find_numeric_generics_in_type(field, found); - } - } - - Type::Function(parameters, return_type, _env, _unconstrained) => { - for parameter in parameters { - Self::find_numeric_generics_in_type(parameter, found); - } - Self::find_numeric_generics_in_type(return_type, found); - } - - Type::Struct(struct_type, generics) => { - for (i, generic) in generics.iter().enumerate() { - if let Type::NamedGeneric(type_variable, name) = generic { - if struct_type.borrow().generics[i].is_numeric() { - found.insert(name.to_string(), type_variable.clone()); - } - } else { - Self::find_numeric_generics_in_type(generic, found); - } - } - } - Type::Alias(alias, generics) => { - for (i, generic) in generics.iter().enumerate() { - if let Type::NamedGeneric(type_variable, name) = generic { - if alias.borrow().generics[i].is_numeric() { - found.insert(name.to_string(), type_variable.clone()); - } - } else { - Self::find_numeric_generics_in_type(generic, found); - } - } - } - Type::MutableReference(element) => Self::find_numeric_generics_in_type(element, found), - Type::String(length) => { - if let Type::NamedGeneric(type_variable, name) = length.as_ref() { - found.insert(name.to_string(), type_variable.clone()); - } - } - Type::FmtString(length, fields) => { - if let Type::NamedGeneric(type_variable, name) = length.as_ref() { - found.insert(name.to_string(), type_variable.clone()); - } - Self::find_numeric_generics_in_type(fields, found); - } - Type::InfixExpr(lhs, _op, rhs) => { - Self::find_numeric_generics_in_type(lhs, found); - Self::find_numeric_generics_in_type(rhs, found); - } - } - } - /// Push a type variable into the current FunctionContext to be defaulted if needed /// at the end of the earlier of either the current function or the current comptime scope. fn push_type_variable(&mut self, typ: Type) { diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index a0fea3aa774..ef8a697966c 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -184,10 +184,6 @@ impl Kind { } } - pub(crate) fn is_numeric(&self) -> bool { - matches!(self.follow_bindings(), Self::Numeric { .. }) - } - pub(crate) fn is_type_level_field_element(&self) -> bool { let type_level = false; self.is_field_element(type_level) @@ -375,10 +371,6 @@ impl ResolvedGeneric { pub fn kind(&self) -> Kind { self.type_var.kind() } - - pub(crate) fn is_numeric(&self) -> bool { - self.kind().is_numeric() - } } enum FunctionCoercionResult { @@ -524,13 +516,6 @@ impl StructType { self.fields.iter().map(|field| field.name.clone()).collect() } - /// Search the fields of a struct for any types with a `TypeKind::Numeric` - pub fn find_numeric_generics_in_fields(&self, found_names: &mut Vec) { - for field in self.fields.iter() { - field.typ.find_numeric_type_vars(found_names); - } - } - /// Instantiate this struct type, returning a Vec of the new generic args (in /// the same order as self.generics) pub fn instantiate(&self, interner: &mut NodeInterner) -> Vec { @@ -1102,99 +1087,6 @@ impl Type { } } - pub fn find_numeric_type_vars(&self, found_names: &mut Vec) { - // Return whether the named generic has a Kind::Numeric and save its name - let named_generic_is_numeric = |typ: &Type, found_names: &mut Vec| { - if let Type::NamedGeneric(var, name) = typ { - if var.kind().is_numeric() { - found_names.push(name.to_string()); - true - } else { - false - } - } else { - false - } - }; - - match self { - Type::FieldElement - | Type::Integer(_, _) - | Type::Bool - | Type::Unit - | Type::Error - | Type::Constant(_, _) - | Type::Forall(_, _) - | Type::Quoted(_) => {} - - Type::TypeVariable(type_var) => { - if let TypeBinding::Bound(typ) = &*type_var.borrow() { - named_generic_is_numeric(typ, found_names); - } - } - - Type::NamedGeneric(_, _) => { - named_generic_is_numeric(self, found_names); - } - Type::CheckedCast { from, to } => { - to.find_numeric_type_vars(found_names); - from.find_numeric_type_vars(found_names); - } - - Type::TraitAsType(_, _, args) => { - for arg in args.ordered.iter() { - arg.find_numeric_type_vars(found_names); - } - for arg in args.named.iter() { - arg.typ.find_numeric_type_vars(found_names); - } - } - Type::Array(length, elem) => { - elem.find_numeric_type_vars(found_names); - named_generic_is_numeric(length, found_names); - } - Type::Slice(elem) => elem.find_numeric_type_vars(found_names), - Type::Tuple(fields) => { - for field in fields.iter() { - field.find_numeric_type_vars(found_names); - } - } - Type::Function(parameters, return_type, env, _unconstrained) => { - for parameter in parameters.iter() { - parameter.find_numeric_type_vars(found_names); - } - return_type.find_numeric_type_vars(found_names); - env.find_numeric_type_vars(found_names); - } - Type::Struct(_, generics) => { - for generic in generics.iter() { - if !named_generic_is_numeric(generic, found_names) { - generic.find_numeric_type_vars(found_names); - } - } - } - Type::Alias(_, generics) => { - for generic in generics.iter() { - if !named_generic_is_numeric(generic, found_names) { - generic.find_numeric_type_vars(found_names); - } - } - } - Type::MutableReference(element) => element.find_numeric_type_vars(found_names), - Type::String(length) => { - named_generic_is_numeric(length, found_names); - } - Type::FmtString(length, elements) => { - elements.find_numeric_type_vars(found_names); - named_generic_is_numeric(length, found_names); - } - Type::InfixExpr(lhs, _op, rhs) => { - lhs.find_numeric_type_vars(found_names); - rhs.find_numeric_type_vars(found_names); - } - } - } - /// True if this type can be used as a parameter to `main` or a contract function. /// This is only false for unsized types like slices or slices that do not make sense /// as a program input such as named generics or mutable references.