Skip to content

Commit

Permalink
fix: companion lookup
Browse files Browse the repository at this point in the history
Co-authored-by: dylandoamaral <[email protected]>
Co-authored-by: luintas <[email protected]>
Co-authored-by: bishabosha <[email protected]>
  • Loading branch information
3 people committed Nov 3, 2022
1 parent 31491b2 commit 6b3f3a6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tasty-query/shared/src/main/scala/tastyquery/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ object Symbols {
* - for `object class C[$]` => `class C`
*/
final def companionClass(using Context): Option[ClassSymbol] = maybeOuter match
case scope: PackageSymbol =>
case scope: DeclaringSymbol =>
scope.getDecl(this.name.companionName).collect { case sym: ClassSymbol =>
sym
}
Expand Down
1 change: 1 addition & 0 deletions tasty-query/shared/src/test/scala/tastyquery/Paths.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ object Paths:
}
def show: String = path.mkString(".")
def debug: String = toDebugString(path)
def asObj: DeclarationPath = path.convertAsObject

extension [T <: DeclarationPath](path: T)
private def convertAsObject: T =
Expand Down
21 changes: 21 additions & 0 deletions tasty-query/shared/src/test/scala/tastyquery/TypeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,25 @@ class TypeSuite extends UnrestrictedUnpicklingSuite {
assert(clue(childToString.allOverriddenSymbols.toList) == List(superToString, objectToString))
assert(clue(childToString.nextOverriddenSymbol) == Some(superToString))
}

def companionClassFullCycle(path: DeclarationPath)(using Context, munit.Location): Unit = {
val cls: ClassSymbol = resolve(path).asClass
val moduleClass: ClassSymbol = resolve(path.asObj).asClass

assert(cls == moduleClass.companionClass.get)
assert(moduleClass.companionClass.get == cls)
}

testWithContext("companion-tests-module-value") {
companionClassFullCycle(name"companions" / tname"CompanionObject")
}

testWithContext("companion-tests-nested-module-value") {
companionClassFullCycle(name"companions" / tname"CompanionObject" / obj / tname"NestedObject")
}

testWithContext("companion-tests-class-nested-module-value") {
companionClassFullCycle(name"companions" / tname"CompanionObject" / tname"ClassNestedObject")
}

}
10 changes: 10 additions & 0 deletions test-sources/src/main/scala/companions/CompanionObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package companions

class CompanionObject {
class ClassNestedObject
object ClassNestedObject
}
object CompanionObject {
class NestedObject
object NestedObject
}

0 comments on commit 6b3f3a6

Please sign in to comment.