Skip to content

Commit

Permalink
Do not crash when reading a Scala 2 macro definition.
Browse files Browse the repository at this point in the history
It contains an annotation with a subtree that is an `IDENTtree`
without prefix nor symbol, with the name `macro`. That is then
`Apply`ed and `TypeApply`ed in ways that make no semantic sense.

We define a special symbol in `Definitions`, which we attach to
such fake identifiers.
  • Loading branch information
sjrd committed Dec 14, 2023
1 parent f450e46 commit 12152c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
.checkCompleted()
end scala2FakeOwner

private[tastyquery] val scala2MacroInfoFakeMethod: TermSymbol =
TermSymbol
.createNotDeclaration(nme.m_macro, scalaPackage)
.withFlags(Synthetic, None)
.withDeclaredType(NothingType)
.setAnnotations(Nil)
.checkCompleted()
end scala2MacroInfoFakeMethod

private def createSpecialTypeAlias(
name: TypeName,
owner: DeclaringSymbol,
Expand Down
2 changes: 2 additions & 0 deletions tasty-query/shared/src/main/scala/tastyquery/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ object Names {

val m_apply: SimpleName = termName("apply")
val m_unapply: SimpleName = termName("unapply")

private[tastyquery] val m_macro: SimpleName = termName("macro")
}

object tpnme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ private[reader] final class ReaderContext(underlying: Context):

def scala2FakeOwner: TermSymbol = underlying.defn.scala2FakeOwner

def scala2MacroInfoFakeMethod: TermSymbol = underlying.defn.scala2MacroInfoFakeMethod

def findPackageFromRootOrCreate(fullyQualifiedName: PackageFullName): PackageSymbol =
underlying.findPackageFromRootOrCreate(fullyQualifiedName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,12 @@ private[pickles] class PickleReader {
val designator = readMaybeExternalSymbolRef()
val name = readTermNameRef()
val tpe: TermReferenceType = designator match
case sym: TermSymbol => sym.localRef
case sym: PackageSymbol => sym.packageRef
case external: ExternalSymbolRef => external.toTermRef(NoPrefix)
case _ => errorBadSignature(s"illegal $designator for IDENTtree (name '$name')")
case sym: TermSymbol => sym.localRef
case sym: PackageSymbol => sym.packageRef
case external: ExternalSymbolRef => external.toTermRef(NoPrefix)
case _: NoExternalSymbolRef if name == nme.m_macro => rctx.scala2MacroInfoFakeMethod.localRef
case _ =>
errorBadSignature(s"illegal $designator for IDENTtree (name '$name')")
Ident(name)(tpe)(pos)

case LITERALtree =>
Expand Down

0 comments on commit 12152c4

Please sign in to comment.