Skip to content

Commit f5e84f4

Browse files
authored
Merge pull request #11123 from BarkingBad/scala3doc/typedef-defdef-issues
Fix cast exception while traversing supertypes in Scala3doc
2 parents 338ea7a + 88c1548 commit f5e84f4

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

scala3doc-testcases/src/tests/fieldsSignatures.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ object Documentation
3838
{
3939
val valInsideDocObject: Nothing
4040
= ???
41-
}
41+
}

scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,23 @@ trait ClassLikeSupport:
6161
modifiers: Seq[Modifier] = classDef.symbol.getExtraModifiers(),
6262
): Member =
6363

64-
// This Try is here because of problem that code compiles, but at runtime fails claiming
65-
// java.lang.ClassCastException: class dotty.tools.dotc.ast.Trees$DefDef cannot be cast to class dotty.tools.dotc.ast.Trees$TypeDef (dotty.tools.dotc.ast.Trees$DefDef and dotty.tools.dotc.ast.Trees$TypeDef are in unnamed module of loader 'app')
66-
// It is probably bug in Tasty
67-
def hackGetParents(classDef: ClassDef): Option[List[Tree]] = scala.util.Try(classDef.parents).toOption
68-
69-
def getSupertypesGraph(classDef: ClassDef, link: LinkToType): Seq[(LinkToType, LinkToType)] =
64+
def unpackTreeToClassDef(tree: Tree): ClassDef = tree match
65+
case tree: ClassDef => tree
66+
case TypeDef(_, tbt: TypeBoundsTree) => unpackTreeToClassDef(tbt.tpe.typeSymbol.tree)
67+
case TypeDef(_, tt: TypeTree) => unpackTreeToClassDef(tt.tpe.typeSymbol.tree)
68+
case c: Apply =>
69+
c.symbol.owner.tree.symbol.tree match
70+
case tree: ClassDef => tree
71+
case tt: TypeTree => unpackTreeToClassDef(tt.tpe.typeSymbol.tree)
72+
73+
def getSupertypesGraph(classDef: Tree, link: LinkToType): Seq[(LinkToType, LinkToType)] =
7074
val smbl = classDef.symbol
71-
val parents = if smbl.exists then hackGetParents(smbl.tree.asInstanceOf[ClassDef]) else None
72-
parents.fold(Seq())(_.flatMap { case tree =>
73-
val symbol = if tree.symbol.isClassConstructor then tree.symbol.owner else tree.symbol
74-
val superLink = LinkToType(tree.dokkaType.asSignature, symbol.dri, bareClasslikeKind(symbol))
75-
Seq(link -> superLink) ++ getSupertypesGraph(tree.asInstanceOf[ClassDef], superLink)
76-
}
77-
)
75+
val parents = unpackTreeToClassDef(classDef).parents
76+
parents.flatMap { case tree =>
77+
val symbol = if tree.symbol.isClassConstructor then tree.symbol.owner else tree.symbol
78+
val superLink = LinkToType(tree.dokkaType.asSignature, symbol.dri, bareClasslikeKind(symbol))
79+
Seq(link -> superLink) ++ getSupertypesGraph(tree, superLink)
80+
}
7881

7982
val supertypes = getSupertypes(using qctx)(classDef).map {
8083
case (symbol, tpe) =>

scala3doc/test/dotty/dokka/diagram/HierarchyTest.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ class HierarchyTest extends ScaladocTest("hierarchy"):
5252
assertEquals(
5353
Set(
5454
"Object" -> "Any",
55-
// "A1" -> "Object", // These are not applicable beacuase of bug and its workaround
56-
// "A2[Int]" -> "Object", // More info at ClassLikeSupport.scala:37
57-
// "A3[Int, String]" -> "Object",
58-
// "B1" -> "Object",
59-
// "B1" -> "A1",
60-
// "B2" -> "Object",
61-
// "B2" -> "A1",
62-
// "B2" -> "A2[Int]",
63-
// "B3" -> "Object",
64-
// "B3" -> "A2[Int]",
65-
// "B3" -> "A3[Int, String]",
66-
// "C1[Int, Boolean, Any]" -> "Object",
67-
// "C1[Int, Boolean, Any]" -> "B1",
68-
// "C1[Int, Boolean, Any]" -> "B2",
69-
// "C1[Int, Boolean, Any]" -> "B3",
55+
"A1" -> "Object",
56+
"A2[Int]" -> "Object",
57+
"A3[Int, String]" -> "Object",
58+
"B1" -> "Object",
59+
"B1" -> "A1",
60+
"B2" -> "Object",
61+
"B2" -> "A1",
62+
"B2" -> "A2[Int]",
63+
"B3" -> "Object",
64+
"B3" -> "A2[Int]",
65+
"B3" -> "A3[Int, String]",
66+
"C1[Int, Boolean, Any]" -> "Object",
67+
"C1[Int, Boolean, Any]" -> "B1",
68+
"C1[Int, Boolean, Any]" -> "B2",
69+
"C1[Int, Boolean, Any]" -> "B3",
7070
"Object" -> "Matchable",
7171
"Matchable" -> "Any",
7272
"E2" -> "D2[Int, Boolean]",

0 commit comments

Comments
 (0)