Skip to content

Scala3doc: improve member lookup #11302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scala3doc-testcases/src/tests/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ package tests
*/
class A {

type X = tests.B
type Y <: tests.B

/** This is my method.
*
* This is a link: [[AA]].
Expand Down
13 changes: 9 additions & 4 deletions scala3doc/src/dotty/dokka/tasty/comments/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ abstract class MarkupConversion[T](val repr: Repr)(using DocContext) {
case Some((sym, targetText)) =>
DocLink.ToDRI(sym.dri, targetText)
case None =>
val msg = s"Not found any dri for query"
// TODO convert owner.pos to get to the comment, change to warning
report.inform(s"$msg: $queryStr")
DocLink.UnresolvedDRI(queryStr, msg)
val txt = s"No DRI found for query"
val msg = s"$txt: $queryStr"
// TODO change to the commented-out version when we'll get rid of the warnings in stdlib
// report.warning(
// msg,
// owner.pos.get.asInstanceOf[dotty.tools.dotc.util.SrcPos],
// )
report.inform(msg)
DocLink.UnresolvedDRI(queryStr, txt)

private val SchemeUri = """[a-z]+:.*""".r

Expand Down
33 changes: 29 additions & 4 deletions scala3doc/src/dotty/dokka/tasty/comments/MemberLookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ trait MemberLookup {
if sym.isClassDef || sym.flags.is(Flags.Package) then sym else nearestMembered(sym.owner)

val res =
def toplevelLookup(querystrings: List[String]) =
downwardLookup(querystrings, defn.PredefModule.moduleClass)
.orElse(downwardLookup(querystrings, defn.ScalaPackage))
.orElse(downwardLookup(querystrings, defn.RootPackage))

ownerOpt match {
case Some(owner) =>
val nearest = nearestMembered(owner)
val nearestCls = nearestClass(owner)
val nearestPkg = nearestPackage(owner)
def relativeLookup(querystrings: List[String]) =
// TODO walk the owner chain?
downwardLookup(querystrings, nearestPkg).orElse(toplevelLookup(querystrings))
query match {
case Query.StrictMemberId(id) => localLookup(id, nearest).map(_ -> id)
case Query.Id(id) =>
(localLookup(id, nearest) orElse localLookup(id, nearestPkg)).map(_ -> id)
(localLookup(id, nearest) orElse relativeLookup(List(id))).map(_ -> id)
case Query.QualifiedId(Query.Qual.This, _, rest) =>
downwardLookup(rest.asList, nearestCls).map(_ -> rest.join)
case Query.QualifiedId(Query.Qual.Package, _, rest) =>
Expand All @@ -43,11 +51,12 @@ trait MemberLookup {
downwardLookup(rest.asList, nearestCls).map(_ -> rest.join)
case Query.QualifiedId(Query.Qual.Id(id), _, rest) if id == nearestPkg.name =>
downwardLookup(rest.asList, nearestPkg).map(_ -> rest.join)
case query: Query.QualifiedId => downwardLookup(query.asList, defn.RootPackage).map(_ -> query.join)
case query: Query.QualifiedId =>
relativeLookup(query.asList).map(_ -> query.join)
}

case None =>
downwardLookup(query.asList, defn.RootPackage).map(_ -> query.join)
toplevelLookup(query.asList).map(_ -> query.join)
}

// println(s"looked up `$query` in ${owner.show}[${owner.flags.show}] as ${res.map(_.show)}")
Expand Down Expand Up @@ -103,7 +112,12 @@ trait MemberLookup {
def hackResolveModule(s: Symbol): Symbol =
if s.flags.is(Flags.Module) then s.moduleClass else s

val matched = syms.find(matches)
// val syms0 = syms.toList
// val matched0 = syms0.find(matches)
// if matched0.isEmpty then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally left it commented out, it's going to be useful when debugging again.

// println(s"Failed to look up $q in $owner; all members below:")
// syms0.foreach { s => println(s"\t$s") }
// val matched = matched0

// def showMatched() = matched.foreach { s =>
// println(s">>> ${s.show}")
Expand All @@ -115,6 +129,7 @@ trait MemberLookup {
// println(s"localLookup for class ${owner.show} of `$q`{forceTerm=$forceTerm}")
// showMatched()

val matched = syms.find(matches)
matched.map(hackResolveModule)
}

Expand All @@ -124,6 +139,16 @@ trait MemberLookup {
owner.tree match {
case tree: ClassDef =>
findMatch(tree.body.iterator.collect { case t: Definition if hackIsNotAbsent(t.symbol) => t.symbol })
case tree: TypeDef =>
val tpe =
tree.rhs match {
case tb : TypeBoundsTree => tb.hi.tpe
case tpt: TypeTree => tpt.tpe
}

tpe.classSymbol.flatMap { s =>
findMatch(hackMembersOf(s))
}
case _ =>
findMatch(hackMembersOf(owner))
}
Expand Down
13 changes: 13 additions & 0 deletions scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {

def testOwnerlessLookup(): Unit = {
val cases = List[(String, Sym)](
"Array" -> cls("scala.Array"),
"Option" -> cls("scala.Option"),
"Predef$.identity" -> cls("scala.Predef$").fun("identity"),
"Array$.from" -> cls("scala.Array$").fun("from"),
"???" -> cls("scala.Predef$").fun("???"),
"tests.A" -> cls("tests.A"),
"tests.A$" -> cls("tests.A$"),
"tests.Methods.simple" -> cls("tests.Methods").fun("simple"),
Expand Down Expand Up @@ -61,6 +66,14 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {

cls("tests.A").fun("method") -> "B" -> cls("tests.B"),
cls("tests.A").fun("method") -> "B$" -> cls("tests.B$"),

cls("tests.A") -> "B.method" -> cls("tests.B").fun("method"),
cls("tests.A") -> "Option" -> cls("scala.Option"),

/*sanity*/ cls("tests.A") -> "this.X" -> cls("tests.A").tpe("X"),
/*sanity*/ cls("tests.A") -> "this.Y" -> cls("tests.A").tpe("Y"),
cls("tests.A") -> "this.X.method" -> cls("tests.B").fun("method"),
cls("tests.A") -> "this.Y.method" -> cls("tests.B").fun("method"),
)

cases.foreach { case ((Sym(owner), query), Sym(target)) =>
Expand Down
2 changes: 1 addition & 1 deletion tasty/src/dotty/tools/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ object TastyFormat {
final val PositionsSection = "Positions"
final val CommentsSection = "Comments"

/** Tags used to serialize names, should update [[nameTagToString]] if a new constant is added */
/** Tags used to serialize names, should update [[TastyFormat$.nameTagToString]] if a new constant is added */
class NameTags {
final val UTF8 = 1 // A simple name in UTF8 encoding.

Expand Down