Skip to content

Commit 4027968

Browse files
authored
Backport "Revert #17623" to LTS (#18963)
Backports #17744 to the LTS branch. PR submitted by the release tooling.
2 parents 08efe53 + f9f6fa8 commit 4027968

File tree

7 files changed

+97
-131
lines changed

7 files changed

+97
-131
lines changed

scaladoc/src/dotty/tools/scaladoc/ScalaModuleProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ case class Module(rootPackage: Member, members: Map[DRI, Member])
88

99
object ScalaModuleProvider:
1010
def mkModule()(using ctx: DocContext): Module =
11-
val (result, rootDoc) = ScaladocTastyInspector.loadDocs()
11+
val (result, rootDoc) = ScaladocTastyInspector().result()
1212
val (rootPck, rest) = result.partition(_.name == "API")
1313
val (emptyPackages, nonemptyPackages) = (rest ++ rootPck.flatMap(_.members))
1414
.filter(p => p.members.nonEmpty || p.docs.nonEmpty).sortBy(_.name)

scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package tasty
55
import java.util.regex.Pattern
66

77
import scala.util.{Try, Success, Failure}
8-
import scala.tasty.inspector.{TastyInspector, Inspector, Tasty}
8+
import scala.tasty.inspector.DocTastyInspector
99
import scala.quoted._
1010

1111
import dotty.tools.dotc
@@ -24,12 +24,24 @@ import ScaladocSupport._
2424
*
2525
* Delegates most of the work to [[TastyParser]] [[dotty.tools.scaladoc.tasty.TastyParser]].
2626
*/
27-
case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
27+
case class ScaladocTastyInspector()(using ctx: DocContext) extends DocTastyInspector:
2828

2929
private val topLevels = Seq.newBuilder[(String, Member)]
3030
private var rootDoc: Option[Comment] = None
3131

32-
def inspect(using Quotes)(tastys: List[scala.tasty.inspector.Tasty[quotes.type]]): Unit =
32+
def processCompilationUnit(using Quotes)(root: reflect.Tree): Unit = ()
33+
34+
override def postProcess(using Quotes): Unit =
35+
// hack into the compiler to get a list of all top-level trees
36+
// in principle, to do this, one would collect trees in processCompilationUnit
37+
// however, path-dependent types disallow doing so w/o using casts
38+
inline def hackForeachTree(thunk: reflect.Tree => Unit): Unit =
39+
given dctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
40+
dctx.run.nn.units.foreach { compilationUnit =>
41+
// mirrors code from TastyInspector
42+
thunk(compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
43+
}
44+
3345
val symbolsToSkip: Set[reflect.Symbol] =
3446
ctx.args.identifiersToSkip.flatMap { ref =>
3547
val qrSymbol = reflect.Symbol
@@ -104,8 +116,7 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
104116
rootDoc = Some(parseCommentString(using parser.qctx, summon[DocContext])(content, topLevelPck, None))
105117
}
106118

107-
for tasty <- tastys do {
108-
val root = tasty.ast
119+
hackForeachTree { root =>
109120
if !isSkipped(root.symbol) then
110121
val treeRoot = root.asInstanceOf[parser.qctx.reflect.Tree]
111122
processRootDocIfNeeded(treeRoot)
@@ -127,47 +138,33 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
127138
topLevels += "scala" -> Member(scalaPckg.fullName, "", scalaPckg.dri, Kind.Package)
128139
topLevels += mergeAnyRefAliasAndObject(parser)
129140

130-
131-
132-
def mergeAnyRefAliasAndObject(parser: TastyParser) =
133-
import parser.qctx.reflect._
134-
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
135-
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
136-
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
137-
"scala" -> aM.copy(
138-
kind = Kind.Class(Nil, Nil),
139-
members = objectMembers
140-
)
141-
142-
object ScaladocTastyInspector:
143-
144-
def loadDocs()(using ctx: DocContext): (List[Member], Option[Comment]) =
141+
def result(): (List[Member], Option[Comment]) =
142+
topLevels.clear()
143+
rootDoc = None
145144
val filePaths = ctx.args.tastyFiles.map(_.getAbsolutePath).toList
146145
val classpath = ctx.args.classpath.split(java.io.File.pathSeparator).toList
147146

148-
val inspector = new ScaladocTastyInspector
149-
150-
val (tastyPaths, nonTastyPaths) = filePaths.partition(_.endsWith(".tasty"))
151-
val (jarPaths, invalidPaths) = nonTastyPaths.partition(_.endsWith(".jar"))
152-
153-
for invalidPath <- invalidPaths do
154-
report.error("File extension is not `tasty` or `jar`: " + invalidPath)
155-
156-
if tastyPaths.nonEmpty then
157-
TastyInspector.inspectAllTastyFiles(tastyPaths, jarPaths, classpath)(inspector)
147+
if filePaths.nonEmpty then inspectFilesInContext(classpath, filePaths)
158148

159-
val all = inspector.topLevels.result()
149+
val all = topLevels.result()
160150
all.groupBy(_._1).map { case (pckName, members) =>
161151
val (pcks, rest) = members.map(_._2).partition(_.kind == Kind.Package)
162152
val basePck = pcks.reduce( (p1, p2) =>
163153
val withNewMembers = p1.withNewMembers(p2.members)
164154
if withNewMembers.docs.isEmpty then withNewMembers.withDocs(p2.docs) else withNewMembers
165155
)
166156
basePck.withMembers((basePck.members ++ rest).sortBy(_.name))
167-
}.toList -> inspector.rootDoc
168-
169-
end ScaladocTastyInspector
157+
}.toList -> rootDoc
170158

159+
def mergeAnyRefAliasAndObject(parser: TastyParser) =
160+
import parser.qctx.reflect._
161+
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
162+
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
163+
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
164+
"scala" -> aM.copy(
165+
kind = Kind.Class(Nil, Nil),
166+
members = objectMembers
167+
)
171168
/** Parses a single Tasty compilation unit. */
172169
case class TastyParser(
173170
qctx: Quotes,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala.tasty.inspector
2+
3+
import dotty.tools.dotc.core.Contexts.Context
4+
5+
abstract class DocTastyInspector extends OldTastyInspector:
6+
def inspectFilesInDocContext(
7+
classpath: List[String],
8+
filePaths: List[String])(
9+
using Context): Unit = inspectFilesInContext(classpath, filePaths)

scaladoc/src/scala/tasty/inspector/Inspector.scala

Lines changed: 0 additions & 33 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Copy of tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala
2-
// FIXME remove this copy of the file
3-
41
package scala.tasty.inspector
52

63
import scala.quoted._
@@ -13,43 +10,45 @@ import dotty.tools.dotc.core.Contexts.Context
1310
import dotty.tools.dotc.core.Mode
1411
import dotty.tools.dotc.core.Phases.Phase
1512
import dotty.tools.dotc.fromtasty._
16-
import dotty.tools.dotc.quoted.QuotesCache
1713
import dotty.tools.dotc.util.ClasspathFromClassloader
1814
import dotty.tools.dotc.CompilationUnit
1915
import dotty.tools.unsupported
2016
import dotty.tools.dotc.report
2117

2218
import java.io.File.pathSeparator
2319

24-
object TastyInspector:
20+
// COPY OF OLD IMPLEMENTATION
21+
// TODO: update to new implementation
22+
trait OldTastyInspector:
23+
self =>
24+
25+
/** Process a TASTy file using TASTy reflect */
26+
protected def processCompilationUnit(using Quotes)(root: quotes.reflect.Tree): Unit
27+
28+
/** Called after all compilation units are processed */
29+
protected def postProcess(using Quotes): Unit = ()
2530

2631
/** Load and process TASTy files using TASTy reflect
2732
*
2833
* @param tastyFiles List of paths of `.tasty` files
29-
*
30-
* @return boolean value indicating whether the process succeeded
3134
*/
32-
def inspectTastyFiles(tastyFiles: List[String])(inspector: Inspector): Boolean =
33-
inspectAllTastyFiles(tastyFiles, Nil, Nil)(inspector)
35+
def inspectTastyFiles(tastyFiles: List[String]): Boolean =
36+
inspectAllTastyFiles(tastyFiles, Nil, Nil)
3437

3538
/** Load and process TASTy files in a `jar` file using TASTy reflect
3639
*
3740
* @param jars Path of `.jar` file
38-
*
39-
* @return boolean value indicating whether the process succeeded
4041
*/
41-
def inspectTastyFilesInJar(jar: String)(inspector: Inspector): Boolean =
42-
inspectAllTastyFiles(Nil, List(jar), Nil)(inspector)
42+
def inspectTastyFilesInJar(jar: String): Boolean =
43+
inspectAllTastyFiles(Nil, List(jar), Nil)
4344

4445
/** Load and process TASTy files using TASTy reflect
4546
*
4647
* @param tastyFiles List of paths of `.tasty` files
4748
* @param jars List of path of `.jar` files
4849
* @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files
49-
*
50-
* @return boolean value indicating whether the process succeeded
5150
*/
52-
def inspectAllTastyFiles(tastyFiles: List[String], jars: List[String], dependenciesClasspath: List[String])(inspector: Inspector): Boolean =
51+
def inspectAllTastyFiles(tastyFiles: List[String], jars: List[String], dependenciesClasspath: List[String]): Boolean =
5352
def checkFile(fileName: String, ext: String): Unit =
5453
val file = dotty.tools.io.Path(fileName)
5554
if file.extension != ext then
@@ -59,30 +58,40 @@ object TastyInspector:
5958
tastyFiles.foreach(checkFile(_, "tasty"))
6059
jars.foreach(checkFile(_, "jar"))
6160
val files = tastyFiles ::: jars
62-
inspectFiles(dependenciesClasspath, files)(inspector)
61+
files.nonEmpty && inspectFiles(dependenciesClasspath, files)
62+
63+
/** Load and process TASTy files using TASTy reflect and provided context
64+
*
65+
* Used in doctool to reuse reporter and setup provided by sbt
66+
*
67+
* @param classes List of paths of `.tasty` and `.jar` files (no validation is performed)
68+
* @param classpath Classpath with extra dependencies needed to load class in the `.tasty` files
69+
*/
70+
protected[inspector] def inspectFilesInContext(classpath: List[String], classes: List[String])(using Context): Unit =
71+
if (classes.isEmpty) report.error("Parameter classes should no be empty")
72+
inspectorDriver().process(inspectorArgs(classpath, classes), summon[Context])
73+
6374

64-
private def inspectorDriver(inspector: Inspector) =
75+
private def inspectorDriver() =
6576
class InspectorDriver extends Driver:
6677
override protected def newCompiler(implicit ctx: Context): Compiler = new TastyFromClass
6778

6879
class TastyInspectorPhase extends Phase:
6980
override def phaseName: String = "tastyInspector"
7081

71-
override def runOn(units: List[CompilationUnit])(using ctx0: Context): List[CompilationUnit] =
72-
val ctx = QuotesCache.init(ctx0.fresh)
73-
runOnImpl(units)(using ctx)
74-
75-
private def runOnImpl(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
76-
val quotesImpl = QuotesImpl()
77-
class TastyImpl(val path: String, val ast: quotesImpl.reflect.Tree) extends Tasty[quotesImpl.type] {
78-
val quotes = quotesImpl
79-
}
80-
val tastys = units.map(unit => new TastyImpl(unit.source.path , unit.tpdTree.asInstanceOf[quotesImpl.reflect.Tree]))
81-
inspector.inspect(using quotesImpl)(tastys)
82+
override def run(implicit ctx: Context): Unit =
83+
val qctx = QuotesImpl()
84+
self.processCompilationUnit(using qctx)(ctx.compilationUnit.tpdTree.asInstanceOf[qctx.reflect.Tree])
85+
86+
class TastyInspectorFinishPhase extends Phase:
87+
override def phaseName: String = "tastyInspectorFinish"
88+
89+
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
90+
val qctx = QuotesImpl()
91+
self.postProcess(using qctx)
8292
units
8393

8494
override def run(implicit ctx: Context): Unit = unsupported("run")
85-
end TastyInspectorPhase
8695

8796
class TastyFromClass extends TASTYCompiler:
8897

@@ -96,6 +105,7 @@ object TastyInspector:
96105

97106
override protected def backendPhases: List[List[Phase]] =
98107
List(new TastyInspectorPhase) :: // Perform a callback for each compilation unit
108+
List(new TastyInspectorFinishPhase) :: // Perform a final callback
99109
Nil
100110

101111
override def newRun(implicit ctx: Context): Run =
@@ -113,14 +123,14 @@ object TastyInspector:
113123
("-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes).toArray
114124

115125

116-
private def inspectFiles(classpath: List[String], classes: List[String])(inspector: Inspector): Boolean =
117-
classes match
118-
case Nil => true
119-
case _ =>
120-
val reporter = inspectorDriver(inspector).process(inspectorArgs(classpath, classes))
121-
!reporter.hasErrors
126+
private def inspectFiles(classpath: List[String], classes: List[String]): Boolean =
127+
if (classes.isEmpty)
128+
throw new IllegalArgumentException("Parameter classes should no be empty")
129+
130+
val reporter = inspectorDriver().process(inspectorArgs(classpath, classes))
131+
reporter.hasErrors
122132

123133
end inspectFiles
124134

125135

126-
end TastyInspector
136+
end OldTastyInspector

scaladoc/src/scala/tasty/inspector/Tasty.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

scaladoc/test/dotty/tools/scaladoc/tasty/comments/MemberLookupTests.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.scaladoc
22
package tasty.comments
33

4-
import scala.quoted.*
4+
import scala.quoted.Quotes
55

66
import org.junit.{Test, Rule}
77
import org.junit.Assert.{assertSame, assertTrue}
@@ -198,11 +198,14 @@ class MemberLookupTests {
198198

199199
@Test
200200
def test(): Unit = {
201-
import scala.tasty.inspector.*
202-
class MyInspector extends Inspector:
201+
import scala.tasty.inspector.OldTastyInspector
202+
class Inspector extends OldTastyInspector:
203+
var alreadyRan: Boolean = false
203204

204-
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
205-
this.test()
205+
override def processCompilationUnit(using ctx: quoted.Quotes)(root: ctx.reflect.Tree): Unit =
206+
if !alreadyRan then
207+
this.test()
208+
alreadyRan = true
206209

207210
def test()(using q: Quotes): Unit = {
208211
import dotty.tools.scaladoc.tasty.comments.MemberLookup
@@ -212,6 +215,6 @@ class MemberLookupTests {
212215
cases.testAll()
213216
}
214217

215-
TastyInspector.inspectTastyFiles(TestUtils.listOurClasses())(new MyInspector)
218+
Inspector().inspectTastyFiles(TestUtils.listOurClasses())
216219
}
217220
}

0 commit comments

Comments
 (0)