effekt icon indicating copy to clipboard operation
effekt copied to clipboard

References escape their region when using `var`

Open phischu opened this issue 1 year ago • 1 comments

Consider the following program where we allocate a mutable variable x in a region r1, and return a closure that contains it.

interface Ref[T] {
  def get(): T
}

def main() = {
  var myref =
    region r1 {
      var x in r1 = "hoho";
      new Ref[String] {
        def get() = x
      }
    }
  println(myref.get())
}

This program is accepted but it should not. When changing var myref to val myref it is rejected, which is good.

phischu avatar Jun 26 '24 10:06 phischu

One bit of additional information: the inferred type of the returned ref is Ref[String] at {r1}, which is correct. So my conjecture is that simply the wellformedness check is buggy:

https://github.com/effekt-lang/effekt/blob/9e651118559e5980e7182a10e47abdbd9bf1b973/effekt/shared/src/main/scala/effekt/typer/Wellformedness.scala#L97-L109

b-studios avatar Jun 26 '24 10:06 b-studios

Potentially fixed by #552

b-studios avatar Aug 22 '24 17:08 b-studios