fix tests

This commit is contained in:
collin 2020-12-01 15:37:44 -05:00
parent 8457b05354
commit d8c08fedd7
22 changed files with 59 additions and 41 deletions

View File

@ -76,6 +76,16 @@ impl Function {
self.input.iter().find(|param| param.is_mut_self()).is_some()
}
///
/// Returns a vector of [&FunctionInput] removing `self` and `mut self` inputs.
///
pub fn filter_self_inputs(&self) -> Vec<&FunctionInput> {
self.input
.iter()
.filter(|input| !input.is_self())
.collect::<Vec<&FunctionInput>>()
}
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "function {}", self.identifier)?;

View File

@ -206,7 +206,7 @@ impl ExpressionError {
}
pub fn undefined_identifier(identifier: Identifier) -> Self {
let message = format!("cannot find value `{}` in this scope", identifier.name);
let message = format!("Cannot find value `{}` in this scope", identifier.name);
Self::new_from_span(message, identifier.span)
}

View File

@ -42,8 +42,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
) -> Result<ConstrainedValue<F, G>, FunctionError> {
let function_name = new_scope(scope, function.get_name());
// Store if function contains input `mut self`.
let mut_self = function.contains_mut_self();
// Store input values as new variables in resolved program
for (input_model, input_expression) in function.input.iter().zip(input.into_iter()) {
for (input_model, input_expression) in function.filter_self_inputs().iter().zip(input.into_iter()) {
let (name, value) = match input_model {
FunctionInput::InputKeyword(keyword) => {
let value =
@ -87,8 +90,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
self.store(input_program_identifier, value);
}
let mut_self = function.contains_mut_self();
// Evaluate every statement in the function and save all potential results
let mut results = vec![];

View File

@ -12,7 +12,7 @@ circuit Foo {
function main() {
let a = Foo { x: 1u32 };
let b = a.call_add_x(1u32);
let b = a.add_x(1u32);
console.assert(b == 2u32);
}

View File

@ -1,5 +1,5 @@
circuit Foo {
static function echo(x: u32) -> u32 {
function echo(x: u32) -> u32 {
return x
}
}

View File

@ -5,5 +5,5 @@ circuit Foo {
}
function main() {
let err = Foo::echo(1u32); // echo is a non-static function and must be accessed using `.`
let err = Foo::echo(1u32); // Correct, echo is a static function and must be accessed using `::`
}

View File

@ -1,11 +1,11 @@
circuit Foo {
static function qux() {}
function qux() {}
static function bar() {
function bar() {
Self::qux();
}
static function baz() {
function baz() {
Self::bar();
}
}

View File

@ -1,5 +1,5 @@
circuit Foo {
static function echo(x: u32) -> u32 {
function echo(x: u32) -> u32 {
return x
}
}

View File

@ -1,7 +1,7 @@
circuit Foo {
foo: u32,
static function bar() -> u32 {
function bar() -> u32 {
return 1u32
}
}

View File

@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_compiler_error,
expect_symbol_table_error,
expect_type_inference_error,
parse_program,
};
use crate::{assert_satisfied, expect_compiler_error, expect_type_inference_error, parse_program};
// Expressions
@ -125,9 +119,9 @@ fn test_member_static_function_nested() {
#[test]
fn test_member_static_function_invalid() {
let bytes = include_bytes!("member_static_function_invalid.leo");
let error = parse_program(bytes).err().unwrap();
let program = parse_program(bytes).unwrap();
expect_type_inference_error(error)
assert_satisfied(program)
}
#[test]
@ -225,9 +219,9 @@ fn test_self_member_pass() {
#[test]
fn test_self_member_invalid() {
let bytes = include_bytes!("self_member_invalid.leo");
let program = parse_program(bytes).unwrap();
let error = parse_program(bytes).err().unwrap();
let _err = expect_compiler_error(program);
expect_type_inference_error(error);
}
#[test]

View File

@ -3,7 +3,7 @@ circuit Foo {
function bar() {}
function set_a(new: u8) {
function set_a(mut self, new: u8) {
self.bar = new;
}
}

View File

@ -1,9 +1,9 @@
circuit Foo {
a: u8,
static function bar() {}
function bar() {}
function set_a(new: u8) {
function set_a(mut self, new: u8) {
self.bar = new;
}
}

View File

@ -1,7 +1,7 @@
circuit Foo {
mut a: u8,
a: u8,
function set_a(new: u8) {
function set_a(mut self, new: u8) {
self.a = new;
console.assert(self.a == new);
}

View File

@ -1,7 +1,7 @@
circuit Foo {
a: u8,
function set_a(new: u8) {
function set_a(self, new: u8) {
self.a = new;
}
}

View File

@ -1,5 +1,5 @@
circuit Foo {
static function bar() {}
function bar() {}
}
function main() {

View File

@ -1,5 +1,5 @@
circuit Foo {
mut a: u8,
a: u8,
}
function main() {

View File

@ -3,7 +3,7 @@ circuit Foo {
}
function main() {
let mut f = Foo { a: 0u8 };
let f = Foo { a: 0u8 };
f.a = 1u8;
}

View File

@ -1,11 +1,11 @@
circuit PedersenHash {
parameters: [u32; 512]
static function new(parameters: [u32; 512]) -> Self {
function new(parameters: [u32; 512]) -> Self {
return Self { parameters: parameters }
}
function hash(bits: [bool; 512]) -> u32 {
function hash(self, bits: [bool; 512]) -> u32 {
let mut digest: u32 = 0;
for i in 0..512 {
let base = if bits[i] ? self.parameters[i] : 0u32;

View File

@ -1,7 +1,7 @@
circuit Foo {
f: u32,
function bar() -> u32 {
function bar(self) -> u32 {
return self.f
}
}

View File

@ -53,7 +53,7 @@ fn test_undefined() {
" 2 | return a",
" | ^",
" |",
" = cannot find value `a` in this scope",
" = Cannot find value `a` in this scope",
]
.join("\n")
);

View File

@ -137,6 +137,16 @@ impl FunctionType {
pub fn contains_self(&self) -> bool {
self.inputs.iter().find(|param| param.is_self()).is_some()
}
///
/// Returns a vector of [&FunctionInputType] removing `self` and `mut self` inputs.
///
pub fn filter_self_inputs(&self) -> Vec<&FunctionInputType> {
self.inputs
.iter()
.filter(|input| !input.is_self())
.collect::<Vec<&FunctionInputType>>()
}
}
impl PartialEq for FunctionType {

View File

@ -1083,8 +1083,6 @@ impl Frame {
// Case 2: no static call + no self keywords => Error
// Case 3: static call + no self keywords => Ok
// Case 4: no static call + self keyword => Ok
println!("static {}", is_static);
println!("function contains self {}", function_type.contains_self());
if is_static && function_type.contains_self() {
return Err(FrameError::self_not_available(&identifier.span));
} else if !is_static && !function_type.contains_self() {
@ -1112,12 +1110,17 @@ impl Frame {
let function_type = self.parse_function_name(expression, span)?;
// Check the length of arguments
if function_type.num_inputs() != inputs.len() {
return Err(FrameError::num_inputs(function_type.num_inputs(), inputs.len(), span));
let num_inputs = function_type.num_inputs();
if num_inputs != inputs.len() {
return Err(FrameError::num_inputs(num_inputs, inputs.len(), span));
}
// Filter out `self` and `mut self` keywords.
let expected_inputs = function_type.filter_self_inputs();
// Assert function inputs are correct types.
for (expected_input, actual_input) in function_type.inputs.iter().zip(inputs) {
for (expected_input, actual_input) in expected_inputs.iter().zip(inputs) {
// Parse expected input type.
let expected_type = expected_input.type_();