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 TypeVariableKind::Constant #6053

Merged
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
31 changes: 9 additions & 22 deletions compiler/noirc_frontend/src/hir/comptime/hir_to_display_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ast::{
use crate::ast::{ConstrainStatement, Expression, Statement, StatementKind};
use crate::hir_def::expr::{HirArrayLiteral, HirBlockExpression, HirExpression, HirIdent};
use crate::hir_def::stmt::{HirLValue, HirPattern, HirStatement};
use crate::hir_def::types::{Type, TypeBinding, TypeVariableKind};
use crate::hir_def::types::{Type, TypeBinding};
use crate::macros_api::HirLiteral;
use crate::node_interner::{ExprId, NodeInterner, StmtId};

Expand Down Expand Up @@ -308,28 +308,15 @@ impl Type {
let name = Path::from_ident(type_def.name.clone());
UnresolvedTypeData::Named(name, generics, false)
}
Type::TypeVariable(binding, kind) => {
match &*binding.borrow() {
TypeBinding::Bound(typ) => return typ.to_display_ast(),
TypeBinding::Unbound(id) => {
let expression = match kind {
// TODO: fix span or make Option<Span>
TypeVariableKind::Constant(value) => {
UnresolvedTypeExpression::Constant(*value, Span::empty(0))
}
other_kind => {
let name = format!("var_{:?}_{}", other_kind, id);

// TODO: fix span or make Option<Span>
let path = Path::from_single(name, Span::empty(0));
UnresolvedTypeExpression::Variable(path)
}
};

UnresolvedTypeData::Expression(expression)
}
Type::TypeVariable(binding, kind) => match &*binding.borrow() {
TypeBinding::Bound(typ) => return typ.to_display_ast(),
TypeBinding::Unbound(id) => {
let name = format!("var_{:?}_{}", kind, id);
let path = Path::from_single(name, Span::empty(0));
let expression = UnresolvedTypeExpression::Variable(path);
UnresolvedTypeData::Expression(expression)
}
}
},
Type::TraitAsType(_, name, generics) => {
let ordered_args = vecmap(&generics.ordered, |generic| generic.to_display_ast());
let named_args = vecmap(&generics.named, |named_type| {
Expand Down
92 changes: 0 additions & 92 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,6 @@
/// A generic integer type. This is a more specific kind of TypeVariable
/// that can only be bound to Type::Integer, or other polymorphic integers.
Integer,

/// A potentially constant array size. This will only bind to itself or
/// Type::Constant(n) with a matching size. This defaults to Type::Constant(n) if still unbound
/// during monomorphization.
Constant(u32),
}

/// A TypeVariable is a mutable reference that is either
Expand Down Expand Up @@ -638,14 +633,6 @@
write!(f, "{}", binding.borrow())
}
}
Type::TypeVariable(binding, TypeVariableKind::Constant(n)) => {
if let TypeBinding::Unbound(_) = &*binding.borrow() {
// TypeVariableKind::Constant(n) binds to Type::Constant(n) by default, so just show that.
write!(f, "{n}")
} else {
write!(f, "{}", binding.borrow())
}
}
Type::Struct(s, args) => {
let args = vecmap(args, |arg| arg.to_string());
if args.is_empty() {
Expand Down Expand Up @@ -781,15 +768,6 @@
Type::TypeVariable(var, TypeVariableKind::Normal)
}

/// Returns a TypeVariable(_, TypeVariableKind::Constant(length)) to bind to
/// a constant integer for e.g. an array length.
pub fn constant_variable(length: u32, interner: &mut NodeInterner) -> Type {
let id = interner.next_type_variable_id();
let kind = TypeVariableKind::Constant(length);
let var = TypeVariable::unbound(id);
Type::TypeVariable(var, kind)
}

pub fn polymorphic_integer_or_field(interner: &mut NodeInterner) -> Type {
let id = interner.next_type_variable_id();
let kind = TypeVariableKind::IntegerOrField;
Expand Down Expand Up @@ -1294,65 +1272,6 @@
}
}

/// Try to bind a MaybeConstant variable to self, succeeding if self is a Constant,
/// MaybeConstant, or type variable. If successful, the binding is placed in the
/// given TypeBindings map rather than linked immediately.
fn try_bind_to_maybe_constant(
&self,
var: &TypeVariable,
target_length: u32,
bindings: &mut TypeBindings,
) -> Result<(), UnificationError> {
let target_id = match &*var.borrow() {
TypeBinding::Bound(_) => unreachable!(),
TypeBinding::Unbound(id) => *id,
};

let this = self.substitute(bindings).follow_bindings();

match &this {
Type::Constant(length) if *length == target_length => {
bindings.insert(target_id, (var.clone(), this));
Ok(())
}
// A TypeVariable is less specific than a MaybeConstant, so we bind
// to the other type variable instead.
Type::TypeVariable(new_var, kind) => {
let borrow = new_var.borrow();
match &*borrow {
TypeBinding::Bound(typ) => {
typ.try_bind_to_maybe_constant(var, target_length, bindings)
}
// Avoid infinitely recursive bindings
TypeBinding::Unbound(id) if *id == target_id => Ok(()),
TypeBinding::Unbound(new_target_id) => match kind {
TypeVariableKind::Normal => {
let clone = Type::TypeVariable(
var.clone(),
TypeVariableKind::Constant(target_length),
);
bindings.insert(*new_target_id, (new_var.clone(), clone));
Ok(())
}
TypeVariableKind::Constant(length) if *length == target_length => {
let clone = Type::TypeVariable(
var.clone(),
TypeVariableKind::Constant(target_length),
);
bindings.insert(*new_target_id, (new_var.clone(), clone));
Ok(())
}
// *length != target_length
TypeVariableKind::Constant(_) => Err(UnificationError),
TypeVariableKind::IntegerOrField => Err(UnificationError),
TypeVariableKind::Integer => Err(UnificationError),
},
}
}
_ => Err(UnificationError),
}
}

/// Try to bind a PolymorphicInt variable to self, succeeding if self is an integer, field,
/// other PolymorphicInt type, or type variable. If successful, the binding is placed in the
/// given TypeBindings map rather than linked immediately.
Expand Down Expand Up @@ -1543,12 +1462,6 @@
})
}

(TypeVariable(var, Kind::Constant(length)), other)
| (other, TypeVariable(var, Kind::Constant(length))) => other
.try_unify_to_type_variable(var, bindings, |bindings| {
other.try_bind_to_maybe_constant(var, *length, bindings)
}),

(Array(len_a, elem_a), Array(len_b, elem_b)) => {
len_a.try_unify(len_b, bindings)?;
elem_a.try_unify(elem_b, bindings)
Expand Down Expand Up @@ -1817,7 +1730,6 @@
}

match self.canonicalize() {
Type::TypeVariable(_, TypeVariableKind::Constant(size)) => Some(size),
Type::Array(len, _elem) => len.evaluate_to_u32(),
Type::Constant(x) => Some(x),
Type::InfixExpr(lhs, op, rhs) => {
Expand Down Expand Up @@ -1987,7 +1899,7 @@
}

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

Check warning on line 1902 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 @@ -2058,7 +1970,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 1973 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 @@ -2225,7 +2137,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 2140 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 @@ -2383,7 +2295,6 @@
match self {
TypeVariableKind::IntegerOrField => Some(Type::default_int_or_field_type()),
TypeVariableKind::Integer => Some(Type::default_int_type()),
TypeVariableKind::Constant(length) => Some(Type::Constant(*length)),
TypeVariableKind::Normal => None,
}
}
Expand Down Expand Up @@ -2485,9 +2396,6 @@
Type::TypeVariable(binding, TypeVariableKind::Integer) => {
write!(f, "Int{:?}", binding)
}
Type::TypeVariable(binding, TypeVariableKind::Constant(n)) => {
write!(f, "{}{:?}", n, binding)
}
Type::Struct(s, args) => {
let args = vecmap(args, |arg| format!("{:?}", arg));
if args.is_empty() {
Expand Down Expand Up @@ -2604,7 +2512,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 2515 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 2515 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 Expand Up @@ -2697,7 +2605,7 @@
// Special case: we consider unbound named generics and type variables to be equal to each
// other if their type variable ids match. This is important for some corner cases in
// monomorphization where we call `replace_named_generics_with_type_variables` but
// still want them to be equal for canonicalization checks in arithmetic generics.

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (canonicalization)
// Without this we'd fail the `serialize` test.
(
NamedGeneric(lhs_var, _, _) | TypeVariable(lhs_var, _),
Expand Down
8 changes: 0 additions & 8 deletions tooling/lsp/src/requests/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
args.files.get_file_id(&path).map(|file_id| {
let file = args.files.get_file(file_id).unwrap();
let source = file.source();
let (parsed_moduled, _errors) = noirc_frontend::parse_program(source);

Check warning on line 44 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)

let span = utils::range_to_byte_span(args.files, file_id, &params.range)
.map(|range| Span::from(range.start as u32..range.end as u32));

let mut collector =
InlayHintCollector::new(args.files, file_id, args.interner, span, options);
parsed_moduled.accept(&mut collector);

Check warning on line 51 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)
collector.inlay_hints
})
});
Expand Down Expand Up @@ -477,14 +477,6 @@
push_type_variable_parts(binding, parts, files);
}
}
Type::TypeVariable(binding, TypeVariableKind::Constant(n)) => {
if let TypeBinding::Unbound(_) = &*binding.borrow() {
// TypeVariableKind::Constant(n) binds to Type::Constant(n) by default, so just show that.
parts.push(string_part(n.to_string()));
} else {
push_type_variable_parts(binding, parts, files);
}
}

Type::FieldElement
| Type::Integer(..)
Expand Down
Loading