Skip to content

Commit 35ca2db

Browse files
committed
scaladoc: fix ThisType rendering
1 parent ef97436 commit 35ca2db

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

scaladoc-testcases/src/tests/exports1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class A: //unexpected
1717
type HKT[T[_], X] //expected: final type HKT = [T[_], X] =>> a.HKT[T, X]
1818
= T[X]
1919
type SomeRandomType = (List[_] | Seq[_]) & String //expected: final type SomeRandomType = a.SomeRandomType
20-
def x[T[_], X](x: X): HKT[T, X] //expected: def x[T[_], X](x: X): A#HKT[T, X]
20+
def x[T[_], X](x: X): HKT[T, X] //expected: def x[T[_], X](x: X): A.this.HKT[T, X]
2121
= ???
2222
def fn[T, U]: T => U
2323
= ???
2424
object Object //expected: val Obj: Object.type
25-
val x: HKT[List, Int] //expected: val x: A#HKT[List, Int]
25+
val x: HKT[List, Int] //expected: val x: A.this.HKT[List, Int]
2626
= ???
2727
class Class(val a: Int, val b: Int) extends Serializable //expected: final type Class = a.Class
2828
enum Enum: //expected: final type Enum = a.Enum

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ trait TypesSupport:
101101
case ByNameType(tpe) => keyword("=> ") :: inner(tpe)
102102
case ConstantType(constant) =>
103103
plain(constant.show).l
104-
case ThisType(tpe) => inner(tpe)
104+
case ThisType(tpe) =>
105+
val prefix = findSupertype(elideThis, tpe.typeSymbol) match
106+
case Some(_) => Nil
107+
case None => inner(tpe) ++ plain(".").l
108+
val suffix = if skipTypeSuffix then Nil else List(plain("."), keyword("type"))
109+
prefix ++ keyword("this").l ++ suffix
105110
case AnnotatedType(AppliedType(_, Seq(tpe)), annotation) if isRepeatedAnnotation(annotation) =>
106111
inner(tpe) :+ plain("*")
107112
case AppliedType(repeatedClass, Seq(tpe)) if isRepeated(repeatedClass) =>
@@ -220,10 +225,6 @@ trait TypesSupport:
220225
}) ++ plain("]").l
221226

222227
case tp @ TypeRef(qual, typeName) =>
223-
def defaultSignature() =
224-
val suffix = keyword("#").l ++ tpe(tp.typeSymbol)
225-
inParens(inner(qual), shouldWrapInParens(qual, tp, true)) ++ suffix
226-
227228
qual match {
228229
case r: RecursiveThis => tpe(s"this.$typeName").l
229230
case t if skipPrefix(t, elideThis) =>
@@ -232,21 +233,23 @@ trait TypesSupport:
232233
val suffix = if tp.typeSymbol == Symbol.noSymbol then tpe(typeName).l else tpe(tp.typeSymbol)
233234
inner(qual)(using skipTypeSuffix = true) ++ plain(".").l ++ suffix
234235
case ThisType(tr) =>
235-
import dotty.tools.scaladoc.tasty.SymOps.isHiddenByVisibility
236-
237-
val supertype = getSupertypes(elideThis).filterNot((s, t) => s.isHiddenByVisibility).find((s, t) => s == tr.typeSymbol)
238-
supertype match
236+
findSupertype(elideThis, tr.typeSymbol) match
239237
case Some((sym, AppliedType(tr2, args))) =>
240238
sym.tree.asInstanceOf[ClassDef].constructor.paramss.headOption match
241239
case Some(TypeParamClause(tpc)) =>
242240
tpc.zip(args).collectFirst {
243241
case (TypeDef(name, _), arg) if name == typeName => arg
244242
} match
245243
case Some(tr) => inner(tr)
246-
case _ => defaultSignature()
247-
case _ => defaultSignature()
248-
case _ => defaultSignature()
249-
case _ => defaultSignature()
244+
case None => tpe(tp.typeSymbol)
245+
case _ => tpe(tp.typeSymbol)
246+
case Some(_) => tpe(tp.typeSymbol)
247+
case None =>
248+
val sig = inParens(inner(qual)(using skipTypeSuffix = true), shouldWrapInParens(qual, tp, true))
249+
sig ++ plain(".").l ++ tpe(tp.typeSymbol)
250+
case _ =>
251+
val sig = inParens(inner(qual), shouldWrapInParens(qual, tp, true))
252+
sig ++ keyword("#").l ++ tpe(tp.typeSymbol)
250253
}
251254

252255
case tr @ TermRef(qual, typeName) =>
@@ -326,6 +329,9 @@ trait TypesSupport:
326329
regularTypeBounds(low, high)
327330
case _ => regularTypeBounds(low, high)
328331

332+
private def findSupertype(using Quotes)(c: reflect.ClassDef, sym: reflect.Symbol) =
333+
getSupertypes(c).find((s, t) => s == sym)
334+
329335
private def skipPrefix(using Quotes)(tr: reflect.TypeRepr, elideThis: reflect.ClassDef) =
330336
import reflect._
331337

0 commit comments

Comments
 (0)