Skip to content

Commit 7415f66

Browse files
Merge pull request #8461 from gzoller/fix8460
Fix #8460: expose children of sealed trait in Reflection
2 parents 65ada3c + 51dd004 commit 7415f66

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17531753
case sym if sym.is(Flags.CaseAccessor) => sym.asTerm
17541754
}
17551755

1756+
def Symbol_children(self: Symbol)(using ctx: Context): List[Symbol] =
1757+
dotty.tools.dotc.transform.SymUtils(self).children
1758+
17561759
private def isField(sym: Symbol)(using ctx: Context): Boolean = sym.isTerm && !sym.is(Flags.Method)
17571760

17581761
def Symbol_of(fullName: String)(using ctx: Context): Symbol =

library/src/scala/tasty/Reflection.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,10 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
22542254
/** Shows the tree as fully typed source code */
22552255
def showWith(syntaxHighlight: SyntaxHighlight)(using ctx: Context): String =
22562256
new SourceCodePrinter[self.type](self)(syntaxHighlight).showSymbol(sym)
2257+
2258+
/** Case class or case object children of a sealed trait */
2259+
def children(using ctx: Context): List[Symbol] =
2260+
internal.Symbol_children(sym)
22572261
}
22582262

22592263

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,9 @@ trait CompilerInterface {
13501350

13511351
def Symbol_noSymbol(using ctx: Context): Symbol
13521352

1353+
/** Case class or case object children of a sealed trait */
1354+
def Symbol_children(self: Symbol)(using ctx: Context): List[Symbol]
1355+
13531356

13541357
///////////
13551358
// FLAGS //
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import scala.tasty.Reflection
2+
import scala.tasty.inspector._
3+
4+
// Ambiguous member names
5+
sealed trait Vehicle
6+
case class Truck(numberOfWheels: Int) extends Vehicle
7+
case class Car(numberOfWheels: Int, color: String) extends Vehicle
8+
case class Plane(numberOfEngines: Int) extends Vehicle
9+
10+
// Case object implementation
11+
sealed trait Flavor
12+
case object Vanilla extends Flavor
13+
case object Chocolate extends Flavor
14+
case object Bourbon extends Flavor
15+
16+
object Test {
17+
def main(args: Array[String]): Unit = {
18+
19+
// Tasty Scala Class
20+
val inspect1 = new TestInspector_Children()
21+
inspect1.inspect("", List("Vehicle"))
22+
assert(inspect1.kids == List("Truck","Car","Plane"))
23+
24+
// Java Class
25+
val inspect2 = new TestInspector_Children()
26+
inspect2.inspect("", List("Flavor"))
27+
assert(inspect2.kids == List("Vanilla","Chocolate","Bourbon"))
28+
}
29+
}
30+
31+
class TestInspector_Children() extends TastyInspector:
32+
33+
var kids: List[String] = Nil
34+
35+
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
36+
import reflect._
37+
inspectClass(reflect)(root)
38+
39+
private def inspectClass(reflect: Reflection)(tree: reflect.Tree): Unit =
40+
import reflect.{given _, _}
41+
tree match {
42+
case t: reflect.PackageClause =>
43+
t.stats.map( m => inspectClass(reflect)(m) )
44+
case t: reflect.ClassDef =>
45+
kids = t.symbol.children.map(_.fullName)
46+
47+
case x =>
48+
}

0 commit comments

Comments
 (0)