Skip to content
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

Redesign Names. #366

Merged
merged 21 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1fa9983
Set the version policy intention to None for upcoming changes.
sjrd Oct 18, 2023
a6057ee
Remove Name.isEmpty.
sjrd Oct 18, 2023
e1fd69e
Split the tag-discrimated names into separate case classes.
sjrd Oct 18, 2023
8eb2172
Fix ObjectClassName.toString to include the trainling '$'.
sjrd Oct 18, 2023
643ae3f
Use `signatureName` instead of `fullName` where it makes more sense.
sjrd Oct 19, 2023
cb606a3
Only use TermName's in signatureName's.
sjrd Oct 19, 2023
5cc2f32
Move `Symbol.fullName` to `PackageSymbol`; introduce `Symbol.displayF…
sjrd Oct 19, 2023
ff6a6e1
Split `FullyQualifiedName` into `PackageFullName` and `SignatureName`.
sjrd Oct 19, 2023
8ff2f76
Enforce that the underlying of `ObjectClassName` is a `SimpleName`.
sjrd Oct 19, 2023
cecd09a
Restrict `toTypeName` to `TermName`, and `toTermName` to `TypeName`.
sjrd Oct 19, 2023
f194fd0
Limit the places where we use `toTypeName` on arbitrary `TermName`s.
sjrd Oct 19, 2023
b459d87
Remove `NumberedName`, and seal the `Name` hierarchy.
sjrd Oct 19, 2023
c359a1a
Remove `Name.asSimpleName`.
sjrd Oct 19, 2023
30ac821
Move Scala 2 name decoding to the Scala unpickler.
sjrd Oct 19, 2023
254e53e
Remove `Name.is{Term,Type}Name`.
sjrd Oct 19, 2023
4039b54
Use the `SimpleTypeName` extractor everywhere it makes sense.
sjrd Oct 20, 2023
976d563
Remove some `TypeName`s that were in `nme`.
sjrd Oct 20, 2023
34df7c1
Change the order of the params of `UniqueName`.
sjrd Oct 20, 2023
2928c0a
Introduce a separate hierarchy of `TypeName`s.
sjrd Oct 20, 2023
9a0500d
Remove `DerivedName`.
sjrd Oct 20, 2023
fc96252
Rename `wrapsObjectName` to `isObjectClassTypeName`.
sjrd Oct 20, 2023
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
29 changes: 21 additions & 8 deletions tasty-query/shared/src/main/scala/tastyquery/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,21 @@ object Symbols {
throw AssertionError(s"Cannot construct static owner prefix for non-static-owner symbol $this")
end staticOwnerPrefix

private def nameWithPrefix(addPrefix: Symbol => Boolean): FullyQualifiedName =
if isPackage && (owner == null || name == nme.EmptyPackageName) then FullyQualifiedName.rootPackageName
else
val pre = owner
if pre != null && addPrefix(pre) then pre.nameWithPrefix(addPrefix).mapLastOption(_.toTermName).select(name)
else FullyQualifiedName(name :: Nil)

final def fullName: FullyQualifiedName = nameWithPrefix(_.isStatic)
/** A full name of this symbol for display purposes, such as debugging or error messages.
*
* `displayFullName` must not be used to identify symbols, as it is not unique.
*/
final def displayFullName: String = this match
case sym: PackageSymbol =>
if sym.isRootPackage then "_root_"
else if sym.name == nme.EmptyPackageName then "<empty>"
else sym.fullName.toString()
case sym: TermOrTypeSymbol =>
val owner = sym.owner
if owner.name == nme.EmptyPackageName then name.toString()
else if owner.isStatic then owner.displayFullName + "." + name.toString()
else name.toString()
end displayFullName

final def isType: Boolean = this.isInstanceOf[TypeSymbol]
final def isTerm: Boolean = this.isInstanceOf[TermSymbol]
Expand Down Expand Up @@ -1689,6 +1696,12 @@ object Symbols {
this.withFlags(EmptyFlagSet, None)
this.setAnnotations(Nil)

private lazy val _fullName: FullyQualifiedName =
if owner == null || name == nme.EmptyPackageName then FullyQualifiedName.rootPackageName
Copy link
Member

@bishabosha bishabosha Oct 20, 2023

Choose a reason for hiding this comment

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

why isn't this emptyPackageName for the empty package? I guess consistency in that <empty> does not appear in the prefix, but just checking

Copy link
Member

Choose a reason for hiding this comment

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

I can see its just a refactoring, so not really important

else owner.fullName.select(name)

final def fullName: FullyQualifiedName = _fullName

private[tastyquery] def getPackageDeclOrCreate(name: SimpleName): PackageSymbol =
getPackageDecl(name).getOrElse {
assert(name != nme.EmptyPackageName, s"Trying to create a subpackage $name of $this")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private[classfiles] final class ClassfileReader private () {
}

private def rootName(classOwner: DeclaringSymbol, classRoot: ClassData): String =
s"${classOwner.fullName}.${classRoot.binaryName}"
s"${classOwner.displayFullName}.${classRoot.binaryName}"

private def acceptMagicNumber(classOwner: DeclaringSymbol, classRoot: ClassData)(using DataStream): Unit = {
val magic = data.readU4()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private[classfiles] object JavaSignatures:
referenceType(null)

def cookFailure(tname: TypeName, reason: String): Nothing =
val path = if !isClass then s"${member.owner.fullName}#${member.name}" else member.fullName
val path = if !isClass then s"${member.owner.displayFullName}#${member.name}" else member.displayFullName
throw ClassfileFormatException(
s"could not resolve type parameter `$tname` in signature `$signature` of $path because $reason"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {
private object SymbolWithName:
def unapply(sym: Symbol): Some[sym.ThisNameType] = Some(sym.name)

private object SymbolWithFullName:
def unapplySeq(sym: Symbol): Option[List[Name]] = Some(sym.fullName.path)
private object PackageWithFullName:
def unapplySeq(sym: PackageSymbol): Option[List[Name]] = Some(sym.fullName.path)

private object ScalaPackageRef:
def unapply(tree: PackageRef): Boolean = tree.fullyQualifiedName.path == List(termName("scala"))
Expand Down Expand Up @@ -255,7 +255,7 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {
val nestedPackages: StructureCheck = {
case PackageDef(
SymbolWithName(SimpleName("simple_trees")),
List(PackageDef(SymbolWithFullName(SimpleName("simple_trees"), SimpleName("nested")), _))
List(PackageDef(PackageWithFullName(SimpleName("simple_trees"), SimpleName("nested")), _))
) =>
}

Expand All @@ -264,7 +264,7 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {

testUnpickleTopLevel("qualified-nested-package", "simple_trees.nested.InQualifiedNestedPackage") { tree =>
val packageCheck: StructureCheck = {
case PackageDef(SymbolWithFullName(SimpleName("simple_trees"), SimpleName("nested")), _) =>
case PackageDef(PackageWithFullName(SimpleName("simple_trees"), SimpleName("nested")), _) =>
}

assert(containsSubtree(packageCheck)(clue(tree)))
Expand Down
18 changes: 15 additions & 3 deletions tasty-query/shared/src/test/scala/tastyquery/SymbolSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ class SymbolSuite extends RestrictedUnpicklingSuite {
assertContainsExactly(ctx.findPackage("empty_class"), Set(tname"EmptyClass"))
}

testWithContext("package-full-names", "simple_trees.nested.InNestedPackage") {
assert(clue(defn.RootPackage.fullName) == FullyQualifiedName.rootPackageName)
assert(clue(defn.EmptyPackage.fullName) == FullyQualifiedName.rootPackageName)
assert(clue(defn.scalaPackage.fullName) == FullyQualifiedName.scalaPackageName)

val simpleTreesPackage = ctx.findPackage("simple_trees")
assert(clue(simpleTreesPackage.fullName) == FullyQualifiedName(List(termName("simple_trees"))))

val nestedPackage = ctx.findPackage("simple_trees.nested")
assert(clue(nestedPackage.fullName) == FullyQualifiedName(List(termName("simple_trees"), termName("nested"))))
}

testWithContext("basic-symbol-structure-nested", "simple_trees.nested.InNestedPackage") {
ctx.findTopLevelClass("simple_trees.nested.InNestedPackage")
// InNestedPackage is the only declaration in the simple_trees.nested package
Expand Down Expand Up @@ -145,15 +157,15 @@ class SymbolSuite extends RestrictedUnpicklingSuite {

val InNestedPackageClass = ctx.findTopLevelClass("simple_trees.nested.InNestedPackage")

assert(InNestedPackageClass.fullName.toString == "simple_trees.nested.InNestedPackage")
assert(InNestedPackageClass.displayFullName == "simple_trees.nested.InNestedPackage")

val simpleTreesPkg = ctx.findPackage("simple_trees")

assert(simpleTreesPkg.fullName.toString == "simple_trees")
assert(simpleTreesPkg.displayFullName == "simple_trees")

val (simpleTreesNestedPkg: PackageSymbol) = simpleTreesPkg.getDecl(name"nested").get: @unchecked

assert(simpleTreesNestedPkg.fullName.toString == "simple_trees.nested")
assert(simpleTreesNestedPkg.displayFullName == "simple_trees.nested")

assert(simpleTreesPkg.getDecl(name"nested") == Some(simpleTreesNestedPkg))
}
Expand Down