Skip to content

Commit

Permalink
feat: add Type::is_field and Type::as_integer (#5670)
Browse files Browse the repository at this point in the history
# Description

## Problem

Part of #5668

## Summary

I could have added more, but I'll try to keep these PRs relatively
small, also to get feedback on the approach.

## Additional Context

None.

## Documentation

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [x] **[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.

---------

Co-authored-by: jfecher <[email protected]>
  • Loading branch information
asterite and jfecher authored Aug 2, 2024
1 parent 9a8cfc9 commit 939357a
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 81 deletions.
21 changes: 21 additions & 0 deletions compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ pub enum IntegerBitSize {
SixtyFour,
}

impl IntegerBitSize {
pub fn bit_size(&self) -> u8 {
match self {
IntegerBitSize::One => 1,
IntegerBitSize::Eight => 8,
IntegerBitSize::Sixteen => 16,
IntegerBitSize::ThirtyTwo => 32,
IntegerBitSize::SixtyFour => 64,
}
}
}

impl IntegerBitSize {
pub fn allowed_sizes() -> Vec<Self> {
vec![Self::One, Self::Eight, Self::ThirtyTwo, Self::SixtyFour]
Expand Down Expand Up @@ -297,6 +309,15 @@ pub enum Signedness {
Signed,
}

impl Signedness {
pub fn is_signed(&self) -> bool {
match self {
Signedness::Unsigned => false,
Signedness::Signed => true,
}
}
}

impl UnresolvedTypeExpression {
// This large error size is justified because it improves parsing speeds by around 40% in
// release mode. See `ParserError` definition for further explanation.
Expand Down
34 changes: 21 additions & 13 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,30 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
let argument = Value::Pointer(Shared::new(argument), true);
self.define_pattern(pattern, typ, argument, location)
}
HirPattern::Tuple(pattern_fields, _) => match (argument, typ) {
(Value::Tuple(fields), Type::Tuple(type_fields))
if fields.len() == pattern_fields.len() =>
{
for ((pattern, typ), argument) in
pattern_fields.iter().zip(type_fields).zip(fields)
HirPattern::Tuple(pattern_fields, _) => {
let typ = &typ.follow_bindings();

match (argument, typ) {
(Value::Tuple(fields), Type::Tuple(type_fields))
if fields.len() == pattern_fields.len() =>
{
self.define_pattern(pattern, typ, argument, location)?;
for ((pattern, typ), argument) in
pattern_fields.iter().zip(type_fields).zip(fields)
{
self.define_pattern(pattern, typ, argument, location)?;
}
Ok(())
}
(value, _) => {
let actual = value.get_type().into_owned();
Err(InterpreterError::TypeMismatch {
expected: typ.clone(),
actual,
location,
})
}
Ok(())
}
(value, _) => {
let actual = value.get_type().into_owned();
Err(InterpreterError::TypeMismatch { expected: typ.clone(), actual, location })
}
},
}
HirPattern::Struct(struct_type, pattern_fields, _) => {
self.push_scope();

Expand Down
Loading

0 comments on commit 939357a

Please sign in to comment.