-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix member lookups for inherited members #11821
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tests | ||
package lookupInheritedMembers.pack1 { | ||
class A: | ||
def x = 1 | ||
val y = 1 | ||
type MyType | ||
|
||
} | ||
package lookupInheritedMembers.pack2 { | ||
class B extends tests.lookupInheritedMembers.pack1.A | ||
} | ||
|
||
package lookupInheritedMembers { | ||
/** | ||
* [[tests.lookupInheritedMembers.pack2.B.x]] | ||
* [[tests.lookupInheritedMembers.pack2.B.y]] | ||
* [[tests.lookupInheritedMembers.pack2.B.MyType]] | ||
*/ | ||
class LookupInheritedMembers | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,22 @@ import scala.quoted._ | |
|
||
trait MemberLookup { | ||
|
||
def memberLookupResult(using Quotes)( | ||
symbol: quotes.reflect.Symbol, | ||
label: String, | ||
inheritingParent: Option[quotes.reflect.Symbol] = None | ||
): (quotes.reflect.Symbol, String, Option[quotes.reflect.Symbol]) = | ||
(symbol, label, inheritingParent) | ||
|
||
def lookup(using Quotes, DocContext)( | ||
query: Query, | ||
owner: quotes.reflect.Symbol, | ||
): Option[(quotes.reflect.Symbol, String)] = lookupOpt(query, Some(owner)) | ||
): Option[(quotes.reflect.Symbol, String, Option[quotes.reflect.Symbol])] = lookupOpt(query, Some(owner)) | ||
|
||
def lookupOpt(using Quotes, DocContext)( | ||
query: Query, | ||
ownerOpt: Option[quotes.reflect.Symbol], | ||
): Option[(quotes.reflect.Symbol, String)] = | ||
): Option[(quotes.reflect.Symbol, String, Option[quotes.reflect.Symbol])] = | ||
try | ||
import quotes.reflect._ | ||
|
||
|
@@ -26,7 +33,7 @@ trait MemberLookup { | |
def nearestMembered(sym: Symbol): Symbol = | ||
if sym.isClassDef || sym.flags.is(Flags.Package) then sym else nearestMembered(sym.owner) | ||
|
||
val res: Option[(Symbol, String)] = { | ||
val res: Option[(Symbol, String, Option[Symbol])] = { | ||
def toplevelLookup(querystrings: List[String]) = | ||
downwardLookup(querystrings, defn.PredefModule.moduleClass) | ||
.orElse(downwardLookup(querystrings, defn.ScalaPackage)) | ||
|
@@ -38,7 +45,7 @@ trait MemberLookup { | |
val nearest = nearestMembered(owner) | ||
val nearestCls = nearestClass(owner) | ||
val nearestPkg = nearestPackage(owner) | ||
def relativeLookup(querystrings: List[String], owner: Symbol): Option[Symbol] = { | ||
def relativeLookup(querystrings: List[String], owner: Symbol): Option[(Symbol, Option[Symbol])] = { | ||
val isMeaningful = | ||
owner.exists | ||
// those are just an optimisation, they can be dropped if problems show up | ||
|
@@ -56,26 +63,27 @@ trait MemberLookup { | |
|
||
query match { | ||
case Query.StrictMemberId(id) => | ||
localLookup(id, nearest).nextOption.map(_ -> id) | ||
localLookup(id, nearest).nextOption.map(memberLookupResult(_, id)) | ||
case Query.QualifiedId(Query.Qual.This, _, rest) => | ||
downwardLookup(rest.asList, nearestCls).map(_ -> rest.join) | ||
downwardLookup(rest.asList, nearestCls).map(memberLookupResult(_, rest.join, _)) | ||
case Query.QualifiedId(Query.Qual.Package, _, rest) => | ||
downwardLookup(rest.asList, nearestPkg).map(_ -> rest.join) | ||
downwardLookup(rest.asList, nearestPkg).map(memberLookupResult(_, rest.join, _)) | ||
case query => | ||
val ql = query.asList | ||
toplevelLookup(ql) | ||
.orElse(relativeLookup(ql, nearest)) | ||
.map(_ -> query.join) | ||
.map(memberLookupResult(_, query.join, _)) | ||
} | ||
|
||
case None => | ||
toplevelLookup(query.asList).map(_ -> query.join) | ||
toplevelLookup(query.asList).map(memberLookupResult(_, query.join, _)) | ||
} | ||
} | ||
|
||
// println(s"looked up `$query` in ${owner.show}[${owner.flags.show}] as ${res.map(_.show)}") | ||
|
||
res | ||
|
||
catch | ||
case e: Exception => | ||
// TODO (https://github.com/lampepfl/scala3doc/issues/238): proper reporting | ||
|
@@ -169,11 +177,13 @@ trait MemberLookup { | |
} | ||
} | ||
|
||
private def downwardLookup(using Quotes)(query: List[String], owner: quotes.reflect.Symbol): Option[quotes.reflect.Symbol] = { | ||
private def downwardLookup(using Quotes)( | ||
query: List[String], owner: quotes.reflect.Symbol | ||
): Option[(quotes.reflect.Symbol, Option[quotes.reflect.Symbol])] = { | ||
import quotes.reflect._ | ||
query match { | ||
case Nil => None | ||
case q :: Nil => localLookup(q, owner).nextOption | ||
case q :: Nil => localLookup(q, owner).nextOption.map((_, None)) | ||
case q :: qs => | ||
val lookedUp = | ||
localLookup(q, owner).toSeq | ||
|
@@ -191,8 +201,18 @@ trait MemberLookup { | |
else if s.flags.is(Flags.Module) then tm = Some(s) | ||
else if s.isClassDef || s.isTypeDef then tp = Some(s) | ||
} | ||
|
||
def downwardLookUpForInheritedMembers(qs: List[String], owner: Symbol): Option[(Symbol, Option[Symbol])] = | ||
downwardLookup(qs, owner).orElse(owner.memberMethod(qs.head).headOption.map((_, Some(owner)))).orElse { | ||
val symbol = owner.memberType(qs.head) | ||
Option.when(symbol != dotty.tools.dotc.core.Symbols.NoSymbol)(symbol).map((_, Some(owner))) | ||
}.orElse { | ||
val symbol = owner.memberField(qs.head) | ||
Option.when(symbol != dotty.tools.dotc.core.Symbols.NoSymbol)(symbol).map((_, Some(owner))) | ||
} | ||
|
||
pk.flatMap(downwardLookup(qs, _)) | ||
.orElse(tp.flatMap(downwardLookup(qs, _))) | ||
.orElse(tp.flatMap(downwardLookUpForInheritedMembers(qs, _))) | ||
.orElse(tm.flatMap(downwardLookup(qs, _))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inherited symbols can also appear in term symbols (objects, that is). |
||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it seems that the code we had previously didn't actually look up inherited members at all? In that case it'll have to be changed. We don't want to simply go through Scala Reflect functions here, we also need to take into account absent symbols (see
hackIsNotAbsent
).This code also doesn't seem to work when the inherited symbol is in the middle of the query. In general it'd be better to modify
localLookup
instead ofdownwardLookup
.