Skip to content

Commit ce8b3bd

Browse files
committed
Fix presentation of HKT and TypeLambdas in Scala3doc
1 parent 49c59f5 commit ce8b3bd

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tests
2+
package hkts
3+
4+
5+
class Case1Variant1[List[_]]
6+
class Case1Variant2[List <: ([_] =>> Any)]
7+
8+
type Case2Variant1 = [A, _] =>> List[A]
9+
type Case2Variant2[A, _] = List[A]
10+
11+
type Case3Variant1 = [_] =>> Int
12+
type Case3Variant2[_] = Int
13+
14+
class A1[X]
15+
class A2[X] extends A1[X]
16+
type Case4Variant1 >: ([X] =>> A2[X]) <: ([X] =>> A1[X])
17+
type Case4Variant2[X] >: A2[X] <: A1[X]
18+
19+
type Case5Variant1 = [X] =>> [Y] =>> (X, Y)
20+
type Case5Variant2[X, Y] = (X, Y)
21+
22+
type Case6Variant1 = [A, B] =>> A => B
23+
type Case6Variant2[A, B] = A => B
24+
25+
class Case7Variant1[List[_] <: Number]
26+
class Case7Variant2[List <: ([_] =>> Number)]
27+
28+
class Case8Variant1[A, List[A] <: Number]
29+
class Case8Variant2[A, List <: ([A] =>> Number)]
30+
31+
type Case9Variant1 <: [X] =>> Option[X]
32+
type Case9Variant2[+X] <: Option[X]
33+
34+
class Case10Variant1[List[_ >: Number]]
35+
class Case10Variant2[List <: ([_ >: Number] =>> Any)]
36+
37+
class Case11Variant1[Map[_, _]]
38+
class Case11Variant2[Map <: ([_, _] =>> Any)]
39+
40+
class Case12Variant1[List[A >: Number]]
41+
class Case12Variant2[List <: ([A >: Number] =>> Any)]
42+
43+
def method1[E, T](value: List[_ >: E]): Int = 0
44+
def method2[F[+X] <: Option[X], A](fa: F[A]): A = fa.get

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ trait ClassLikeSupport:
144144
Some(parseMethod(c, dd.symbol))
145145

146146
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
147-
Some(parseTypeDef(td))
147+
Some(parseTypeDef(c, td))
148148

149149
case vd: ValDef if !isSyntheticField(vd.symbol)
150150
&& (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum))
@@ -292,7 +292,7 @@ trait ClassLikeSupport:
292292

293293
val enumTypes = companion.membersToDocument.collect {
294294
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && td.symbol.flags.is(Flags.Enum) && td.symbol.flags.is(Flags.Case) => td
295-
}.toList.map(parseTypeDef)
295+
}.toList.map(parseTypeDef(classDef, _))
296296

297297
val enumNested = companion.membersToDocument.collect {
298298
case c: ClassDef if c.symbol.flags.is(Flags.Case) && c.symbol.flags.is(Flags.Enum) => processTree(c)(parseClasslike(c))
@@ -378,7 +378,7 @@ trait ClassLikeSupport:
378378
if argument.symbol.flags.is(Flags.Covariant) then "+"
379379
else if argument.symbol.flags.is(Flags.Contravariant) then "-"
380380
else ""
381-
val name = argument.symbol.normalizedName
381+
val name = argument.symbol.normalizedName.takeWhile(_ != '$')
382382
TypeParameter(
383383
argument.symbol.getAnnotations(),
384384
variancePrefix,
@@ -387,15 +387,17 @@ trait ClassLikeSupport:
387387
memberInfo.get(name).fold(argument.rhs.asSignature)(_.asSignature)
388388
)
389389

390-
def parseTypeDef(typeDef: TypeDef): Member =
390+
def parseTypeDef(c: ClassDef, typeDef: TypeDef): Member =
391391
def isTreeAbstract(typ: Tree): Boolean = typ match {
392392
case TypeBoundsTree(_, _) => true
393393
case LambdaTypeTree(params, body) => isTreeAbstract(body)
394394
case _ => false
395395
}
396396

397+
val memberInfo = unwrapMemberInfo(c, typeDef.symbol)
397398
val (generics, tpeTree) = typeDef.rhs match
398-
case LambdaTypeTree(params, body) => (params.map(mkTypeArgument(_)), body)
399+
case LambdaTypeTree(params, body) =>
400+
(params.map(mkTypeArgument(_)), body)
399401
case tpe => (Nil, tpe)
400402

401403
val kind = Kind.Type(!isTreeAbstract(typeDef.rhs), typeDef.symbol.isOpaque, generics)

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait TypesSupport:
4040
extension (tpeTree: Tree)
4141
def asSignature: DocSignature =
4242
tpeTree match
43-
case TypeBoundsTree(low, high) => typeBound(low.tpe, low = true) ++ typeBound(high.tpe, low = false)
43+
case TypeBoundsTree(low, high) => typeBoundsTreeOfHigherKindedType(low, high)
4444
case tpeTree: TypeTree => inner(tpeTree.tpe)
4545
case term: Term => inner(term.tpe)
4646

@@ -95,9 +95,10 @@ trait TypesSupport:
9595
case AnnotatedType(tpe, _) =>
9696
inner(tpe)
9797
case tl @ TypeLambda(params, paramBounds, resType) =>
98-
// println(params)
99-
// println(paramBounds)
100-
texts("[") ++ commas(params.zip(paramBounds).map( (name, typ) => texts(s"${name}") ++ inner(typ) )) ++ texts("]")
98+
texts("[") ++ commas(params.zip(paramBounds).map { (name, typ) =>
99+
val normalizedName = name.takeWhile(_ != '$')
100+
texts(normalizedName) ++ inner(typ)
101+
}) ++ texts("]")
101102
++ texts(" =>> ")
102103
++ inner(resType)
103104

@@ -184,7 +185,10 @@ trait TypesSupport:
184185
Nil
185186
case args =>
186187
texts("(") ++ commas(args.map(inner)) ++ texts(")")
187-
else inner(tpe) ++ texts("[") ++ commas(typeList.map(inner)) ++ texts("]")
188+
else inner(tpe) ++ texts("[") ++ commas(typeList.map { t => t match
189+
case _: TypeBounds => texts("_") ++ inner(t)
190+
case _ => inner(t)
191+
}) ++ texts("]")
188192

189193
case tp @ TypeRef(qual, typeName) =>
190194
qual match {
@@ -267,3 +271,17 @@ trait TypesSupport:
267271
case _ => Nil
268272
}
269273

274+
private def typeBoundsTreeOfHigherKindedType(low: TypeTree, high: TypeTree) =
275+
def regularTypeBounds(low: TypeTree, high: TypeTree) =
276+
typeBound(low.tpe, low = true) ++ typeBound(high.tpe, low = false)
277+
high.tpe.match
278+
case t: TypeLambda => t.match
279+
case TypeLambda(params, paramBounds, resType) =>
280+
if resType.typeSymbol == defn.AnyClass && params.foldLeft(true)((acc,e) => acc && e.contains("$")) then
281+
texts("[") ++ commas(paramBounds.map { typ =>
282+
texts("_") ++ inner(typ)
283+
}) ++ texts("]")
284+
else
285+
regularTypeBounds(low, high)
286+
case _ => regularTypeBounds(low, high)
287+
case _ => regularTypeBounds(low, high)

0 commit comments

Comments
 (0)