mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-19 15:41:36 +03:00
fix circuit selftype error bug
This commit is contained in:
parent
0f91630a9f
commit
cd53cba77a
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user