c3c icon indicating copy to clipboard operation
c3c copied to clipboard

`return` in contracts have undesirable effects, consider a change

Open vssukharev opened this issue 11 months ago • 6 comments

Let we have such code:

import std::collections::list;
def IntList = List(<int>);
<*
 @require {| IntList* l = (IntList*) data; return l.allocator && l.capacity; |}
*>
fn void f(void* data)
{
  // ...
}

It fails compilation with the following error:

 5: def IntList = List(<int>);
 6: 
 7: <*
 8:  @require {| IntList* l = (IntList*) data; return l.allocator && l.capacity; |}
                                                     ^
(/home/starleks/coding/probe/c3-sandbox/main.c3:8:50) Error: Expected ';'

vssukharev avatar Jan 09 '25 11:01 vssukharev

This is due to return being handled as a token inside of contracts.

We could possibly replace this with something else, such as:

$return
$return()
$$return
$$RETURN
$RETURN
@return
$ret
$ret()
$$RET
$RET
@ret
$_
$$_

lerno avatar Jan 11 '25 03:01 lerno

<*
 @ensure $return() > 0
*>
fn int foo() { ... }

<*
 @ensure $ret() > 0
*>
fn int foo() { ... }

<*
 @ensure $_ > 0
*>
fn int foo() { ... }

lerno avatar Jan 11 '25 03:01 lerno

An advantage would be that the lexer would no longer need to special case lexing for return inside of the <* *>

lerno avatar Jan 11 '25 03:01 lerno

We have already $vaarg which is context-dependent compile time variable. Technically, we could explain the reason for $return or $ret the same.

vssukharev avatar Jan 11 '25 10:01 vssukharev

{| |} is not in 0.7 now, but I still consider this as something that needs to change. However I am unsure what the exact name should be.

lerno avatar Mar 10 '25 09:03 lerno

Ping

lerno avatar Jun 29 '25 19:06 lerno

Since expression blocks are no longer a thing, return should no longer be an issue.

lerno avatar Jul 29 '25 19:07 lerno

So I'm going to close this, but reopen it if there are further ambiguities around this.

lerno avatar Jul 29 '25 19:07 lerno