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

Existentially quantified types are defined in outer scope

b-studios opened this issue · comments

type Foo {
  Bar[T]()
}

type Baz {
  Bam[T]()
}

should type check, but reports:

Type T already defined in the current scope

The scope of T is probably not delimited correctly in Namer and hence it introduces a file-global type T.

Here is gpt4o:

In the build.sbt, ensure that type parameters are handled correctly.

class Namer {
  def defineTypeParameters(params: List[TypeParameter], scope: Scope): Unit = {
    params.foreach { param =>
      scope.define(param)
    }
  }

  def enterScope[T](scope: Scope)(block: => T): T = {
    val previousScope = currentScope
    currentScope = scope
    try {
      block
    } finally {
      currentScope = previousScope
    }
  }
}

Create tests to ensure type parameters are scoped correctly.

test("Type parameters are scoped locally") {
  val fooScope = new Scope()
  val bazScope = new Scope()

  enterScope(fooScope) {
    defineTypeParameters(List("T"), fooScope)
    assert(fooScope.lookup("T").isDefined)
  }

  enterScope(bazScope) {
    defineTypeParameters(List("T"), bazScope)
    assert(bazScope.lookup("T").isDefined)
  }
}

Sorry @unnir, not even close :)