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

View File

@ -24,20 +24,17 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
fn check_return_type(expected: Option<Type>, actual: Type, span: &Span) -> Result<(), StatementError> {
match expected {
Some(expected) => {
if expected.ne(&actual) {
if (expected.is_self() && actual.is_circuit()) || expected.eq_flat(&actual) {
return Ok(());
} else {
return Err(StatementError::arguments_type(&expected, &actual, span.to_owned()));
}
}
/// Returns `Ok` if the expected type == actual type, returns `Err` otherwise.
pub fn check_return_type(expected: &Type, actual: &Type, span: &Span) -> Result<(), StatementError> {
if expected.ne(&actual) {
// If the return type is `SelfType` returning the circuit type is okay.
return if (expected.is_self() && actual.is_circuit()) || expected.eq_flat(&actual) {
Ok(())
}
None => Ok(()),
} else {
Err(StatementError::arguments_type(&expected, &actual, span.to_owned()))
};
}
Ok(())
}
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
@ -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)?;
// 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)
}