fix circuit selftype error bug

This commit is contained in:
collin 2020-12-04 17:56:59 -05:00
parent 0f91630a9f
commit cd53cba77a
2 changed files with 14 additions and 20 deletions

View File

@ -17,6 +17,7 @@
//! Enforces that one return value is produced in a compiled Leo program. //! Enforces that one return value is produced in a compiled Leo program.
use crate::{ use crate::{
check_return_type,
errors::StatementError, errors::StatementError,
get_indicator_value, get_indicator_value,
program::ConstrainedProgram, program::ConstrainedProgram,
@ -76,13 +77,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
for (indicator, result) in results.into_iter() { for (indicator, result) in results.into_iter() {
// Error if a statement returned a result with an incorrect type // Error if a statement returned a result with an incorrect type
let result_type = result.to_type(span)?; let result_type = result.to_type(span)?;
if return_type != result_type { check_return_type(&return_type, &result_type, span)?;
return Err(StatementError::arguments_type(
&return_type,
&result_type,
span.to_owned(),
));
}
if get_indicator_value(&indicator) { if get_indicator_value(&indicator) {
// Error if we already have a return value. // Error if we already have a return value.

View File

@ -24,21 +24,18 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem, gadgets::r1cs::ConstraintSystem,
}; };
fn check_return_type(expected: Option<Type>, actual: Type, span: &Span) -> Result<(), StatementError> { /// Returns `Ok` if the expected type == actual type, returns `Err` otherwise.
match expected { pub fn check_return_type(expected: &Type, actual: &Type, span: &Span) -> Result<(), StatementError> {
Some(expected) => {
if expected.ne(&actual) { if expected.ne(&actual) {
if (expected.is_self() && actual.is_circuit()) || expected.eq_flat(&actual) { // If the return type is `SelfType` returning the circuit type is okay.
return Ok(()); return if (expected.is_self() && actual.is_circuit()) || expected.eq_flat(&actual) {
Ok(())
} else { } else {
return Err(StatementError::arguments_type(&expected, &actual, span.to_owned())); Err(StatementError::arguments_type(&expected, &actual, span.to_owned()))
} };
} }
Ok(()) Ok(())
} }
None => Ok(()),
}
}
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> { impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_return_statement<CS: ConstraintSystem<F>>( pub fn enforce_return_statement<CS: ConstraintSystem<F>>(
@ -53,7 +50,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let result = self.enforce_operand(cs, file_scope, function_scope, return_type.clone(), expression, span)?; let result = self.enforce_operand(cs, file_scope, function_scope, return_type.clone(), expression, span)?;
// Make sure we return the correct type. // Make sure we return the correct type.
check_return_type(return_type, result.to_type(&span)?, span)?; if let Some(expected) = return_type {
check_return_type(&expected, &result.to_type(span)?, span)?;
}
Ok(result) Ok(result)
} }