effekt-lang / effekt

A research language with effect handlers and lightweight effect polymorphism

Home Page:https://effekt-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

References escape their region when using `var`

phischu opened this issue · 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.

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:

case tree @ source.Region(id, body) =>
val reg = tree.symbol
val tpe = Context.inferredTypeOf(body)
val free = freeCapture(tpe)
if (free contains reg.capture) {
Context.at(body) {
Context.error(pp"The return type ${tpe} of the statement is not allowed to refer to region ${reg}")
}
}
scoped { query(body) }