effekt-lang / effekt

A research language with effect handlers and lightweight effect polymorphism
https://effekt-lang.org
MIT License
302 stars 14 forks source link

Existentially quantified types are defined in outer scope #515

Open b-studios opened 1 week ago

b-studios commented 1 week ago
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.

unnir commented 4 days ago

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)
  }
}
b-studios commented 4 days ago

Sorry @unnir, not even close :)