Merge pull request #493 from AleoHQ/fix/self-access-scope

fix accessing self variable inside nested scope bug
This commit is contained in:
Howard Wu 2020-12-11 17:33:32 -04:00 committed by GitHub
commit 878f0a8461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 1 deletions

View File

@ -36,6 +36,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
indicator: &Boolean,
block: Block,
return_type: Option<Type>,
declared_circuit_reference: &str,
mut_self: bool,
) -> StatementResult<Vec<IndicatorAndConstrainedValue<F, G>>> {
let mut results = Vec::with_capacity(block.statements.len());
@ -48,7 +49,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
indicator,
statement,
return_type.clone(),
"",
declared_circuit_reference,
mut_self,
)?;

View File

@ -52,6 +52,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
indicator: &Boolean,
statement: ConditionalStatement,
return_type: Option<Type>,
declared_circuit_reference: &str,
mut_self: bool,
span: &Span,
) -> StatementResult<Vec<IndicatorAndConstrainedValue<F, G>>> {
@ -96,6 +97,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
&branch_1_indicator,
statement.block,
return_type.clone(),
declared_circuit_reference,
mut_self,
)?;
@ -125,6 +127,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
&branch_2_indicator,
*nested,
return_type,
declared_circuit_reference,
mut_self,
span,
)?,
@ -135,6 +138,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
&branch_2_indicator,
block,
return_type,
declared_circuit_reference,
mut_self,
)?,
},

View File

@ -48,6 +48,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
stop: Expression,
block: Block,
return_type: Option<Type>,
declared_circuit_reference: &str,
mut_self: bool,
span: &Span,
) -> StatementResult<Vec<IndicatorAndConstrainedValue<F, G>>> {
@ -75,6 +76,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
indicator,
block.clone(),
return_type.clone(),
declared_circuit_reference,
mut_self,
)?;

View File

@ -90,6 +90,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
indicator,
statement,
return_type,
declared_circuit_reference,
mut_self,
&span,
)?;
@ -107,6 +108,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
start_stop.1,
block,
return_type,
declared_circuit_reference,
mut_self,
&span,
)?;

View File

@ -150,6 +150,14 @@ fn test_mutate_self_variable() {
assert_satisfied(program);
}
#[test]
fn test_mutate_self_variable_conditional() {
let program_string = include_str!("mut_self_variable_conditional.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_mutate_self_variable_fail() {
let program_string = include_str!("mut_self_variable_fail.leo");

View File

@ -0,0 +1,15 @@
function main() {
let mut f = Foo { a: 0u32 };
f.bar();
}
circuit Foo {
a: u32
function bar(mut self) {
if true {
self.a = 5u32; // Mutating a variable inside a conditional statement should work.
}
}
}