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

Possible regression in 2.9: @tailrec annotated method rejected by scalac

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: Scala 2.9.0
    • Fix Version/s: Backlog
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      Scala 2.9.0.1 via Maven, IntelliJ, Mac OS X 10.6

      Description

      The following compiled with Scala 2.8.1:

      @annotation.tailrec
      def lazyFilter[E](s: Stream[E], p: E => Boolean): Stream[E] = s match {
        case h #:: t => if (p(h)) h #:: lazyFilter(t, p) else lazyFilter(t, p)
      }
      

      In 2.9.0 and 2.9.0.1, I get:

        error: could not optimize @tailrec annotated method lazyFilter: it contains a recursive call not in tail position
      

      To my understanding, the method is tail recursive, as the first occurrence of "lazyFilter" is not a call, but a closure that is passed to cons.

      I'm not sure if this only a bug in the evaluation of @tailrec, or if the 2.9 compiler really cannot apply tailrec optimization here anymore. In the latter case, it would affect the Stream class as well.

      Could you please clarify about this.

      Thanks.

        Activity

        Hide
        Paul Phillips added a comment -

        You are correct. It only worked in 2.8.1 by accident, as this case was not considered at all. Tightening the general case scooped this up as well, but the closure should be excluded. You can see looking at the bytecode that it compiles to a jump in both versions.

        Show
        Paul Phillips added a comment - You are correct. It only worked in 2.8.1 by accident, as this case was not considered at all. Tightening the general case scooped this up as well, but the closure should be excluded. You can see looking at the bytecode that it compiles to a jump in both versions.
        Hide
        Adriaan Moors added a comment - - edited

        Vlad, since you've spent some quality time in tail calls recently, could you please take care of suppressing the error. At least, if I understood correctly, the only actual call to lazyFilter is in tail position. The other occurrence is not a call.

        Show
        Adriaan Moors added a comment - - edited Vlad, since you've spent some quality time in tail calls recently, could you please take care of suppressing the error. At least, if I understood correctly, the only actual call to lazyFilter is in tail position. The other occurrence is not a call.
        Hide
        Vlad Ureche added a comment -

        If by "quality time" you mean scratching my head and trying to figure out why the typer doesn't work as I expect, then yeah, I did plenty of that )

        Anyway, bug accepted

        Show
        Vlad Ureche added a comment - If by "quality time" you mean scratching my head and trying to figure out why the typer doesn't work as I expect, then yeah, I did plenty of that ) Anyway, bug accepted

          People

          • Assignee:
            Vlad Ureche
            Reporter:
            Christoph Radig
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:

              Development