Did the fix work ?
.. anvil-playground::
:playground-url: https://anvil.capstone.kisp-lab.org
chan foobar_ch {
left req : (logic[8]@res),
right res : (logic[8]@#1)
}
func is_even(x){
x & 8'd1 == 8'd0
}
func answer_to_universe(x){
if (call is_even(x)){
8'd42
}
else{
8'd0
}
}
proc Foo(ep : left foobar_ch) {
reg cycle_count : logic[8];
reg ans : logic[8];
loop {
let x = recv ep.req >>
if(call is_even(x)){
dprint"[Cycle %d] Received even number %d : Should get answer 42 in 3 cycles" (*cycle_count, x) >>
cycle 2 >>
set ans := call answer_to_universe(x)
}
else{
dprint"[Cycle %d] Received odd number %d : Should get answer 0 in 4 cycles" (*cycle_count, x) >>
cycle 3 >>
set ans := call answer_to_universe(x)
} >>
send ep.res (*ans) >>
cycle 1
}
loop{
set cycle_count := *cycle_count + 8'd1
}
}
proc Top(){
chan ep_le -- ep_ri : foobar_ch;
spawn Foo(ep_le);
reg input : logic[8];
reg counter : logic[8];
loop {
send ep_ri.req (*input) >>
let data = recv ep_ri.res >>
set input:= *input + 8'd1 >>
dprint"[Cycle %d] The answer to the universe is %d" (*counter, data) >>
cycle 1
}
loop{
set counter := *counter + 8'd1
}
loop{
cycle 10 >>
dfinish
}
}
.. raw:: html
```
With this change, you will now encounter a different type error indicating that the value `data` in the `Top` process is being used after its lifetime has expired. This happens because the lifetime of the `res` message is specified as `@#1`, meaning that the received value is guaranteed to remain valid for only one cycle after the `send` of `res`.
As a result, any use of `data` must occur within one cycle of the `recv` expression. However, in the code above, the value of `data` is used only after the `set` expression. Since the `set` expression itself consumes one cycle to execute, the usage of `data` is effectively delayed beyond its permitted lifetime. Hence, the type system correctly rejects this program.
To fix this violation, we must ensure that `data` is consumed before any cycle-advancing operation occurs. Concretely, this can be achieved by moving the `set` expression after the `dprint` expression, thereby guaranteeing that `data` is used safely within its one-cycle lifetime.
```{eval-rst}
.. raw:: html