Scala Programming Language
  1. Scala Programming Language
  2. SI-3275

Give a more specific error when @scala.annotation.tailrec is used on a method that doesn't recurse

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      tail recursion compile error

      Description

      Compiling with 2.8.0.Beta1, if I use @scala.annotation.tailrec on a function that doesn't recurse, the compiler simply tells me that it cannot optimise it. It would be very useful if the compiler were able to tell me that this is because the function doesn't recurse.

      The use case may sound silly - why annotate the function if it's not recursive? - but it has happened to me a few times where I have refactored a recursive function to no longer recurse, and then scratched my head for a while after getting the compile error.

        Activity

        Hide
        Paul Phillips added a comment -

        I now have fancy detailed @tailrec messages implemented, but I'm putting the patch on ice until after this upcoming release-thing is rolled.

        Show
        Paul Phillips added a comment - I now have fancy detailed @tailrec messages implemented, but I'm putting the patch on ice until after this upcoming release-thing is rolled.
        Hide
        Paul Phillips added a comment -

        (In r21599) Improved @tailrec error messages to specify the reason.
        In the process fixed old bug involving tail call transformation.
        Closes SI-3275, SI-2018. Review by dragos.

        Show
        Paul Phillips added a comment - (In r21599) Improved @tailrec error messages to specify the reason. In the process fixed old bug involving tail call transformation. Closes SI-3275 , SI-2018 . Review by dragos.
        Hide
        Dan Rosen added a comment -

        Reopening: when @tailrec is used on a non-recursive method, the default error message is still used: "it contains a recursive call not in tail position".

        From TailCalls.scala:

        else if (!isRecursiveCall) {
          if (receiverIsSuper) failHere("it contains a recursive call targetting a supertype")
          else failHere(defaultReason)
        }
        

        I haven't done any scalac hacking, but I assume this is the spot where you'd want to say "method is non-recursive."

        Show
        Dan Rosen added a comment - Reopening: when @tailrec is used on a non-recursive method, the default error message is still used: "it contains a recursive call not in tail position". From TailCalls.scala : else if (!isRecursiveCall) { if (receiverIsSuper) failHere("it contains a recursive call targetting a supertype") else failHere(defaultReason) } I haven't done any scalac hacking, but I assume this is the spot where you'd want to say "method is non-recursive."
        Hide
        Paul Phillips added a comment -

        79e937bea2

        Show
        Paul Phillips added a comment - 79e937bea2

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Graham Lea
            TracCC:
            Paul Phillips
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development