scala-ide / scala-worksheet

A Scala IDE plugin for a multi-line REPL (called worksheet)
96 stars 24 forks source link

@tailrec not correctly supported in main object #122

Open skyluc opened 11 years ago

skyluc commented 11 years ago

An error is reported the level of the @tailrec, when using it at a top level def, in the main object of a worksheet:

import scala.annotation.tailrec
object worksheet {
  @tailrec
  def f(i: Int): Int =
    if (i % 2 == 0)
      i
    else
      f(i -1)
}

The reason is that the annotation is not correctly managed by the instrumentation. The code generated is the following:

 @tailrec;$skip(87);
  def f(i: Int): Int =
    if (i % 2 == 0)
...

And it looks like there will be another problem when this is fixed, the worksheet doesn't allow the use of private or final on a top level def, which is required to be able to use @tailrec.

The workaround is to put the @tailrec inside a def, or in a different object.

skyluc commented 11 years ago

Originally reported against Scala IDE: #1001636.

skyluc commented 11 years ago

My comment on a possible additionnal problem is wrong. private or final modifier cannot be used at this position, but it doesn't block using @tailrec if the code is correctly instrumented.