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

chore: Remove unused methods from implicit numeric generics #6541

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
111 changes: 3 additions & 108 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,7 +25,7 @@
HirBinaryOp, HirCallExpression, HirExpression, HirLiteral, HirMemberAccess,
HirMethodReference, HirPrefixExpression, TraitMethod,
},
function::{FuncMeta, Parameters},
function::FuncMeta,
stmt::HirStatement,
traits::{NamedType, ResolvedTraitBound, Trait, TraitConstraint},
},
Expand All @@ -34,8 +34,7 @@
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};
Expand Down Expand Up @@ -420,7 +419,7 @@
// TODO(https://github.com/noir-lang/noir/issues/6238):
// support non-u32 generics here
if !kind.unifies(&Kind::u32()) {
let error = TypeCheckError::EvaluatedGlobalIsntU32 {

Check warning on line 422 in compiler/noirc_frontend/src/elaborator/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
expected_kind: Kind::u32().to_string(),
expr_kind: kind.to_string(),
expr_span: path.span(),
Expand Down Expand Up @@ -1725,110 +1724,6 @@
}
}

pub fn find_numeric_generics(
parameters: &Parameters,
return_type: &Type,
) -> Vec<(String, TypeVariable)> {
let mut found = BTreeMap::new();
for (_, parameter, _) in &parameters.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<String, TypeVariable>) {
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) {
Expand Down
108 changes: 0 additions & 108 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@
}
}

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)
Expand Down Expand Up @@ -375,10 +371,6 @@
pub fn kind(&self) -> Kind {
self.type_var.kind()
}

pub(crate) fn is_numeric(&self) -> bool {
self.kind().is_numeric()
}
}

enum FunctionCoercionResult {
Expand Down Expand Up @@ -524,13 +516,6 @@
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<String>) {
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<Type> {
Expand Down Expand Up @@ -1102,99 +1087,6 @@
}
}

pub fn find_numeric_type_vars(&self, found_names: &mut Vec<String>) {
// Return whether the named generic has a Kind::Numeric and save its name
let named_generic_is_numeric = |typ: &Type, found_names: &mut Vec<String>| {
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.
Expand Down Expand Up @@ -2243,7 +2135,7 @@
}

let recur_on_binding = |id, replacement: &Type| {
// Prevent recuring forever if there's a `T := T` binding

Check warning on line 2138 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
if replacement.type_variable_id() == Some(id) {
replacement.clone()
} else {
Expand Down Expand Up @@ -2328,7 +2220,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 2223 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down Expand Up @@ -2500,7 +2392,7 @@

/// Replace any `Type::NamedGeneric` in this type with a `Type::TypeVariable`
/// using to the same inner `TypeVariable`. This is used during monomorphization
/// to bind to named generics since they are unbindable during type checking.

Check warning on line 2395 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)
pub fn replace_named_generics_with_type_variables(&mut self) {
match self {
Type::FieldElement
Expand Down Expand Up @@ -2961,7 +2853,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 2856 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)

Check warning on line 2856 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::Struct(def, args) => {
def.hash(state);
args.hash(state);
Expand Down
Loading