Skip to content

Commit

Permalink
Merge pull request #285 from sjrd/fix-read-type-bindings-tree
Browse files Browse the repository at this point in the history
Fix #284: Read BLOCK's in TypeTree position as TypeBindingsTree.
  • Loading branch information
bishabosha authored Apr 17, 2023
2 parents c5704a7 + 565cc83 commit 4497b2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tasty-query/shared/src/main/scala/tastyquery/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ object Trees {
case TypeTreeBind(name, body, symbol) => body :: Nil
case WildcardTypeBoundsTree(bounds) => bounds :: Nil
case TypeLambdaTree(tparams, body) => tparams ::: body :: Nil
case TypeBindingsTree(bindings, body) => bindings ::: body :: Nil

case InferredTypeBoundsTree(bounds) => Nil
case ExplicitTypeBoundsTree(low, high) => low :: high :: Nil
Expand Down Expand Up @@ -795,6 +796,12 @@ object Trees {
override final def withSpan(span: Span): TypeLambdaTree = TypeLambdaTree(tparams, body)(span)
}

final case class TypeBindingsTree(bindings: List[TypeMember], body: TypeTree)(span: Span) extends TypeTree(span):
override protected def calculateType(using Context): Type = body.toType

override def withSpan(span: Span): TypeBindingsTree = TypeBindingsTree(bindings, body)(span)
end TypeBindingsTree

// --- TypeDefinitionTrees and TypeBoundsTrees ------------------------------

sealed abstract class TypeDefinitionTree(span: Span) extends Tree(span):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,14 @@ private[tasties] class TreeUnpickler(
val spn = span
reader.readByte()
ByNameTypeTree(readTypeTree)(spn)
case BLOCK =>
// #284 See QuotesAndSplices.typeQuoteMatching
val spn = span
reader.readByte()
val end = reader.readEnd()
val body = readTypeTree
val bindings = readStats(end).map(_.asInstanceOf[TypeMember])
TypeBindingsTree(bindings, body)(spn)
case SHAREDterm =>
val spn = span
reader.readByte()
Expand Down
22 changes: 22 additions & 0 deletions tasty-query/shared/src/test/scala/tastyquery/ReadTreeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2290,4 +2290,26 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {
}
assert(containsSubtree(abstractVarCheck)(clue(tree)))
}

testUnpickle("quotes-and-splices", "simple_trees.QuotesAndSplices$") { tree =>
val typeQuoteMatchingDef = findTree(tree) { case dd @ DefDef(SimpleName("typeQuoteMatching"), _, _, _, _) =>
dd
}
val typeQuoteMatchingCaseDef = findTree(typeQuoteMatchingDef) { case cd: CaseDef =>
cd
}
val typeQuoteMatchingCheck: StructureCheck = {
case TypeBindingsTree(
List(
TypeMember(SimpleTypeName("t"), InferredTypeBoundsTree(_), tSym),
TypeMember(SimpleTypeName("u"), InferredTypeBoundsTree(_), uSym)
),
AppliedTypeTree(
TypeIdent(SimpleTypeName("Map")),
List(TypeWrapper(TypeRefInternal(NoPrefix, tSymRef)), TypeWrapper(TypeRefInternal(NoPrefix, uSymRef)))
)
) if tSymRef == tSym && uSymRef == uSym && tSym != uSym =>
}
assert(containsSubtree(typeQuoteMatchingCheck)(clue(typeQuoteMatchingCaseDef)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package simple_trees

import scala.quoted.*

object QuotesAndSplices:
def typeQuoteMatching(using quotes: Quotes): Expr[Unit] =
Type.of[Any] match
case '[Map[t, u]] => '{ () }
end QuotesAndSplices

0 comments on commit 4497b2a

Please sign in to comment.