From 8cf16e374eae5913e1b3097fa5b2beda2279f44b Mon Sep 17 00:00:00 2001
From: Hamza Remmal <hamza@remmal.net>
Date: Fri, 14 Mar 2025 02:19:22 +0100
Subject: [PATCH] chore: effectively use tasty-inspector in scaladoc

This partially reverts #21929's fix but keeps the tests that were introduced
---
 .../tools/scaladoc/tasty/TastyParser.scala    |   4 +-
 .../src/scala/tasty/inspector/Inspector.scala |  33 ----
 .../src/scala/tasty/inspector/Tasty.scala     |  20 ---
 .../tasty/inspector/TastyInspector.scala      | 148 ------------------
 4 files changed, 2 insertions(+), 203 deletions(-)
 delete mode 100644 scaladoc/src/scala/tasty/inspector/Inspector.scala
 delete mode 100644 scaladoc/src/scala/tasty/inspector/Tasty.scala
 delete mode 100644 scaladoc/src/scala/tasty/inspector/TastyInspector.scala

diff --git a/scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala b/scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala
index 1a8337e0c6b7..f55451fdc636 100644
--- a/scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala
+++ b/scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala
@@ -5,7 +5,7 @@ package tasty
 import java.util.regex.Pattern
 
 import scala.util.{Try, Success, Failure}
-import scala.tasty.inspector.{ScaladocInternalTastyInspector, Inspector, Tasty}
+import scala.tasty.inspector.{TastyInspector, Inspector, Tasty}
 import scala.quoted._
 
 import dotty.tools.dotc
@@ -160,7 +160,7 @@ object ScaladocTastyInspector:
       report.error("File extension is not `tasty` or `jar`: " + invalidPath)
 
     if tastyPaths.nonEmpty then
-      ScaladocInternalTastyInspector.inspectAllTastyFilesInContext(tastyPaths, jarPaths, classpath)(inspector)(using ctx.compilerContext)
+      TastyInspector.inspectAllTastyFiles(tastyPaths, jarPaths, classpath)(inspector)
 
     val all = inspector.topLevels.result()
     all.groupBy(_._1).map { case (pckName, members) =>
diff --git a/scaladoc/src/scala/tasty/inspector/Inspector.scala b/scaladoc/src/scala/tasty/inspector/Inspector.scala
deleted file mode 100644
index 061c7dff0c44..000000000000
--- a/scaladoc/src/scala/tasty/inspector/Inspector.scala
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copy of tasty-inspector/src/scala/tasty/inspector/Inspector.scala
-// FIXME remove this copy of the file
-
-package scala.tasty.inspector
-
-import scala.quoted._
-import scala.quoted.runtime.impl.QuotesImpl
-
-import dotty.tools.dotc.Compiler
-import dotty.tools.dotc.Driver
-import dotty.tools.dotc.Run
-import dotty.tools.dotc.core.Contexts.Context
-import dotty.tools.dotc.core.Mode
-import dotty.tools.dotc.core.Phases.Phase
-import dotty.tools.dotc.fromtasty._
-import dotty.tools.dotc.util.ClasspathFromClassloader
-import dotty.tools.dotc.CompilationUnit
-import dotty.tools.unsupported
-import dotty.tools.dotc.report
-
-import java.io.File.pathSeparator
-
-trait Inspector:
-
-  /** Inspect all TASTy files using `Quotes` reflect API.
-   *
-   *  Note: Within this method `quotes.reflect.SourceFile.current` will not work, hence the explicit source paths.
-   *
-   *  @param tastys List of `Tasty` containing `.tasty`file path and AST
-   */
-  def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit
-
-end Inspector
diff --git a/scaladoc/src/scala/tasty/inspector/Tasty.scala b/scaladoc/src/scala/tasty/inspector/Tasty.scala
deleted file mode 100644
index b3e65bb5479e..000000000000
--- a/scaladoc/src/scala/tasty/inspector/Tasty.scala
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copy of tasty-inspector/src/scala/tasty/inspector/Tasty.scala
-// FIXME remove this copy of the file
-
-package scala.tasty.inspector
-
-import scala.quoted._
-
-/** `.tasty` file representation containing file path and the AST */
-trait Tasty[Q <: Quotes & Singleton]:
-
-  /** Instance of `Quotes` used to load the AST */
-  val quotes: Q
-
-  /** Path to the `.tasty` file */
-  def path: String
-
-  /** Abstract Syntax Tree contained in the `.tasty` file */
-  def ast: quotes.reflect.Tree
-
-end Tasty
diff --git a/scaladoc/src/scala/tasty/inspector/TastyInspector.scala b/scaladoc/src/scala/tasty/inspector/TastyInspector.scala
deleted file mode 100644
index 190be6a588a1..000000000000
--- a/scaladoc/src/scala/tasty/inspector/TastyInspector.scala
+++ /dev/null
@@ -1,148 +0,0 @@
-// Renamed copy of tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala
-// FIXME remove this copy of the file
-// Since copying, an inspectAllTastyFilesInContext method was added for scaladoc only
-// to fix regressions introduced by the switch from old to a new TastyInspector
-
-package scala.tasty.inspector
-
-import scala.quoted._
-import scala.quoted.runtime.impl.QuotesImpl
-
-import dotty.tools.dotc.Compiler
-import dotty.tools.dotc.Driver
-import dotty.tools.dotc.Run
-import dotty.tools.dotc.core.Contexts.Context
-import dotty.tools.dotc.core.Mode
-import dotty.tools.dotc.core.Phases.Phase
-import dotty.tools.dotc.fromtasty._
-import dotty.tools.dotc.quoted.QuotesCache
-import dotty.tools.dotc.util.ClasspathFromClassloader
-import dotty.tools.dotc.CompilationUnit
-import dotty.tools.unsupported
-import dotty.tools.dotc.report
-
-import java.io.File.pathSeparator
-
-object ScaladocInternalTastyInspector:
-
-  /** Load and process TASTy files using TASTy reflect
-   *
-   *  @param tastyFiles List of paths of `.tasty` files
-   *
-   *  @return boolean value indicating whether the process succeeded
-   */
-  def inspectTastyFiles(tastyFiles: List[String])(inspector: Inspector): Boolean =
-    inspectAllTastyFiles(tastyFiles, Nil, Nil)(inspector)
-
-  /** Load and process TASTy files in a `jar` file using TASTy reflect
-   *
-   *  @param jars Path of `.jar` file
-   *
-   *  @return boolean value indicating whether the process succeeded
-   */
-  def inspectTastyFilesInJar(jar: String)(inspector: Inspector): Boolean =
-    inspectAllTastyFiles(Nil, List(jar), Nil)(inspector)
-
-  private def checkFiles(tastyFiles: List[String], jars: List[String]): Unit =
-    def checkFile(fileName: String, ext: String): Unit =
-      val file = dotty.tools.io.Path(fileName)
-      if !file.ext.toLowerCase.equalsIgnoreCase(ext) then
-        throw new IllegalArgumentException(s"File extension is not `.$ext`: $file")
-      else if !file.exists then
-        throw new IllegalArgumentException(s"File not found: ${file.toAbsolute}")
-    tastyFiles.foreach(checkFile(_, "tasty"))
-    jars.foreach(checkFile(_, "jar"))
-
-  /**
-  * Added for Scaladoc-only.
-  * Meant to fix regressions introduces by the switch from old to new TastyInspector:
-  *  https://github.com/scala/scala3/issues/18231
-  *  https://github.com/scala/scala3/issues/20476
-  * Stable TastyInspector API does not support passing compiler context.
-  */
-  def inspectAllTastyFilesInContext(tastyFiles: List[String], jars: List[String], dependenciesClasspath: List[String])(inspector: Inspector)(using Context): Boolean =
-    checkFiles(tastyFiles, jars)
-    val classes = tastyFiles ::: jars
-    classes match
-      case Nil => true
-      case _ =>
-        val reporter = inspectorDriver(inspector).process(inspectorArgs(dependenciesClasspath, classes), summon[Context])
-        !reporter.hasErrors
-
-  /** Load and process TASTy files using TASTy reflect
-   *
-   *  @param tastyFiles List of paths of `.tasty` files
-   *  @param jars List of path of `.jar` files
-   *  @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files
-   *
-   *  @return boolean value indicating whether the process succeeded
-   */
-  def inspectAllTastyFiles(tastyFiles: List[String], jars: List[String], dependenciesClasspath: List[String])(inspector: Inspector): Boolean =
-    checkFiles(tastyFiles, jars)
-    val files = tastyFiles ::: jars
-    inspectFiles(dependenciesClasspath, files)(inspector)
-
-  private def inspectorDriver(inspector: Inspector) =
-    class InspectorDriver extends Driver:
-      override protected def newCompiler(implicit ctx: Context): Compiler = new TastyFromClass
-
-    class TastyInspectorPhase extends Phase:
-      override def phaseName: String = "tastyInspector"
-
-      override def runOn(units: List[CompilationUnit])(using ctx0: Context): List[CompilationUnit] =
-        // NOTE: although this is a phase, do not expect this to be ran with an xsbti.CompileProgress
-        val ctx = QuotesCache.init(ctx0.fresh)
-        runOnImpl(units)(using ctx)
-
-      private def runOnImpl(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
-        val quotesImpl = QuotesImpl()
-        class TastyImpl(val path: String, val ast: quotesImpl.reflect.Tree) extends Tasty[quotesImpl.type] {
-          val quotes = quotesImpl
-        }
-        val tastys = units.map(unit => new TastyImpl(unit.source.path , unit.tpdTree.asInstanceOf[quotesImpl.reflect.Tree]))
-        inspector.inspect(using quotesImpl)(tastys)
-        units
-
-      override def run(implicit ctx: Context): Unit = unsupported("run")
-    end TastyInspectorPhase
-
-    class TastyFromClass extends TASTYCompiler:
-
-      override protected def frontendPhases: List[List[Phase]] =
-        List(new ReadTasty) :: // Load classes from tasty
-        Nil
-
-      override protected def picklerPhases: List[List[Phase]] = Nil
-
-      override protected def transformPhases: List[List[Phase]] = Nil
-
-      override protected def backendPhases: List[List[Phase]] =
-        List(new TastyInspectorPhase) ::  // Perform a callback for each compilation unit
-        Nil
-
-      override def newRun(implicit ctx: Context): Run =
-        reset()
-        val ctx2 = ctx.fresh
-            .addMode(Mode.ReadPositions)
-            .setSetting(ctx.settings.XreadComments, true)
-        new TASTYRun(this, ctx2)
-
-    new InspectorDriver
-
-  private def inspectorArgs(classpath: List[String], classes: List[String]): Array[String] =
-    val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)
-    val fullClasspath = (classpath :+ currentClasspath).mkString(pathSeparator)
-    ("-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes).toArray
-
-
-  private def inspectFiles(classpath: List[String], classes: List[String])(inspector: Inspector): Boolean =
-    classes match
-      case Nil => true
-      case _ =>
-        val reporter = inspectorDriver(inspector).process(inspectorArgs(classpath, classes))
-        !reporter.hasErrors
-
-  end inspectFiles
-
-
-end ScaladocInternalTastyInspector