Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

closures can't be assigned to local variables #152

Open
sternenseemann opened this issue Jan 25, 2021 · 3 comments
Open

closures can't be assigned to local variables #152

sternenseemann opened this issue Jan 25, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@sternenseemann
Copy link

Taken from the README with fixed syntax:

closure rand_t() -> int;

fn secure_random() -> int {
    return 42;
}

fn main() {
    rand_t rand = secure_random;
}

Compiling fails:

[ERROR] incompatible types ::zztest::main::rand_t and ::zztest::main::secure_random
 --> /home/lukas/src/misc/zztest/src/main.zz:8:5
  |
8 |     rand_t rand = secure_random;␊
  |     ^-------------------------^
  |
  = this expression is unprovable over incompatible types

 --> /home/lukas/src/misc/zztest/src/main.zz:8:5
  |
8 |     rand_t rand = secure_random;␊
  |     ^-------------------------^
  |
  = rand := ::zztest::main::rand_t Uninitialized

 --> /home/lukas/src/misc/zztest/src/main.zz:3:1
  |
3 | fn secure_random() -> int {␊
  | ...
6 | fn main() -> int {␊
  | ^
  |
  = ::zztest::main::secure_random := ::zztest::main::secure_random Uninitialized
sternenseemann added a commit to sternenseemann/zz that referenced this issue Jan 25, 2021
This should improve documentation somewhat as the examples actually work
after being copy pasted.

Note that the example for closures isn't fixed in this commit as I
couldn't figure out how to fix it yet. See zetzit#152.
@aep
Copy link
Collaborator

aep commented Jan 26, 2021

ah yes, closures can't be assigned to local variables. i dont know why, this looks like a bug.

@aep aep changed the title closures don't work as documented closures can't be assigned to local variables Jan 26, 2021
@aep aep added the bug Something isn't working label Jan 26, 2021
@aep
Copy link
Collaborator

aep commented Jan 26, 2021

oh i remember why. fixing this is hard because closures are emitted as structures for capture context.
this is a corner case where there's no special assignment case in symbolic, its just passed as literal = into C, and using = for structs is not legal in C. We'd either need yet another special case or finally some more generic way to assignment.

sternenseemann added a commit to sternenseemann/zz that referenced this issue Mar 7, 2021
This should improve documentation somewhat as the examples actually work
after being copy pasted.

Note that the example for closures isn't fixed in this commit as I
couldn't figure out how to fix it yet. See zetzit#152.
@jacereda
Copy link

jacereda commented May 4, 2021

C should handle = for structs AFAIK...


struct foo { void * p; int i; };

struct foo test() {
  struct foo a = {0,1};
  struct foo b;
  b = a;
  return b;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants