dafny icon indicating copy to clipboard operation
dafny copied to clipboard

Bad encoding for `this` in tail recursive functions when compiling to Java

Open fabiomadge opened this issue 1 year ago • 1 comments

Dafny version

master

Code to produce this issue

class C {
  var data: nat
  constructor() {}
  function Loop(xs: seq<()>): () reads this {
    if |xs| == 0
    then ()
    else
      var _ := forall c :: c in { this } ==> c.data == this.data;
      var _ := set n: nat {:trigger n * 1} | n < 100 && n == this.data;
      var _ := map n: nat {:trigger n * 1} | n < 100 :: this;
      Loop(xs[1..])
  }
}

method Main(){
  var c := new C();
  print c.Loop([(), ()]), "\n";
}

Command to run and resulting output

No response

What happened?

Java lambdas don't support the way we use them.

What type of operating system are you experiencing the problem on?

Mac

fabiomadge avatar Jun 05 '24 19:06 fabiomadge

Can confirm that running above code with java as target throws an error (error: local variables referenced from a lambda expression must be final or effectively final).

stefan-aws avatar Jun 12 '24 16:06 stefan-aws