mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-18 15:31:32 +03:00
fix recursive circuit member function namespace bug
This commit is contained in:
parent
3f10bcfe82
commit
84f634559c
48
README.md
48
README.md
@ -205,6 +205,7 @@ In the underlying circuit this can be thought of as a demultiplexer.
|
||||
```rust
|
||||
function main(a: bool, b: bool) -> u32 {
|
||||
let mut res = 0u32;
|
||||
|
||||
if a {
|
||||
res = 1;
|
||||
} else if b {
|
||||
@ -212,6 +213,7 @@ function main(a: bool, b: bool) -> u32 {
|
||||
} else {
|
||||
res = 3;
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
```
|
||||
@ -299,8 +301,8 @@ function main() -> u32 {
|
||||
## Circuits
|
||||
Circuits in Leo are similar to classes in object oriented langauges. Circuits are defined above functions in a Leo program. Circuits can have one or more members.
|
||||
|
||||
|
||||
Members can be defined as fields which hold primitive values
|
||||
#### Circuit member values
|
||||
Members can be defined as fields which hold primitive values.
|
||||
```rust
|
||||
circuit Point {
|
||||
x: u32
|
||||
@ -312,51 +314,73 @@ function main() -> u32 {
|
||||
}
|
||||
```
|
||||
|
||||
#### Circuit member functions
|
||||
Members can also be defined as functions.
|
||||
```rust
|
||||
circuit Circ {
|
||||
circuit Foo {
|
||||
function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> u32 {
|
||||
let c = Circ { };
|
||||
let c = Foo { };
|
||||
return c.echo(1u32)
|
||||
}
|
||||
```
|
||||
|
||||
#### Circuit member static functions
|
||||
Circuit functions can be made static, enabling them to be called without instantiation.
|
||||
```rust
|
||||
circuit Circ {
|
||||
circuit Foo {
|
||||
static function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> u32 {
|
||||
return Circ::echo(1u32)
|
||||
return Foo::echo(1u32)
|
||||
}
|
||||
```
|
||||
|
||||
The `Self` keyword is supported in circuit functions.
|
||||
#### `Self` and `self`
|
||||
The `Self` keyword references the circuit definition.
|
||||
```rust
|
||||
circuit Circ {
|
||||
circuit Foo {
|
||||
b: bool
|
||||
|
||||
static function new() -> Self {
|
||||
static function new() -> Self { // Self resolves to Foo
|
||||
return Self { b: true }
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> Circ {
|
||||
let c = Circ::new();
|
||||
function main() -> bool {
|
||||
let c = Foo::new();
|
||||
return c.b
|
||||
}
|
||||
```
|
||||
|
||||
The `self` keyword references the circuit's members.
|
||||
```rust
|
||||
circuit Foo {
|
||||
b: bool
|
||||
|
||||
function bar() -> bool {
|
||||
return self.b
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> bool {
|
||||
let c = Foo { b: true };
|
||||
return c.b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Imports
|
||||
Leo supports importing functions and circuits by name into the current file with the following syntax:
|
||||
Leo supports importing functions
|
||||
}
|
||||
} and circuits by name into the current file with the following syntax:
|
||||
|
||||
```rust
|
||||
import [package].[name];
|
||||
|
@ -704,9 +704,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
// access a circuit member using the `self` keyword
|
||||
if let Expression::Identifier(ref identifier) = *circuit_identifier {
|
||||
if identifier.is_self() {
|
||||
let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string());
|
||||
let self_file_scope = new_scope(file_scope.clone(), identifier.name.to_string());
|
||||
let self_function_scope = new_scope(self_file_scope.clone(), identifier.name.to_string());
|
||||
|
||||
let member_value =
|
||||
self.evaluate_identifier(file_scope, self_keyword, &vec![], circuit_member.clone())?;
|
||||
self.evaluate_identifier(self_file_scope, self_function_scope, &vec![], circuit_member.clone())?;
|
||||
|
||||
return Ok(member_value);
|
||||
}
|
||||
@ -730,22 +732,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
Some(member) => {
|
||||
match &member.1 {
|
||||
ConstrainedValue::Function(ref _circuit_identifier, ref _function) => {
|
||||
// Pass static circuit fields into function call by value
|
||||
// Pass circuit members into function call by value
|
||||
for stored_member in members {
|
||||
match &stored_member.1 {
|
||||
ConstrainedValue::Function(_, _) => {}
|
||||
ConstrainedValue::Static(_) => {}
|
||||
_ => {
|
||||
let circuit_scope = new_scope(file_scope.clone(), circuit_name.to_string());
|
||||
let function_scope = new_scope(circuit_scope, member.0.to_string());
|
||||
let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string());
|
||||
let self_keyword = new_scope(circuit_scope, SELF_KEYWORD.to_string());
|
||||
let field = new_scope(self_keyword, stored_member.0.to_string());
|
||||
|
||||
self.store(field, stored_member.1.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ConstrainedValue::Static(value) => {
|
||||
return Err(ExpressionError::invalid_static_access(value.to_string(), span));
|
||||
}
|
||||
@ -831,7 +826,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
*function.clone(),
|
||||
)?;
|
||||
|
||||
let (outer_scope, function_call) = function_value.extract_function(file_scope, span.clone())?;
|
||||
let (outer_scope, function_call) = function_value.extract_function(file_scope.clone(), span.clone())?;
|
||||
|
||||
let name_unique = format!(
|
||||
"function call {} {}:{}",
|
||||
|
@ -14,6 +14,10 @@ pub fn new_scope(outer: String, inner: String) -> String {
|
||||
format!("{}_{}", outer, inner)
|
||||
}
|
||||
|
||||
pub fn is_in_scope(current_scope: &String, desired_scope: &String) -> bool {
|
||||
current_scope.ends_with(desired_scope)
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
@ -3,6 +3,7 @@
|
||||
use crate::{
|
||||
constraints::boolean::{allocate_bool, new_bool_constant},
|
||||
errors::{ExpressionError, FieldError, ValueError},
|
||||
is_in_scope,
|
||||
new_scope,
|
||||
FieldType,
|
||||
GroupType,
|
||||
@ -122,8 +123,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
ConstrainedValue::Function(circuit_identifier, function) => {
|
||||
let mut outer_scope = scope.clone();
|
||||
// If this is a circuit function, evaluate inside the circuit scope
|
||||
if circuit_identifier.is_some() {
|
||||
outer_scope = new_scope(scope, circuit_identifier.unwrap().to_string());
|
||||
if let Some(identifier) = circuit_identifier {
|
||||
// avoid creating recursive scope
|
||||
if !is_in_scope(&scope, &identifier.name.to_string()) {
|
||||
outer_scope = new_scope(scope, identifier.name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Ok((outer_scope, function.clone()))
|
||||
|
17
compiler/tests/circuits/member_function_nested.leo
Normal file
17
compiler/tests/circuits/member_function_nested.leo
Normal file
@ -0,0 +1,17 @@
|
||||
circuit Foo {
|
||||
x: u32,
|
||||
|
||||
function add_x(y: u32) -> u32 {
|
||||
return self.x + y
|
||||
}
|
||||
|
||||
function call_add_x(y: u32) -> u32 {
|
||||
return self.add_x(y)
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> u32 {
|
||||
let f = Foo { x: 1u32 };
|
||||
|
||||
return f.call_add_x(1u32)
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
get_error,
|
||||
get_output,
|
||||
integers::u32::output_one,
|
||||
integers::u32::{output_number, output_one},
|
||||
parse_program,
|
||||
EdwardsConstrainedValue,
|
||||
EdwardsTestCompiler,
|
||||
@ -138,6 +138,14 @@ fn test_member_function_invalid() {
|
||||
expect_fail(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_function_nested() {
|
||||
let bytes = include_bytes!("member_function_nested.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
output_number(program, 2u32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_static_function() {
|
||||
let bytes = include_bytes!("member_static_function.leo");
|
||||
|
Loading…
Reference in New Issue
Block a user