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: Add Type: Debug impl #4156

Merged
merged 1 commit into from
Jan 25, 2024
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
110 changes: 104 additions & 6 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use super::expr::{HirCallExpression, HirExpression, HirIdent};

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
#[derive(PartialEq, Eq, Clone, Hash)]
pub enum Type {
/// A primitive Field type
FieldElement,
Expand Down Expand Up @@ -190,7 +190,7 @@
/// Represents a struct type in the type system. Each instance of this
/// rust struct will be shared across all Type::Struct variants that represent
/// the same struct type.
#[derive(Debug, Eq)]
#[derive(Eq)]
pub struct StructType {
/// A unique id representing this struct type. Used to check if two
/// struct types are equal.
Expand Down Expand Up @@ -449,7 +449,7 @@

/// A TypeVariable is a mutable reference that is either
/// bound to some type, or unbound with a given TypeVariableId.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
#[derive(PartialEq, Eq, Clone, Hash)]
pub struct TypeVariable(TypeVariableId, Shared<TypeBinding>);

impl TypeVariable {
Expand Down Expand Up @@ -516,7 +516,7 @@

/// TypeBindings are the mutable insides of a TypeVariable.
/// They are either bound to some type, or are unbound.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum TypeBinding {
Bound(Type),
Unbound(TypeVariableId),
Expand All @@ -529,7 +529,7 @@
}

/// A unique ID used to differentiate different type variables
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct TypeVariableId(pub usize);

impl Type {
Expand Down Expand Up @@ -804,7 +804,7 @@
Type::Function(args, ret, env) => {
let closure_env_text = match **env {
Type::Unit => "".to_string(),
_ => format!(" with closure environment {env}"),
_ => format!(" with env {env}"),
};

let args = vecmap(args.iter(), ToString::to_string);
Expand Down Expand Up @@ -1408,7 +1408,7 @@
Type::NamedGeneric(binding, _) | Type::TypeVariable(binding, _) => {
substitute_binding(binding)
}
// Do not substitute_helper fields, it ca, substitute_bound_typevarsn lead to infinite recursion

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsn)
// and we should not match fields when type checking anyway.
Type::Struct(fields, args) => {
let args = vecmap(args, |arg| {
Expand All @@ -1423,7 +1423,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 1426 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 @@ -1661,3 +1661,101 @@
}
}
}

impl std::fmt::Debug for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::FieldElement => {
write!(f, "Field")
}
Type::Array(len, typ) => {
if matches!(len.follow_bindings(), Type::NotConstant) {
write!(f, "[{typ:?}]")
} else {
write!(f, "[{typ:?}; {len:?}]")
}
}
Type::Integer(sign, num_bits) => match sign {
Signedness::Signed => write!(f, "i{num_bits}"),
Signedness::Unsigned => write!(f, "u{num_bits}"),
},
Type::TypeVariable(var, TypeVariableKind::Normal) => write!(f, "{:?}", var),
Type::TypeVariable(binding, TypeVariableKind::IntegerOrField) => {
write!(f, "IntOrField{:?}", 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() {
write!(f, "{:?}", s.borrow())
} else {
write!(f, "{:?}<{}>", s.borrow(), args.join(", "))
}
}
Type::TraitAsType(_id, name, generics) => {
write!(f, "impl {}", name)?;
if !generics.is_empty() {
let generics = vecmap(generics, |arg| format!("{:?}", arg)).join(", ");
write!(f, "<{generics}>")?;
}
Ok(())
}
Type::Tuple(elements) => {
let elements = vecmap(elements, |arg| format!("{:?}", arg));
write!(f, "({})", elements.join(", "))
}
Type::Bool => write!(f, "bool"),
Type::String(len) => write!(f, "str<{len:?}>"),
Type::FmtString(len, elements) => {
write!(f, "fmtstr<{len:?}, {elements:?}>")
}
Type::Unit => write!(f, "()"),
Type::Error => write!(f, "error"),
Type::NamedGeneric(binding, name) => write!(f, "{}{:?}", name, binding),
Type::Constant(x) => x.fmt(f),
Type::Forall(typevars, typ) => {
let typevars = vecmap(typevars, |var| format!("{:?}", var));
write!(f, "forall {}. {:?}", typevars.join(" "), typ)
}
Type::Function(args, ret, env) => {
let closure_env_text = match **env {
Type::Unit => "".to_string(),
_ => format!(" with env {env:?}"),
};

let args = vecmap(args.iter(), |arg| format!("{:?}", arg));

write!(f, "fn({}) -> {ret:?}{closure_env_text}", args.join(", "))
}
Type::MutableReference(element) => {
write!(f, "&mut {element:?}")
}
Type::NotConstant => write!(f, "NotConstant"),
}
}
}

impl std::fmt::Debug for TypeVariableId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "'{}", self.0)
}
}

impl std::fmt::Debug for TypeVariable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.id())?;

if let TypeBinding::Bound(typ) = &*self.borrow() {
write!(f, " -> {typ:?}")?;
}
Ok(())
}
}

impl std::fmt::Debug for StructType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
Loading