Skip to content

Commit

Permalink
scaladoc: fix ThisType rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian3k committed May 31, 2023
1 parent ef97436 commit 35ca2db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions scaladoc-testcases/src/tests/exports1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class A: //unexpected
type HKT[T[_], X] //expected: final type HKT = [T[_], X] =>> a.HKT[T, X]
= T[X]
type SomeRandomType = (List[_] | Seq[_]) & String //expected: final type SomeRandomType = a.SomeRandomType
def x[T[_], X](x: X): HKT[T, X] //expected: def x[T[_], X](x: X): A#HKT[T, X]
def x[T[_], X](x: X): HKT[T, X] //expected: def x[T[_], X](x: X): A.this.HKT[T, X]
= ???
def fn[T, U]: T => U
= ???
object Object //expected: val Obj: Object.type
val x: HKT[List, Int] //expected: val x: A#HKT[List, Int]
val x: HKT[List, Int] //expected: val x: A.this.HKT[List, Int]
= ???
class Class(val a: Int, val b: Int) extends Serializable //expected: final type Class = a.Class
enum Enum: //expected: final type Enum = a.Enum
Expand Down
32 changes: 19 additions & 13 deletions scaladoc/src/dotty/tools/scaladoc/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ trait TypesSupport:
case ByNameType(tpe) => keyword("=> ") :: inner(tpe)
case ConstantType(constant) =>
plain(constant.show).l
case ThisType(tpe) => inner(tpe)
case ThisType(tpe) =>
val prefix = findSupertype(elideThis, tpe.typeSymbol) match
case Some(_) => Nil
case None => inner(tpe) ++ plain(".").l
val suffix = if skipTypeSuffix then Nil else List(plain("."), keyword("type"))
prefix ++ keyword("this").l ++ suffix
case AnnotatedType(AppliedType(_, Seq(tpe)), annotation) if isRepeatedAnnotation(annotation) =>
inner(tpe) :+ plain("*")
case AppliedType(repeatedClass, Seq(tpe)) if isRepeated(repeatedClass) =>
Expand Down Expand Up @@ -220,10 +225,6 @@ trait TypesSupport:
}) ++ plain("]").l

case tp @ TypeRef(qual, typeName) =>
def defaultSignature() =
val suffix = keyword("#").l ++ tpe(tp.typeSymbol)
inParens(inner(qual), shouldWrapInParens(qual, tp, true)) ++ suffix

qual match {
case r: RecursiveThis => tpe(s"this.$typeName").l
case t if skipPrefix(t, elideThis) =>
Expand All @@ -232,21 +233,23 @@ trait TypesSupport:
val suffix = if tp.typeSymbol == Symbol.noSymbol then tpe(typeName).l else tpe(tp.typeSymbol)
inner(qual)(using skipTypeSuffix = true) ++ plain(".").l ++ suffix
case ThisType(tr) =>
import dotty.tools.scaladoc.tasty.SymOps.isHiddenByVisibility

val supertype = getSupertypes(elideThis).filterNot((s, t) => s.isHiddenByVisibility).find((s, t) => s == tr.typeSymbol)
supertype match
findSupertype(elideThis, tr.typeSymbol) match
case Some((sym, AppliedType(tr2, args))) =>
sym.tree.asInstanceOf[ClassDef].constructor.paramss.headOption match
case Some(TypeParamClause(tpc)) =>
tpc.zip(args).collectFirst {
case (TypeDef(name, _), arg) if name == typeName => arg
} match
case Some(tr) => inner(tr)
case _ => defaultSignature()
case _ => defaultSignature()
case _ => defaultSignature()
case _ => defaultSignature()
case None => tpe(tp.typeSymbol)
case _ => tpe(tp.typeSymbol)
case Some(_) => tpe(tp.typeSymbol)
case None =>
val sig = inParens(inner(qual)(using skipTypeSuffix = true), shouldWrapInParens(qual, tp, true))
sig ++ plain(".").l ++ tpe(tp.typeSymbol)
case _ =>
val sig = inParens(inner(qual), shouldWrapInParens(qual, tp, true))
sig ++ keyword("#").l ++ tpe(tp.typeSymbol)
}

case tr @ TermRef(qual, typeName) =>
Expand Down Expand Up @@ -326,6 +329,9 @@ trait TypesSupport:
regularTypeBounds(low, high)
case _ => regularTypeBounds(low, high)

private def findSupertype(using Quotes)(c: reflect.ClassDef, sym: reflect.Symbol) =
getSupertypes(c).find((s, t) => s == sym)

private def skipPrefix(using Quotes)(tr: reflect.TypeRepr, elideThis: reflect.ClassDef) =
import reflect._

Expand Down

0 comments on commit 35ca2db

Please sign in to comment.