Search code examples
scalascala-macrosscala-macro-paradise

Workaround for Scala macro annotation bug


I have a macro annotation that I use to inject implicit type class to a companion method.

@MyMacro case class MyClass[T](a: String, b: Int, t: T)

Most of the time it work as expected, but it breaks when I use type constraint notation:

@MyMacro case class MyClass[T: TypeClass](a: String, b: Int, t: T)
// private[this] not allowed for case class parameters

This error was described on SO and reported as a bug.

Thing is: macros (v1) are no longer maintained, so I cannot expect that this will be fixed.

So what I wanted to know is: can I fix this myself within a macro? Is this change done to AST in a way that I could somehow undo it? I would like to try repairing it within a macro instead of forcing all users to rewrite their code to ...(implicit tc: TypeClass[T]).


Solution

  • class AnnotationType() extends scala.annotation.StaticAnnotation {
      def macroTransform(annottees: Any*): Any = macro AnnotationTypeImpl.impl
    }
    
    class AnnotationTypeImpl(val c: blackbox.Context) {
    
      import c.universe._
    
      def impl(annottees: Tree*): Tree = {
    
        val tree = annottees.head.asInstanceOf[ClassDef]
    
        val newTree = tree match {
          case ClassDef(mods, name, tparams, impl@Template(parents, self, body)) =>
            val newBody = body.map {
              case ValDef(mods, name, tpt, rhs) =>
                // look here
                // the flag of `private[this]` is Flag.PRIVATE | Flag.LOCAL
                // the flag of `private` is Flag.PRIVATE
                // drop Flag.LOCAL in Modifiers.flags ,  it will change `private[this]` to `private`
                val newMods =
                  if(mods.hasFlag(Flag.IMPLICIT))
                    mods.asInstanceOf[scala.reflect.internal.Trees#Modifiers].&~(Flag.LOCAL.asInstanceOf[Long]).&~(Flag.CASEACCESSOR.asInstanceOf[Long]).asInstanceOf[Modifiers]
                  else
                    mods
                ValDef(newMods, name, tpt, rhs)
    
              case e => e
            }
            ClassDef(mods, name, tparams, Template(parents, self, newBody))
        }
        println(show(tree))
        println(show(newTree))
    
        q"..${newTree +: annottees.tail}"
      }
    }
    

    // test

    @AnnotationType()
    case class AnnotationTypeTest[T: Option](a: T){
      def option = implicitly[Option[T]]
    }
    
    object AnnotationTypeTest {
      def main(args: Array[String]): Unit = {
        implicit val x = Option(1)
    
        println(AnnotationTypeTest(100))
        println(AnnotationTypeTest(100).option)
        println(AnnotationTypeTest(100).copy(a =2222))
        println(AnnotationTypeTest(100).copy(a =2222)(Some(999)).option)
      }
    
    }