Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return in contracts have undesirable effects, consider a change #1800

Open
vssukharev opened this issue Jan 9, 2025 · 4 comments
Open

return in contracts have undesirable effects, consider a change #1800

vssukharev opened this issue Jan 9, 2025 · 4 comments
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request

Comments

@vssukharev
Copy link
Contributor

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 ';'
@lerno lerno added Enhancement Request New feature or request Discussion needed This feature needs discussion to iron out details labels Jan 9, 2025
@lerno lerno changed the title Expression block in a contract doesn't work return in contracts have undesirable effects, consider a change Jan 11, 2025
@lerno
Copy link
Collaborator

lerno commented Jan 11, 2025

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
Copy link
Collaborator

lerno commented Jan 11, 2025

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

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

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

@lerno
Copy link
Collaborator

lerno commented Jan 11, 2025

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

@vssukharev
Copy link
Contributor Author

vssukharev commented Jan 11, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants