apply suggestions
This commit is contained in:
parent
67aa4217dd
commit
bc25bc4816
5 changed files with 26 additions and 23 deletions
|
@ -752,6 +752,12 @@ pub enum TypedExpression<'ast, T> {
|
|||
Int(IntExpression<'ast, T>),
|
||||
}
|
||||
|
||||
impl<'ast, T> TypedExpression<'ast, T> {
|
||||
pub fn empty_tuple() -> TypedExpression<'ast, T> {
|
||||
TypedExpression::Tuple(TupleExpressionInner::Value(vec![]).annotate(TupleType::new(vec![])))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T> From<BooleanExpression<'ast, T>> for TypedExpression<'ast, T> {
|
||||
fn from(e: BooleanExpression<'ast, T>) -> TypedExpression<T> {
|
||||
TypedExpression::Boolean(e)
|
||||
|
|
|
@ -887,6 +887,10 @@ impl<S> GType<S> {
|
|||
pub fn uint<W: Into<UBitwidth>>(b: W) -> Self {
|
||||
GType::Uint(b.into())
|
||||
}
|
||||
|
||||
pub fn is_empty_tuple(&self) -> bool {
|
||||
matches!(self, GType::Tuple(ty) if ty.elements.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: fmt::Display + PartialEq + fmt::Debug> Type<'ast, T> {
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
## Functions
|
||||
|
||||
Functions are declared using the `def` keyword. A function's signature has to be explicitly provided.
|
||||
Its arguments are type annotated, just like variables, and, if the function returns a value,
|
||||
the return type must be specified after an arrow `->`.
|
||||
|
||||
A function has to be declared at the top level before it is called.
|
||||
|
||||
```zokrates
|
||||
{{#include ../../../zokrates_cli/examples/book/function_declaration.zok}}
|
||||
```
|
||||
|
||||
A function's signature has to be explicitly provided.
|
||||
|
||||
A function can be generic over any number of values of type `u32`.
|
||||
|
||||
```zokrates
|
||||
|
@ -18,4 +20,8 @@ The generic parameters can be provided explicitly, especially when they cannot b
|
|||
|
||||
```zokrates
|
||||
{{#include ../../../zokrates_cli/examples/book/explicit_generic_parameters.zok}}
|
||||
```
|
||||
```
|
||||
|
||||
Functions that "don't" return a value, actually return an empty tuple `()`.
|
||||
When a function returns an empty tuple `()`, the return type can be omitted from
|
||||
the signature. In this case, a return statement can be omitted.
|
1
zokrates_cli/examples/compile_errors/no_return.zok
Normal file
1
zokrates_cli/examples/compile_errors/no_return.zok
Normal file
|
@ -0,0 +1 @@
|
|||
def main() -> field {}
|
|
@ -1186,16 +1186,10 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
}
|
||||
|
||||
if !found_return {
|
||||
match &s.output {
|
||||
box DeclarationType::Tuple(tuple_type)
|
||||
if tuple_type.elements.is_empty() =>
|
||||
{
|
||||
statements_checked.push(TypedStatement::Return(TypedExpression::Tuple(
|
||||
TupleExpressionInner::Value(vec![])
|
||||
.annotate(TupleType::new(vec![])),
|
||||
)))
|
||||
}
|
||||
_ => {
|
||||
match (&*s.output).is_empty_tuple() {
|
||||
true => statements_checked
|
||||
.push(TypedStatement::Return(TypedExpression::empty_tuple())),
|
||||
false => {
|
||||
errors.push(ErrorInner {
|
||||
pos: Some(pos),
|
||||
message: "Expected a return statement".to_string(),
|
||||
|
@ -1877,11 +1871,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
}
|
||||
.map_err(|e| vec![e])
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
Ok(TupleExpressionInner::Value(vec![])
|
||||
.annotate(TupleType::new(vec![]))
|
||||
.into())
|
||||
})?;
|
||||
.unwrap_or_else(|| Ok(TypedExpression::empty_tuple()))?;
|
||||
|
||||
let res = match TypedExpression::align_to_type(e_checked.clone(), &return_type)
|
||||
.map_err(|e| {
|
||||
|
@ -4700,11 +4690,7 @@ mod tests {
|
|||
10u32.into(),
|
||||
for_statements_checked,
|
||||
),
|
||||
TypedStatement::Return(
|
||||
TupleExpressionInner::Value(vec![])
|
||||
.annotate(TupleType::new(vec![]))
|
||||
.into(),
|
||||
),
|
||||
TypedStatement::Return(TypedExpression::empty_tuple()),
|
||||
];
|
||||
|
||||
let foo = Function {
|
||||
|
|
Loading…
Reference in a new issue