effekt-lang / effekt

A language with lexical effect handlers and lightweight effect polymorphism
https://effekt-lang.org
MIT License
328 stars 23 forks source link

Nicer error messages on a likely forgotten resume #680

Open jiribenes opened 4 hours ago

jiribenes commented 4 hours ago

Students (and many other users like me) often forget to resume from a handler. We could customise the error message, especially when it's obvious from the type that using resume would fix the type error one gets.

Let's take a look at a small example:

effect get(): Int

def foo() = 
  try { println(do get()) }
  with get {
    42
//  ^^
//  Expected Unit but got Int.
  }

We could customise ( 🚲🏠 ) the error to something slightly more specific like:

Expected Unit but got Int.
Did you mean to use `resume: Int => Unit` in the handler in order to resume?

The heuristic is pretty straightforward: an additional hint should fire if in the handler we get "expected τ1 but got τ2" and resume has type τ2 => τ1.

jiribenes commented 4 hours ago

If I'm looking at it correctly, the error message is produced by the body checkAgainst ret here (and we could use the resumeType): https://github.com/effekt-lang/effekt/blob/000a76885d1a1d0004a41422ca35a87362582503/effekt/shared/src/main/scala/effekt/Typer.scala#L470-L482