Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading error "recursive functions need annotated return types" #430

Open
jiribenes opened this issue Mar 22, 2024 · 4 comments
Open
Labels
bug Something isn't working errormessage

Comments

@jiribenes
Copy link
Contributor

Here's the whole error: notice that the real problem is that the actual return type doesn't match the expected return type --- this has nothing to do with recursive / forward-declared functions!
Screenshot 2024-03-22 at 16 53 41

Interestingly, it goes away if we try just functions with numbers. 🤔

Repro:

def foo(): Unit = {
  val x = Nil[Int]()
  head(x)
}

Originally reported by @mlutze

@jiribenes
Copy link
Contributor Author

jiribenes commented Mar 22, 2024

Fascinatingly, the error doesn't go away if I annotate the return type as Int:

def foo(): Int = {
  val x = Nil[Int]()
  head(x)
}

So this is an actual bug, not just a bad error message -- is it perhaps a "unhandled effect" error? 🤔

@jiribenes jiribenes added the bug Something isn't working label Mar 22, 2024
@jiribenes jiribenes changed the title Misleading error "recursive functions need annotated return types" on return type mismatch Misleading error "recursive functions need annotated return types" Mar 22, 2024
@jiribenes
Copy link
Contributor Author

As @marvinborner noted, this issue is fixed by handling the Exception effect.
The following code doesn't report the error anymore:

def foo(): Int = {
  val x = Nil[Int]()
  try {
    head(x)
  } with Exception[EmptyList] {
    def raise(exception: EmptyList, msg: String) = {
      println(msg); 0
    }
  }
}

@jiribenes
Copy link
Contributor Author

The issue is also fixed by annotating the Exception effect:

def foo(): Int / Exception[EmptyList] = {
  //             ^^^^^^^^^^^^^^^^^^^^
  val x = Nil[Int]()
  head(x)
}

@b-studios
Copy link
Collaborator

Strangely it does report the correct error in the following case:

def myHead[A](l: List[A]) = head(l)

def foo(): Unit = {
  val x = Nil[Int]()
  myHead(x)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working errormessage
Projects
None yet
Development

No branches or pull requests

2 participants