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

feat: TypeVariableKind for just Integers #4118

Merged
merged 29 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9510292
WIP: first pass of adding a TypeVariableKind specifically for Integer(s)
michaeljklein Jan 22, 2024
ffb8301
WIP working through cargo errors
michaeljklein Jan 23, 2024
5a9e434
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Jan 31, 2024
c8ebfb9
revert unrelated test programs change, update printing of TypeVariabl…
michaeljklein Jan 31, 2024
a169552
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
940ec3a
wip implementing type check rules for Kind::Integer
michaeljklein Feb 7, 2024
5a1b8b6
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
56886b0
resolve typing with TypeVariableKind::Integer (bindings), update borr…
michaeljklein Feb 7, 2024
83e776c
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
4a2b99e
add test programs for regressions
michaeljklein Feb 7, 2024
442051d
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
db62dfc
ensure IntegerOrField and Integer TypeVariableKind's are equivalent w…
michaeljklein Feb 7, 2024
cc739c3
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
5a9781e
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 7, 2024
d7c8098
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 8, 2024
130d19d
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 8, 2024
7102e4d
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 8, 2024
78747a2
Merge branch 'master' into michaeljklein/integer-kind
TomAFrench Feb 9, 2024
c98ad67
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 12, 2024
6e34306
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 12, 2024
082d3cc
Update test_programs/compile_success_empty/infer_int_loop_bounds/src/…
michaeljklein Feb 12, 2024
f0918c8
Update test_programs/compile_success_empty/int_or_field_casting_loop/…
michaeljklein Feb 12, 2024
a913bb4
Update compiler/noirc_frontend/src/hir_def/types.rs
michaeljklein Feb 12, 2024
e6eb976
update comment w/ type checking context
michaeljklein Feb 13, 2024
d404ad4
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 13, 2024
7cdacc2
Update compiler/noirc_frontend/src/hir_def/types.rs
michaeljklein Feb 13, 2024
ea3fd62
remove tests that pass on master branch
michaeljklein Feb 13, 2024
126965a
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 13, 2024
77fc805
Merge branch 'master' into michaeljklein/integer-kind
michaeljklein Feb 13, 2024
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
49 changes: 47 additions & 2 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
borrow::Cow,
borrow::{Borrow, Cow},
cell::RefCell,
collections::{BTreeSet, HashMap},
rc::Rc,
Expand Down Expand Up @@ -441,6 +441,10 @@
/// type annotations on each integer literal.
IntegerOrField,

/// 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, Type::NotConstant, or
/// Type::Constant(n) with a matching size. This defaults to Type::Constant(n) if still unbound
/// during monomorphization.
Expand Down Expand Up @@ -745,7 +749,17 @@
Signedness::Signed => write!(f, "i{num_bits}"),
Signedness::Unsigned => write!(f, "u{num_bits}"),
},
Type::TypeVariable(var, TypeVariableKind::Normal) => write!(f, "{}", var.borrow()),
Type::TypeVariable(var, TypeVariableKind::Normal) => write!(f, "{}", var.0.borrow()),
Type::TypeVariable(binding, TypeVariableKind::Integer) => {
if let TypeBinding::Unbound(_) = &*binding.0.borrow() {
// Show a Field by default if this TypeVariableKind::IntegerOrField is unbound, since that is
// what they bind to by default anyway. It is less confusing than displaying it
// as a generic.
write!(f, "Integer")
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved
} else {
write!(f, "{}", binding.0.borrow())
}
}
Type::TypeVariable(binding, TypeVariableKind::IntegerOrField) => {
if let TypeBinding::Unbound(_) = &*binding.borrow() {
// Show a Field by default if this TypeVariableKind::IntegerOrField is unbound, since that is
Expand Down Expand Up @@ -949,6 +963,20 @@
}
}
}
Type::TypeVariable(self_var, TypeVariableKind::Integer) => {
let borrow = self_var.0.borrow();
match &*borrow {
TypeBinding::Bound(typ) => typ.try_bind_to_polymorphic_int(var, bindings),
// Avoid infinitely recursive bindings
TypeBinding::Unbound(id) if *id == target_id => Ok(()),
TypeBinding::Unbound(id) => {

// we want to bind var to self_var (the less specific to the more specififc)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (specififc)
bindings.insert(*id, (var.clone(), ()));
Ok(())
}
}
}
Type::TypeVariable(binding, TypeVariableKind::Normal) => {
let borrow = binding.borrow();
match &*borrow {
Expand Down Expand Up @@ -1054,6 +1082,16 @@
})
}

(TypeVariable(var, Kind::Integer), other)
| (other, TypeVariable(var, Kind::Integer)) => {
()
}
// {
// other.try_unify_to_type_variable(var, bindings, |bindings| {
// other.try_bind_to_polymorphic_int(var, bindings)
// })
// }

(TypeVariable(var, Kind::Normal), other) | (other, TypeVariable(var, Kind::Normal)) => {
other.try_unify_to_type_variable(var, bindings, |bindings| {
other.try_bind_to(var, bindings)
Expand Down Expand Up @@ -1408,7 +1446,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 1449 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 +1461,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 1464 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 @@ -1599,6 +1637,7 @@
pub(crate) fn default_type(&self) -> Type {
match self {
TypeVariableKind::IntegerOrField | TypeVariableKind::Normal => Type::default_int_type(),
TypeVariableKind::Integer => Type::default_range_loop_type(),
TypeVariableKind::Constant(length) => Type::Constant(*length),
}
}
Expand All @@ -1625,6 +1664,12 @@
Signedness::Unsigned => PrintableType::UnsignedInteger { width: *bit_width },
Signedness::Signed => PrintableType::SignedInteger { width: *bit_width },
},
Type::TypeVariable(binding, TypeVariableKind::Integer) => {
match &*binding.0.borrow() {
TypeBinding::Bound(typ) => typ.into(),
TypeBinding::Unbound(_) => Type::default_range_loop_type().into(),
}
}
Type::TypeVariable(binding, TypeVariableKind::IntegerOrField) => {
match &*binding.borrow() {
TypeBinding::Bound(typ) => typ.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Tests arithmetic operations on integers
fn main() {
let x: u32 = 6;
let y: u32 = 2;
let x = 6;
let y = 2;

assert((x + y) == add(x, y));

Expand Down Expand Up @@ -59,8 +59,9 @@ unconstrained fn and(x: u32, y: u32) -> u32 {
x & y
}

unconstrained fn or(x: u32, y: u32) -> u32 {
x | y
unconstrained fn or(x: u32, y: u32) -> () {
let z = x | y;
()
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved
}

unconstrained fn check_xor(x: u32, y: u32, result: u32) -> bool {
Expand Down
Loading