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

Provide way to annotate method as compile-time-only

    Details

      Description

      One pattern used with macros is to have an unimplemented stub method that is valid only in the context of a macro and is then removed by that macro:

      trait Task[T] {
        def value: T = ???
      }
      
      val foo: Task[Int] = ...
      
      val t: Task[Int] = task {
        foo.value + 1
      }
      

      The problem is that use of `value` outside of the macro is not caught at compile time, but at runtime. For example, the following throws an exception at runtime because value is not used within the task macro that would otherwise remove the call to it.

      val foo: Task[Int] = ...
      val i: Int = foo.value
      

      The proposed solution is to have an annotation or some other way of telling the compiler that calls to `value` that survive typechecking are an error. For example,

         @compileTimeOnly("Calls to `value` must be made inside the the task macro.")
         def value: T = ???
      

      This would be useful for standard macro-related methods like splice as well.

        Activity

        Hide
        Eugene Burmako added a comment -

        I have an idea, which I would like to try. It involves removing @compileTimeOnly altogether, but first I need to experiment a bit (after I fix reflection sync).

        Show
        Eugene Burmako added a comment - I have an idea, which I would like to try. It involves removing @compileTimeOnly altogether, but first I need to experiment a bit (after I fix reflection sync).
        Hide
        Jason Zaugg added a comment -

        We could also make this work by making the compiler tolerant of the absence of this annotation. SBT could then need to provide the annotation, in it's source code. But I also look forward to your idea..

        Show
        Jason Zaugg added a comment - We could also make this work by making the compiler tolerant of the absence of this annotation. SBT could then need to provide the annotation, in it's source code. But I also look forward to your idea..
        Hide
        Eugene Burmako added a comment -

        As we discussed on the core meeting, Mark is okay with having the annotation in scala-compiler.jar. Therefore I think we should go for this option for now.

        Show
        Eugene Burmako added a comment - As we discussed on the core meeting, Mark is okay with having the annotation in scala-compiler.jar. Therefore I think we should go for this option for now.
        Show
        Eugene Burmako added a comment - https://github.com/scala/scala/pull/2019
        Show
        Eugene Burmako added a comment - https://github.com/scala/scala/commit/29892586382ef846b0ad46271c2fba9970943faf

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Mark Harrah
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development