Skip to content

Commit

Permalink
chore: remove unused TypeVariableKind::Constant (#6053)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #6052

## Summary\*



## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
michaeljklein authored Sep 17, 2024
1 parent 5bbd9ba commit f476c4b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 122 deletions.
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 @@ pub enum TypeVariableKind {
/// 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 @@ impl std::fmt::Display for Type {
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 @@ impl Type {
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 @@ impl Type {
}
}

/// 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 @@ impl Type {
})
}

(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 @@ impl Type {
}

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 @@ -2383,7 +2295,6 @@ impl TypeVariableKind {
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 @@ impl std::fmt::Debug for Type {
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
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 @@ -477,14 +477,6 @@ fn push_type_parts(typ: &Type, parts: &mut Vec<InlayHintLabelPart>, files: &File
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

0 comments on commit f476c4b

Please sign in to comment.