Repository: scalacenter/tasty-query (scala)

Issue:
erasure of java.lang.reflect.Method in tasty definition causes MatchError
## source code
```scala
// compiled by Scala 3
package javacompat

object LazyVals:
  def getOffsetStatic(field: java.lang.reflect.Field) = 1L
```

## Test case
```scala
// SignatureSuite
testWithContext("package-ref-from-tasty") {
  val LazyVals = resolve(name"javacompat" / tname"LazyVals" / obj).asClass
  val getOffsetStatic = LazyVals.getDecl(name"getOffsetStatic").get.asTerm
  assertIsSignedName(getOffsetStatic.signedName, "getOffsetStatic", "(java.lang.reflect.Field):scala.Long")
}
```

## Error

```
java.lang.ClassCastException: tastyquery.Symbols$PackageSymbol cannot be cast to tastyquery.Symbols$TermSymbol
    at tastyquery.Types$TermRef.asThisSymbolType(Types.scala:739)
    at tastyquery.Types$TermRef.asThisSymbolType(Types.scala:738)
    at tastyquery.Types$NamedType.symbol(Types.scala:557)
    at tastyquery.Types$TermRef.computeUnderlying(Types.scala:753)
    at tastyquery.Types$TermRef.underlying(Types.scala:747)
    at tastyquery.Types$NamedType.findPrefixSym$1(Types.scala:590)
    at tastyquery.Types$NamedType.computeSymbol(Types.scala:600)
    at tastyquery.Types$NamedType.symbol(Types.scala:557)
    at tastyquery.Types$TermRef.computeUnderlying(Types.scala:753)
    at tastyquery.Types$TermRef.underlying(Types.scala:747)
    at tastyquery.Types$NamedType.findPrefixSym$1(Types.scala:590)
    at tastyquery.Types$NamedType.computeSymbol(Types.scala:600)
    at tastyquery.Types$NamedType.symbol(Types.scala:557)
    at tastyquery.Types$ErasedTypeRef$.preErase(Types.scala:119)
    at tastyquery.Types$ErasedTypeRef$.erase(Types.scala:69)
    at tastyquery.Signatures$Signature$.$anonfun$1(Signatures.scala:33)
    at scala.collection.immutable.List.map(List.scala:246)
    at tastyquery.Signatures$Signature$.rec$1(Signatures.scala:33)
    at tastyquery.Signatures$Signature$.fromMethodic(Signatures.scala:44)
    at tastyquery.Symbols$TermSymbol.signature(Symbols.scala:218)
    at tastyquery.Symbols$TermSymbol.signedName(Symbols.scala:225)
    at tastyquery.SignatureSuite.$init$$$anonfun$20(SignatureSuite.scala:216)
```

Pull request description:
fixes #145

Patch:
diff --git a/tasty-query/shared/src/main/scala/tastyquery/Trees.scala b/tasty-query/shared/src/main/scala/tastyquery/Trees.scala
index f0e17729..2a515abc 100644
--- a/tasty-query/shared/src/main/scala/tastyquery/Trees.scala
+++ b/tasty-query/shared/src/main/scala/tastyquery/Trees.scala
@@ -314,7 +314,7 @@ object Trees {
   case class Select(qualifier: Tree, name: TermName)(span: Span) extends Tree(span) {
     // not final because it is overridden in SelectIn
     protected def calculateType(using Context): Type =
-      NamedType(qualifier.tpe, name)
+      NamedType.possibleSelFromPackage(qualifier.tpe, name)
 
     override def withSpan(span: Span): Select = Select(qualifier, name)(span)
   }
diff --git a/tasty-query/shared/src/main/scala/tastyquery/TypeTrees.scala b/tasty-query/shared/src/main/scala/tastyquery/TypeTrees.scala
index 989d8c91..dbfef255 100644
--- a/tasty-query/shared/src/main/scala/tastyquery/TypeTrees.scala
+++ b/tasty-query/shared/src/main/scala/tastyquery/TypeTrees.scala
@@ -87,7 +87,7 @@ object TypeTrees {
   /** qualifier.name */
   case class TermRefTypeTree(qualifier: Tree, name: TermName)(span: Span) extends TypeTree(span) {
     override protected def calculateType(using Context): Type =
-      TermRef(qualifier.tpe, name)
+      NamedType.possibleSelFromPackage(qualifier.tpe, name)
 
     override final def withSpan(span: Span): TermRefTypeTree = TermRefTypeTree(qualifier, name)(span)
   }
diff --git a/tasty-query/shared/src/main/scala/tastyquery/Types.scala b/tasty-query/shared/src/main/scala/tastyquery/Types.scala
index 3a1c7859..4c455efa 100644
--- a/tasty-query/shared/src/main/scala/tastyquery/Types.scala
+++ b/tasty-query/shared/src/main/scala/tastyquery/Types.scala
@@ -699,6 +699,15 @@ object Types {
   }
 
   object NamedType {
+
+    private[tastyquery] def possibleSelFromPackage(prefix: Type, name: TermName)(using Context): Type = prefix match
+      case prefix: PackageRef if name.isInstanceOf[SimpleName] =>
+        prefix.symbol.getPackageDeclInternal(name.asSimpleName) match
+          case Some(nested) => PackageRef(nested)
+          case _            => apply(prefix, name)
+      case prefix =>
+        apply(prefix, name)
+
     def apply(prefix: Type, sym: Symbol)(using Context): NamedType =
       sym match
         case sym: TypeSymbol => TypeRef(prefix, sym)


Test patch:
diff --git a/tasty-query/shared/src/test/scala/tastyquery/SignatureSuite.scala b/tasty-query/shared/src/test/scala/tastyquery/SignatureSuite.scala
index a4cd317c..945f93cd 100644
--- a/tasty-query/shared/src/test/scala/tastyquery/SignatureSuite.scala
+++ b/tasty-query/shared/src/test/scala/tastyquery/SignatureSuite.scala
@@ -210,4 +210,10 @@ class SignatureSuite extends UnrestrictedUnpicklingSuite:
     assertIsSignedName(arrayOfIntArrayOps.signedName, "arrayOfIntArrayOps", "(scala.Int[][]):inheritance.MyArrayOps[]")
   }
 
+  testWithContext("package-ref-from-tasty") {
+    val LazyVals = resolve(name"javacompat" / tname"LazyVals" / obj).asClass
+    val getOffsetStatic = LazyVals.getDecl(name"getOffsetStatic").get.asTerm
+    assertIsSignedName(getOffsetStatic.signedName, "getOffsetStatic", "(java.lang.reflect.Field):scala.Long")
+  }
+
 end SignatureSuite
diff --git a/test-sources/src/main/scala/javacompat/LazyVals.scala b/test-sources/src/main/scala/javacompat/LazyVals.scala
new file mode 100644
index 00000000..c1568b74
--- /dev/null
+++ b/test-sources/src/main/scala/javacompat/LazyVals.scala
@@ -0,0 +1,4 @@
+package javacompat
+
+object LazyVals:
+  def getOffsetStatic(field: java.lang.reflect.Field) = 1L