Skip to content

Commit

Permalink
Make accessing OriginalTree noop on Scala 2.12.0-2
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored and eed3si9n committed Sep 19, 2021
1 parent 34c2fd1 commit 922885f
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions internal/compiler-bridge/src/main/scala-2.12/xsbt/Compat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ abstract class Compat {

/** If given tree contains object tree attachment calls func on tree from attachment. */
protected def processOriginalTreeAttachment(in: Tree)(func: Tree => Unit): Unit = {
import analyzer._
in.attachments.get[OriginalTreeAttachment].foreach { a =>
func(a.original)
}
Compat.OriginalTreeTraverser.Instance.traverseOriginal(in)(func)
}
}
object Compat {
Expand All @@ -34,6 +31,32 @@ object Compat {

// IMain in 2.13 accepts ReplReporter
def replReporter(settings: Settings, writer: PrintWriter) = writer

sealed abstract class OriginalTreeTraverser private {
def traverseOriginal[T <: Global#Tree](t: T)(f: T => Unit): Unit
}

object OriginalTreeTraverser {
private[this] val cls = try {
Class.forName("scala.tools.nsc.typechecker.StdAttachments$OriginalTreeAttachment")
} catch { case _: Throwable => null }

private object Reflective extends OriginalTreeTraverser {
private[this] val ct = scala.reflect.ClassTag(cls)
private[this] val meth = cls.getMethod("original")
def traverseOriginal[T <: Global#Tree](t: T)(f: T => Unit): Unit =
t.attachments.get(ct) match {
case Some(attachment) => f(meth.invoke(attachment).asInstanceOf[T])
case None =>
}
}

private object NoOp extends OriginalTreeTraverser {
def traverseOriginal[T <: Global#Tree](t: T)(f: T => Unit): Unit = ()
}

val Instance = if (cls == null) NoOp else Reflective
}
}

/** Defines compatibility utils for [[ZincCompiler]]. */
Expand Down

0 comments on commit 922885f

Please sign in to comment.