From 62222ef006bc6f251355f0cb6319de84f83adc38 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 16:41:50 -0300 Subject: [PATCH 01/16] Add Type::is_field --- .../src/hir/comptime/interpreter/builtin.rs | 14 ++++++++++++++ noir_stdlib/src/meta/typ.nr | 5 +++++ .../comptime_type/src/main.nr | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 1904eaa02b9..bb49cfdb07b 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -56,6 +56,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "quoted_as_trait_constraint" => quoted_as_trait_constraint(self, arguments, location), "quoted_as_type" => quoted_as_type(self, arguments, location), "type_eq" => type_eq(arguments, location), + "type_is_field" => type_is_field(arguments, location), "type_of" => type_of(arguments, location), "zeroed" => zeroed(return_type), _ => { @@ -444,6 +445,19 @@ fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult Ok(Value::Bool(value1 == value2)) } +fn type_is_field(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { + check_argument_count(1, &arguments, location)?; + + let value = arguments.pop().unwrap().0; + let Value::Type(typ) = value else { + panic!("type_is_field shold have been called with a type"); + }; + + let is_field = matches!(typ, Type::FieldElement); + + Ok(Value::Bool(is_field)) +} + fn type_of(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { check_argument_count(1, &arguments, location)?; diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index 197c41a482c..ee44cf3f268 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -1,5 +1,10 @@ use crate::cmp::Eq; +impl Type { + #[builtin(type_is_field)] + fn is_field(self) -> bool {} +} + impl Eq for Type { fn eq(self, other: Self) -> bool { type_eq(self, other) diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index e74cc0a4d14..82c62c27619 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -12,5 +12,9 @@ fn main() { let i32_type = type_of(an_i32); assert(field_type_1 == field_type_2); assert(field_type_1 != i32_type); + + // Check Type::is_field + assert(field_type_1.is_field()); + assert(!i32_type.is_field()); } } From fa746e21252397c1b7dc44a8afb83c2d4536a7df Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 17:43:20 -0300 Subject: [PATCH 02/16] Add Type::as_integer --- .../src/hir/comptime/interpreter.rs | 30 +++++++----- .../src/hir/comptime/interpreter/builtin.rs | 49 +++++++++++++++++++ noir_stdlib/src/meta/typ.nr | 4 ++ .../comptime_type/src/main.nr | 13 +++++ 4 files changed, 84 insertions(+), 12 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index a3d37fd76fc..324f8a93a9c 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -328,21 +328,27 @@ 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(()) } - Ok(()) - } - (value, _) => { - Err(InterpreterError::TypeMismatch { expected: typ.clone(), value, location }) + (value, actual_type) => Err(InterpreterError::TypeMismatch { + expected: typ.clone(), + value, + location, + }), } - }, + } HirPattern::Struct(struct_type, pattern_fields, _) => { self.push_scope(); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index bb49cfdb07b..83b96a0cb09 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -55,6 +55,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { } "quoted_as_trait_constraint" => quoted_as_trait_constraint(self, arguments, location), "quoted_as_type" => quoted_as_type(self, arguments, location), + "type_as_integer" => type_as_integer(arguments, return_type, location), "type_eq" => type_eq(arguments, location), "type_is_field" => type_is_field(arguments, location), "type_of" => type_of(arguments, location), @@ -437,6 +438,54 @@ fn quoted_as_type( Ok(Value::Type(typ)) } +fn type_as_integer( + mut arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + check_argument_count(1, &arguments, location)?; + + let value = arguments.pop().unwrap().0; + let Value::Type(typ) = value else { + panic!("type_as_integer shold have been called with a type"); + }; + + let Type::Struct(_option_type, mut generics) = return_type.clone() else { + panic!("Expected return type to be an Option"); + }; + + let t = generics.pop().expect("Expected Option to have a T generic type"); + + let is_some; + let value; + + if let Type::Integer(sign, bits) = typ { + is_some = Value::Bool(true); + value = Value::Tuple(vec![ + Value::Bool(match sign { + Signedness::Unsigned => false, + Signedness::Signed => true, + }), + Value::U8(match bits { + IntegerBitSize::One => 1, + IntegerBitSize::Eight => 8, + IntegerBitSize::Sixteen => 16, + IntegerBitSize::ThirtyTwo => 32, + IntegerBitSize::SixtyFour => 64, + }), + ]); + } else { + is_some = Value::Bool(false); + value = Value::Zeroed(t); + } + + let mut fields = HashMap::default(); + fields.insert(Rc::new("_is_some".to_string()), is_some); + fields.insert(Rc::new("_value".to_string()), value); + let option = Value::Struct(fields, return_type); + Ok(option) +} + fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { check_argument_count(2, &arguments, location)?; diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index ee44cf3f268..a9b77fdd969 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -1,6 +1,10 @@ use crate::cmp::Eq; +use crate::option::Option; impl Type { + #[builtin(type_as_integer)] + fn as_integer(self) -> Option<(bool, u8)> {} + #[builtin(type_is_field)] fn is_field(self) -> bool {} } diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index 82c62c27619..2747dbb846b 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -16,5 +16,18 @@ fn main() { // Check Type::is_field assert(field_type_1.is_field()); assert(!i32_type.is_field()); + + // Check Type::as_integer + assert(field_type_1.as_integer().is_none()); + + let (signed, bits) = i32_type.as_integer().unwrap(); + assert(signed); + assert_eq(bits, 32); + + let an_u8: u8 = 0; + let u8_type = type_of(an_u8); + let (signed, bits) = u8_type.as_integer().unwrap(); + assert(!signed); + assert_eq(bits, 8); } } From a63d36e7ab40a9e59cd1daf2442658e1de160d12 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 17:44:44 -0300 Subject: [PATCH 03/16] Add some comments for clarity --- .../noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 83b96a0cb09..5e2805c8ccb 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -438,6 +438,7 @@ fn quoted_as_type( Ok(Value::Type(typ)) } +// fn as_integer(self) -> Option<(bool, u8)> fn type_as_integer( mut arguments: Vec<(Value, Location)>, return_type: Type, @@ -486,6 +487,7 @@ fn type_as_integer( Ok(option) } +// fn type_eq(_first: Type, _second: Type) -> bool fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { check_argument_count(2, &arguments, location)?; @@ -494,6 +496,7 @@ fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult Ok(Value::Bool(value1 == value2)) } +// fn is_field(self) -> bool fn type_is_field(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { check_argument_count(1, &arguments, location)?; From c4161830bd7ca5d1e8bbb6fd7e9184762f86ea82 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 17:55:06 -0300 Subject: [PATCH 04/16] Extract `Value::option` --- .../src/hir/comptime/interpreter/builtin.rs | 27 +++++-------------- .../noirc_frontend/src/hir/comptime/value.rs | 23 ++++++++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 5e2805c8ccb..5155bfef25f 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -451,18 +451,8 @@ fn type_as_integer( panic!("type_as_integer shold have been called with a type"); }; - let Type::Struct(_option_type, mut generics) = return_type.clone() else { - panic!("Expected return type to be an Option"); - }; - - let t = generics.pop().expect("Expected Option to have a T generic type"); - - let is_some; - let value; - - if let Type::Integer(sign, bits) = typ { - is_some = Value::Bool(true); - value = Value::Tuple(vec![ + let option_value = if let Type::Integer(sign, bits) = typ { + Some(Value::Tuple(vec![ Value::Bool(match sign { Signedness::Unsigned => false, Signedness::Signed => true, @@ -474,16 +464,13 @@ fn type_as_integer( IntegerBitSize::ThirtyTwo => 32, IntegerBitSize::SixtyFour => 64, }), - ]); + ])) } else { - is_some = Value::Bool(false); - value = Value::Zeroed(t); - } + None + }; + + let option = Value::option(return_type, option_value); - let mut fields = HashMap::default(); - fields.insert(Rc::new("_is_some".to_string()), is_some); - fields.insert(Rc::new("_value".to_string()), value); - let option = Value::Struct(fields, return_type); Ok(option) } diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index 0959e4c17ac..92f43145c77 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -397,6 +397,29 @@ impl Value { value => Err(InterpreterError::CannotInlineMacro { value, location }), } } + + /// Creates a value that holds an `Option``. + /// `option_type` must be a Type referencing the `Option` type. + pub(crate) fn option(option_type: Type, value: Option) -> Value { + let Type::Struct(shared_option_type, mut generics) = option_type.clone() else { + panic!("Expected type to be a struct"); + }; + + let shared_option_type = shared_option_type.borrow(); + assert_eq!(shared_option_type.name.0.contents, "Option"); + + let t = generics.pop().expect("Expected Option to have a T generic type"); + + let (is_some, value) = match value { + Some(value) => (Value::Bool(true), value), + None => (Value::Bool(false), Value::Zeroed(t)), + }; + + let mut fields = HashMap::default(); + fields.insert(Rc::new("_is_some".to_string()), is_some); + fields.insert(Rc::new("_value".to_string()), value); + Value::Struct(fields, option_type) + } } /// Unwraps an Rc value without cloning the inner value if the reference count is 1. Clones otherwise. From 61814295431ebc0d7a80c3ed8187a2b5a407092e Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 18:21:25 -0300 Subject: [PATCH 05/16] Inline value Co-authored-by: jfecher --- .../noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 5155bfef25f..96206a26af5 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -469,9 +469,7 @@ fn type_as_integer( None }; - let option = Value::option(return_type, option_value); - - Ok(option) + Ok(Value::option(return_type, option_value)) } // fn type_eq(_first: Type, _second: Type) -> bool From b954f4ec2a90fb0f616909a4a0767ed03f9ab786 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 1 Aug 2024 18:22:33 -0300 Subject: [PATCH 06/16] Inline value Co-authored-by: jfecher --- .../noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 96206a26af5..5a89e11fd41 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -490,9 +490,7 @@ fn type_is_field(mut arguments: Vec<(Value, Location)>, location: Location) -> I panic!("type_is_field shold have been called with a type"); }; - let is_field = matches!(typ, Type::FieldElement); - - Ok(Value::Bool(is_field)) + Ok(Value::Bool(matches!(typ, Type::FieldElement))) } fn type_of(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { From 04ec389feecfe4b07852bff13adf016d61289046 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 06:59:04 -0300 Subject: [PATCH 07/16] Use zeroed instead of Value::Zeroed --- .../src/hir/comptime/interpreter/builtin.rs | 25 ++++++++++++++++++- .../noirc_frontend/src/hir/comptime/value.rs | 23 ----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 5a89e11fd41..7375dbf7a68 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -469,7 +469,7 @@ fn type_as_integer( None }; - Ok(Value::option(return_type, option_value)) + option(return_type, option_value) } // fn type_eq(_first: Type, _second: Type) -> bool @@ -691,3 +691,26 @@ fn trait_def_as_trait_constraint( Ok(Value::TraitConstraint(trait_id, trait_generics)) } + +/// Creates a value that holds an `Option``. +/// `option_type` must be a Type referencing the `Option` type. +pub(crate) fn option(option_type: Type, value: Option) -> IResult { + let Type::Struct(shared_option_type, mut generics) = option_type.clone() else { + panic!("Expected type to be a struct"); + }; + + let shared_option_type = shared_option_type.borrow(); + assert_eq!(shared_option_type.name.0.contents, "Option"); + + let t = generics.pop().expect("Expected Option to have a T generic type"); + + let (is_some, value) = match value { + Some(value) => (Value::Bool(true), value), + None => (Value::Bool(false), zeroed(t)?), + }; + + let mut fields = HashMap::default(); + fields.insert(Rc::new("_is_some".to_string()), is_some); + fields.insert(Rc::new("_value".to_string()), value); + Ok(Value::Struct(fields, option_type)) +} diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index 92f43145c77..0959e4c17ac 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -397,29 +397,6 @@ impl Value { value => Err(InterpreterError::CannotInlineMacro { value, location }), } } - - /// Creates a value that holds an `Option``. - /// `option_type` must be a Type referencing the `Option` type. - pub(crate) fn option(option_type: Type, value: Option) -> Value { - let Type::Struct(shared_option_type, mut generics) = option_type.clone() else { - panic!("Expected type to be a struct"); - }; - - let shared_option_type = shared_option_type.borrow(); - assert_eq!(shared_option_type.name.0.contents, "Option"); - - let t = generics.pop().expect("Expected Option to have a T generic type"); - - let (is_some, value) = match value { - Some(value) => (Value::Bool(true), value), - None => (Value::Bool(false), Value::Zeroed(t)), - }; - - let mut fields = HashMap::default(); - fields.insert(Rc::new("_is_some".to_string()), is_some); - fields.insert(Rc::new("_value".to_string()), value); - Value::Struct(fields, option_type) - } } /// Unwraps an Rc value without cloning the inner value if the reference count is 1. Clones otherwise. From 4c20a2e723be3d7f63bf20d22fc98c5f815d2672 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 07:02:58 -0300 Subject: [PATCH 08/16] Move methods to Signedness and IntegerBitSize --- compiler/noirc_frontend/src/ast/mod.rs | 21 +++++++++++++++++++ .../src/hir/comptime/interpreter/builtin.rs | 14 +------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/compiler/noirc_frontend/src/ast/mod.rs b/compiler/noirc_frontend/src/ast/mod.rs index ef4932fa9f0..6cefe468cee 100644 --- a/compiler/noirc_frontend/src/ast/mod.rs +++ b/compiler/noirc_frontend/src/ast/mod.rs @@ -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 { vec![Self::One, Self::Eight, Self::ThirtyTwo, Self::SixtyFour] @@ -297,6 +309,15 @@ pub enum Signedness { Signed, } +impl Signedness { + pub fn to_bool(&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. diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 7375dbf7a68..fe922e814ff 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -452,19 +452,7 @@ fn type_as_integer( }; let option_value = if let Type::Integer(sign, bits) = typ { - Some(Value::Tuple(vec![ - Value::Bool(match sign { - Signedness::Unsigned => false, - Signedness::Signed => true, - }), - Value::U8(match bits { - IntegerBitSize::One => 1, - IntegerBitSize::Eight => 8, - IntegerBitSize::Sixteen => 16, - IntegerBitSize::ThirtyTwo => 32, - IntegerBitSize::SixtyFour => 64, - }), - ])) + Some(Value::Tuple(vec![Value::Bool(sign.to_bool()), Value::U8(bits.bit_size())])) } else { None }; From 5ef1f1d14d297a5a2fedf43ad731a33a77f9ebde Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 07:07:42 -0300 Subject: [PATCH 09/16] an_u8 -> a_u8 --- test_programs/compile_success_empty/comptime_type/src/main.nr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_programs/compile_success_empty/comptime_type/src/main.nr b/test_programs/compile_success_empty/comptime_type/src/main.nr index 2747dbb846b..768155f0fc6 100644 --- a/test_programs/compile_success_empty/comptime_type/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type/src/main.nr @@ -24,8 +24,8 @@ fn main() { assert(signed); assert_eq(bits, 32); - let an_u8: u8 = 0; - let u8_type = type_of(an_u8); + let a_u8: u8 = 0; + let u8_type = type_of(a_u8); let (signed, bits) = u8_type.as_integer().unwrap(); assert(!signed); assert_eq(bits, 8); From f648d3095e935981c67372f110b87d4015efa660 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 11:38:22 -0300 Subject: [PATCH 10/16] Introduce a `get_type` function --- .../src/hir/comptime/interpreter/builtin.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index fe922e814ff..6c8428ad468 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -155,6 +155,16 @@ fn get_trait_def(value: Value, location: Location) -> IResult { } } +fn get_type(value: Value, location: Location) -> IResult { + match value { + Value::Type(typ) => Ok(typ), + value => { + let expected = Type::Quoted(QuotedType::Type); + Err(InterpreterError::TypeMismatch { expected, value, location }) + } + } +} + fn get_quoted(value: Value, location: Location) -> IResult> { match value { Value::Code(tokens) => Ok(tokens), @@ -447,9 +457,7 @@ fn type_as_integer( check_argument_count(1, &arguments, location)?; let value = arguments.pop().unwrap().0; - let Value::Type(typ) = value else { - panic!("type_as_integer shold have been called with a type"); - }; + let typ = get_type(value, location)?; let option_value = if let Type::Integer(sign, bits) = typ { Some(Value::Tuple(vec![Value::Bool(sign.to_bool()), Value::U8(bits.bit_size())])) @@ -474,9 +482,7 @@ fn type_is_field(mut arguments: Vec<(Value, Location)>, location: Location) -> I check_argument_count(1, &arguments, location)?; let value = arguments.pop().unwrap().0; - let Value::Type(typ) = value else { - panic!("type_is_field shold have been called with a type"); - }; + let typ = get_type(value, location)?; Ok(Value::Bool(matches!(typ, Type::FieldElement))) } From 2b617e6b4e155d2e10bdb5caee68f7d605220356 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 11:51:29 -0300 Subject: [PATCH 11/16] Extract `check_one_argument` --- .../src/hir/comptime/interpreter/builtin.rs | 95 ++++++++++--------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 6c8428ad468..88a87761908 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -81,6 +81,15 @@ pub(super) fn check_argument_count( } } +pub(super) fn check_one_argument( + mut arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult<(Value, Location)> { + check_argument_count(1, &arguments, location)?; + + Ok(arguments.pop().unwrap()) +} + fn failing_constraint(message: impl Into, location: Location) -> IResult { let message = Some(Value::String(Rc::new(message.into()))); Err(InterpreterError::FailingConstraint { message, location }) @@ -177,12 +186,12 @@ fn get_quoted(value: Value, location: Location) -> IResult> { fn array_len( interner: &NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - match arguments.pop().unwrap().0 { + match argument { Value::Array(values, _) | Value::Slice(values, _) => Ok(Value::U32(values.len() as u32)), value => { let type_var = Box::new(interner.next_type_variable()); @@ -194,12 +203,11 @@ fn array_len( fn as_slice( interner: &NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (array, _) = check_one_argument(arguments, location)?; - let (array, _) = arguments.pop().unwrap(); match array { Value::Array(values, Type::Array(_, typ)) => Ok(Value::Slice(values, Type::Slice(typ))), value => { @@ -226,12 +234,12 @@ fn slice_push_back( /// fn as_type(self) -> Type fn struct_def_as_type( interner: &NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let struct_def = match arguments.pop().unwrap().0 { + let struct_def = match argument { Value::StructDefinition(id) => id, value => { let expected = Type::Quoted(QuotedType::StructDefinition); @@ -253,12 +261,12 @@ fn struct_def_as_type( /// fn generics(self) -> [Quoted] fn struct_def_generics( interner: &NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let argument = check_one_argument(arguments, location)?; - let (struct_def, span) = match arguments.pop().unwrap() { + let (struct_def, span) = match argument { (Value::StructDefinition(id), location) => (id, location.span), value => { let expected = Type::Quoted(QuotedType::StructDefinition); @@ -282,12 +290,12 @@ fn struct_def_generics( /// Returns (name, type) pairs of each field of this StructDefinition fn struct_def_fields( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let argument = check_one_argument(arguments, location)?; - let (struct_def, span) = match arguments.pop().unwrap() { + let (struct_def, span) = match argument { (Value::StructDefinition(id), location) => (id, location.span), value => { let expected = Type::Quoted(QuotedType::StructDefinition); @@ -357,12 +365,12 @@ fn slice_push_front( fn slice_pop_front( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; + let (mut values, typ) = get_slice(interner, argument, location)?; match values.pop_front() { Some(element) => Ok(Value::Tuple(vec![element, Value::Slice(values, typ)])), None => failing_constraint("slice_pop_front called on empty slice", location), @@ -371,12 +379,12 @@ fn slice_pop_front( fn slice_pop_back( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; + let (mut values, typ) = get_slice(interner, argument, location)?; match values.pop_back() { Some(element) => Ok(Value::Tuple(vec![Value::Slice(values, typ), element])), None => failing_constraint("slice_pop_back called on empty slice", location), @@ -400,12 +408,12 @@ fn slice_insert( // fn as_trait_constraint(quoted: Quoted) -> TraitConstraint fn quoted_as_trait_constraint( interpreter: &mut Interpreter, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let tokens = get_quoted(arguments.pop().unwrap().0, location)?; + let tokens = get_quoted(argument, location)?; let quoted = tokens.as_ref().clone(); let trait_bound = parser::trait_bound().parse(quoted).map_err(|mut errors| { @@ -427,12 +435,12 @@ fn quoted_as_trait_constraint( // fn as_type(quoted: Quoted) -> Type fn quoted_as_type( interpreter: &mut Interpreter, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let tokens = get_quoted(arguments.pop().unwrap().0, location)?; + let tokens = get_quoted(argument, location)?; let quoted = tokens.as_ref().clone(); let typ = parser::parse_type().parse(quoted).map_err(|mut errors| { @@ -450,13 +458,11 @@ fn quoted_as_type( // fn as_integer(self) -> Option<(bool, u8)> fn type_as_integer( - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, return_type: Type, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; - - let value = arguments.pop().unwrap().0; + let (value, _) = check_one_argument(arguments, location)?; let typ = get_type(value, location)?; let option_value = if let Type::Integer(sign, bits) = typ { @@ -478,19 +484,16 @@ fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult } // fn is_field(self) -> bool -fn type_is_field(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { - check_argument_count(1, &arguments, location)?; - - let value = arguments.pop().unwrap().0; +fn type_is_field(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + let (value, _) = check_one_argument(arguments, location)?; let typ = get_type(value, location)?; Ok(Value::Bool(matches!(typ, Type::FieldElement))) } -fn type_of(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { - check_argument_count(1, &arguments, location)?; - - let value = arguments.pop().unwrap().0; +// fn type_of(x: T) -> Type +fn type_of(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + let (value, _) = check_one_argument(arguments, location)?; let typ = value.get_type().into_owned(); Ok(Value::Type(typ)) } @@ -498,12 +501,12 @@ fn type_of(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult // fn constraint_hash(constraint: TraitConstraint) -> Field fn trait_constraint_hash( _interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let bound = get_trait_constraint(arguments.pop().unwrap().0, location)?; + let bound = get_trait_constraint(argument, location)?; let mut hasher = std::collections::hash_map::DefaultHasher::new(); bound.hash(&mut hasher); @@ -672,12 +675,12 @@ fn modulus_num_bits( fn trait_def_as_trait_constraint( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> Result { - check_argument_count(1, &arguments, location)?; + let (argument, _) = check_one_argument(arguments, location)?; - let trait_id = get_trait_def(arguments.pop().unwrap().0, location)?; + let trait_id = get_trait_def(argument, location)?; let the_trait = interner.get_trait(trait_id); let trait_generics = vecmap(&the_trait.generics, |generic| { Type::NamedGeneric(generic.type_var.clone(), generic.name.clone(), generic.kind.clone()) @@ -686,7 +689,7 @@ fn trait_def_as_trait_constraint( Ok(Value::TraitConstraint(trait_id, trait_generics)) } -/// Creates a value that holds an `Option``. +/// Creates a value that holds an `Option`. /// `option_type` must be a Type referencing the `Option` type. pub(crate) fn option(option_type: Type, value: Option) -> IResult { let Type::Struct(shared_option_type, mut generics) = option_type.clone() else { From b3b946d685d5dc8c8faf1f29c41234a0530db315 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 11:59:21 -0300 Subject: [PATCH 12/16] Extract `check_two_arguments` --- .../src/hir/comptime/interpreter/builtin.rs | 50 +++++++++++-------- .../src/hir/comptime/interpreter/foreign.rs | 10 ++-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 88a87761908..6941b4508f3 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -90,6 +90,18 @@ pub(super) fn check_one_argument( Ok(arguments.pop().unwrap()) } +pub(super) fn check_two_arguments( + mut arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult<((Value, Location), (Value, Location))> { + check_argument_count(2, &arguments, location)?; + + let argument2 = arguments.pop().unwrap(); + let argument1 = arguments.pop().unwrap(); + + Ok((argument1, argument2)) +} + fn failing_constraint(message: impl Into, location: Location) -> IResult { let message = Some(Value::String(Rc::new(message.into()))); Err(InterpreterError::FailingConstraint { message, location }) @@ -220,13 +232,12 @@ fn as_slice( fn slice_push_back( interner: &NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(2, &arguments, location)?; + let ((slice, _), (element, _)) = check_two_arguments(arguments, location)?; - let (element, _) = arguments.pop().unwrap(); - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; + let (mut values, typ) = get_slice(interner, slice, location)?; values.push_back(element); Ok(Value::Slice(values, typ)) } @@ -326,13 +337,13 @@ fn struct_def_fields( fn slice_remove( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(2, &arguments, location)?; + let ((slice, _), (index, _)) = check_two_arguments(arguments, location)?; - let index = get_u32(arguments.pop().unwrap().0, location)? as usize; - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; + let index = get_u32(index, location)? as usize; + let (mut values, typ) = get_slice(interner, slice, location)?; if values.is_empty() { return failing_constraint("slice_remove called on empty slice", location); @@ -352,13 +363,12 @@ fn slice_remove( fn slice_push_front( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(2, &arguments, location)?; + let ((slice, _), (element, _)) = check_two_arguments(arguments, location)?; - let (element, _) = arguments.pop().unwrap(); - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; + let (mut values, typ) = get_slice(interner, slice, location)?; values.push_front(element); Ok(Value::Slice(values, typ)) } @@ -475,12 +485,10 @@ fn type_as_integer( } // fn type_eq(_first: Type, _second: Type) -> bool -fn type_eq(mut arguments: Vec<(Value, Location)>, location: Location) -> IResult { - check_argument_count(2, &arguments, location)?; +fn type_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + let ((self_type, _), (other_type, _)) = check_two_arguments(arguments, location)?; - let value1 = arguments.pop().unwrap().0; - let value2 = arguments.pop().unwrap().0; - Ok(Value::Bool(value1 == value2)) + Ok(Value::Bool(self_type == other_type)) } // fn is_field(self) -> bool @@ -518,13 +526,13 @@ fn trait_constraint_hash( // fn constraint_eq(constraint_a: TraitConstraint, constraint_b: TraitConstraint) -> bool fn trait_constraint_eq( _interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(2, &arguments, location)?; + let ((value_a, _), (value_b, _)) = check_two_arguments(arguments, location)?; - let constraint_b = get_trait_constraint(arguments.pop().unwrap().0, location)?; - let constraint_a = get_trait_constraint(arguments.pop().unwrap().0, location)?; + let constraint_a = get_trait_constraint(value_a, location)?; + let constraint_b = get_trait_constraint(value_b, location)?; Ok(Value::Bool(constraint_a == constraint_b)) } diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs index fc8c57ab634..064187d682d 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs @@ -8,7 +8,7 @@ use crate::{ macros_api::NodeInterner, }; -use super::builtin::{check_argument_count, get_array, get_u32}; +use super::builtin::{check_two_arguments, get_array, get_u32}; pub(super) fn call_foreign( interner: &mut NodeInterner, @@ -28,13 +28,13 @@ pub(super) fn call_foreign( // poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] fn poseidon2_permutation( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(2, &arguments, location)?; + let ((input, _), (state_length, _)) = check_two_arguments(arguments, location)?; - let state_length = get_u32(arguments.pop().unwrap().0, location)?; - let (input, typ) = get_array(interner, arguments.pop().unwrap().0, location)?; + let (input, typ) = get_array(interner, input, location)?; + let state_length = get_u32(state_length, location)?; let input = try_vecmap(input, |integer| get_field(integer, location))?; From 1958459ac7aef82567d1535e42bc247da1954425 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 12:01:48 -0300 Subject: [PATCH 13/16] Extract `check_three_arguments` --- .../src/hir/comptime/interpreter/builtin.rs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 6941b4508f3..c54239942d8 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -102,6 +102,19 @@ pub(super) fn check_two_arguments( Ok((argument1, argument2)) } +pub(super) fn check_three_arguments( + mut arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult<((Value, Location), (Value, Location), (Value, Location))> { + check_argument_count(3, &arguments, location)?; + + let argument3 = arguments.pop().unwrap(); + let argument2 = arguments.pop().unwrap(); + let argument1 = arguments.pop().unwrap(); + + Ok((argument1, argument2, argument3)) +} + fn failing_constraint(message: impl Into, location: Location) -> IResult { let message = Some(Value::String(Rc::new(message.into()))); Err(InterpreterError::FailingConstraint { message, location }) @@ -403,15 +416,14 @@ fn slice_pop_back( fn slice_insert( interner: &mut NodeInterner, - mut arguments: Vec<(Value, Location)>, + arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - check_argument_count(3, &arguments, location)?; + let ((slice, _), (index, _), (element, _)) = check_three_arguments(arguments, location)?; - let (element, _) = arguments.pop().unwrap(); - let index = get_u32(arguments.pop().unwrap().0, location)?; - let (mut values, typ) = get_slice(interner, arguments.pop().unwrap().0, location)?; - values.insert(index as usize, element); + let index = get_u32(index, location)? as usize; + let (mut values, typ) = get_slice(interner, slice, location)?; + values.insert(index, element); Ok(Value::Slice(values, typ)) } From 142da036137adb07836be97b4082a609ae952804 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 12:05:58 -0300 Subject: [PATCH 14/16] Unpack to tuple, then match only one of the values --- .../noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index c54239942d8..28bc646b66b 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -288,13 +288,13 @@ fn struct_def_generics( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let argument = check_one_argument(arguments, location)?; + let (argument, argument_location) = check_one_argument(arguments, location)?; let (struct_def, span) = match argument { - (Value::StructDefinition(id), location) => (id, location.span), + Value::StructDefinition(id) => (id, argument_location.span), value => { let expected = Type::Quoted(QuotedType::StructDefinition); - return Err(InterpreterError::TypeMismatch { expected, location, value: value.0 }); + return Err(InterpreterError::TypeMismatch { expected, location, value }); } }; From 548e8ca2092cdb4e6edb5edd7ec87da3ea8d5f8a Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 12:26:23 -0300 Subject: [PATCH 15/16] Don't return locations when checking a fixed number of arguments --- .../src/hir/comptime/interpreter/builtin.rs | 58 +++++++++---------- .../src/hir/comptime/interpreter/foreign.rs | 2 +- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 4f7a7e9688a..2e9c405b674 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -86,20 +86,20 @@ pub(super) fn check_argument_count( pub(super) fn check_one_argument( mut arguments: Vec<(Value, Location)>, location: Location, -) -> IResult<(Value, Location)> { +) -> IResult { check_argument_count(1, &arguments, location)?; - Ok(arguments.pop().unwrap()) + Ok(arguments.pop().unwrap().0) } pub(super) fn check_two_arguments( mut arguments: Vec<(Value, Location)>, location: Location, -) -> IResult<((Value, Location), (Value, Location))> { +) -> IResult<(Value, Value)> { check_argument_count(2, &arguments, location)?; - let argument2 = arguments.pop().unwrap(); - let argument1 = arguments.pop().unwrap(); + let argument2 = arguments.pop().unwrap().0; + let argument1 = arguments.pop().unwrap().0; Ok((argument1, argument2)) } @@ -107,12 +107,12 @@ pub(super) fn check_two_arguments( pub(super) fn check_three_arguments( mut arguments: Vec<(Value, Location)>, location: Location, -) -> IResult<((Value, Location), (Value, Location), (Value, Location))> { +) -> IResult<(Value, Value, Value)> { check_argument_count(3, &arguments, location)?; - let argument3 = arguments.pop().unwrap(); - let argument2 = arguments.pop().unwrap(); - let argument1 = arguments.pop().unwrap(); + let argument3 = arguments.pop().unwrap().0; + let argument2 = arguments.pop().unwrap().0; + let argument1 = arguments.pop().unwrap().0; Ok((argument1, argument2, argument3)) } @@ -223,7 +223,7 @@ fn array_len( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; match argument { Value::Array(values, _) | Value::Slice(values, _) => Ok(Value::U32(values.len() as u32)), @@ -241,7 +241,7 @@ fn as_slice( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (array, _) = check_one_argument(arguments, location)?; + let array = check_one_argument(arguments, location)?; match array { Value::Array(values, Type::Array(_, typ)) => Ok(Value::Slice(values, Type::Slice(typ))), @@ -259,7 +259,7 @@ fn slice_push_back( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((slice, _), (element, _)) = check_two_arguments(arguments, location)?; + let (slice, element) = check_two_arguments(arguments, location)?; let (mut values, typ) = get_slice(interner, slice, location)?; values.push_back(element); @@ -272,7 +272,7 @@ fn struct_def_as_type( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let struct_def = match argument { Value::StructDefinition(id) => id, @@ -300,7 +300,7 @@ fn struct_def_generics( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let struct_def = match argument { Value::StructDefinition(id) => id, @@ -330,7 +330,7 @@ fn struct_def_fields( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let struct_def = match argument { Value::StructDefinition(id) => id, @@ -364,7 +364,7 @@ fn slice_remove( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((slice, _), (index, _)) = check_two_arguments(arguments, location)?; + let (slice, index) = check_two_arguments(arguments, location)?; let index = get_u32(index, location)? as usize; let (mut values, typ) = get_slice(interner, slice, location)?; @@ -390,7 +390,7 @@ fn slice_push_front( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((slice, _), (element, _)) = check_two_arguments(arguments, location)?; + let (slice, element) = check_two_arguments(arguments, location)?; let (mut values, typ) = get_slice(interner, slice, location)?; values.push_front(element); @@ -402,7 +402,7 @@ fn slice_pop_front( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let (mut values, typ) = get_slice(interner, argument, location)?; match values.pop_front() { @@ -416,7 +416,7 @@ fn slice_pop_back( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let (mut values, typ) = get_slice(interner, argument, location)?; match values.pop_back() { @@ -430,7 +430,7 @@ fn slice_insert( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((slice, _), (index, _), (element, _)) = check_three_arguments(arguments, location)?; + let (slice, index, element) = check_three_arguments(arguments, location)?; let index = get_u32(index, location)? as usize; let (mut values, typ) = get_slice(interner, slice, location)?; @@ -444,7 +444,7 @@ fn quoted_as_trait_constraint( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let tokens = get_quoted(argument, location)?; let quoted = add_token_spans(tokens.clone(), location.span); @@ -470,7 +470,7 @@ fn quoted_as_type( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let tokens = get_quoted(argument, location)?; let quoted = add_token_spans(tokens.clone(), location.span); @@ -493,7 +493,7 @@ fn type_as_integer( return_type: Type, location: Location, ) -> IResult { - let (value, _) = check_one_argument(arguments, location)?; + let value = check_one_argument(arguments, location)?; let typ = get_type(value, location)?; let option_value = if let Type::Integer(sign, bits) = typ { @@ -507,14 +507,14 @@ fn type_as_integer( // fn type_eq(_first: Type, _second: Type) -> bool fn type_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult { - let ((self_type, _), (other_type, _)) = check_two_arguments(arguments, location)?; + let (self_type, other_type) = check_two_arguments(arguments, location)?; Ok(Value::Bool(self_type == other_type)) } // fn is_field(self) -> bool fn type_is_field(arguments: Vec<(Value, Location)>, location: Location) -> IResult { - let (value, _) = check_one_argument(arguments, location)?; + let value = check_one_argument(arguments, location)?; let typ = get_type(value, location)?; Ok(Value::Bool(matches!(typ, Type::FieldElement))) @@ -522,7 +522,7 @@ fn type_is_field(arguments: Vec<(Value, Location)>, location: Location) -> IResu // fn type_of(x: T) -> Type fn type_of(arguments: Vec<(Value, Location)>, location: Location) -> IResult { - let (value, _) = check_one_argument(arguments, location)?; + let value = check_one_argument(arguments, location)?; let typ = value.get_type().into_owned(); Ok(Value::Type(typ)) } @@ -533,7 +533,7 @@ fn trait_constraint_hash( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let bound = get_trait_constraint(argument, location)?; @@ -550,7 +550,7 @@ fn trait_constraint_eq( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((value_a, _), (value_b, _)) = check_two_arguments(arguments, location)?; + let (value_a, value_b) = check_two_arguments(arguments, location)?; let constraint_a = get_trait_constraint(value_a, location)?; let constraint_b = get_trait_constraint(value_b, location)?; @@ -738,7 +738,7 @@ fn trait_def_as_trait_constraint( arguments: Vec<(Value, Location)>, location: Location, ) -> Result { - let (argument, _) = check_one_argument(arguments, location)?; + let argument = check_one_argument(arguments, location)?; let trait_id = get_trait_def(argument, location)?; let the_trait = interner.get_trait(trait_id); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs index 064187d682d..f0dc2dcf487 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs @@ -31,7 +31,7 @@ fn poseidon2_permutation( arguments: Vec<(Value, Location)>, location: Location, ) -> IResult { - let ((input, _), (state_length, _)) = check_two_arguments(arguments, location)?; + let (input, state_length) = check_two_arguments(arguments, location)?; let (input, typ) = get_array(interner, input, location)?; let state_length = get_u32(state_length, location)?; From d8498948e28dcb4ebf5f2b4b6cb39da91cb5e856 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 2 Aug 2024 16:23:46 -0300 Subject: [PATCH 16/16] to_bool -> is_signed --- compiler/noirc_frontend/src/ast/mod.rs | 2 +- compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/ast/mod.rs b/compiler/noirc_frontend/src/ast/mod.rs index 6cefe468cee..4fb5730e93b 100644 --- a/compiler/noirc_frontend/src/ast/mod.rs +++ b/compiler/noirc_frontend/src/ast/mod.rs @@ -310,7 +310,7 @@ pub enum Signedness { } impl Signedness { - pub fn to_bool(&self) -> bool { + pub fn is_signed(&self) -> bool { match self { Signedness::Unsigned => false, Signedness::Signed => true, diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 2e9c405b674..c92e9a73b5a 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -497,7 +497,7 @@ fn type_as_integer( let typ = get_type(value, location)?; let option_value = if let Type::Integer(sign, bits) = typ { - Some(Value::Tuple(vec![Value::Bool(sign.to_bool()), Value::U8(bits.bit_size())])) + Some(Value::Tuple(vec![Value::Bool(sign.is_signed()), Value::U8(bits.bit_size())])) } else { None };