Skip to content

Commit

Permalink
Upgrade to Scala 3.4.0 and support its TASTy format.
Browse files Browse the repository at this point in the history
Changes notably include:

* Handling `Lambda`s that are poly functions.
* Weakening a test affected by scalacenter#436.
  • Loading branch information
sjrd committed Feb 20, 2024
1 parent 1a61e54 commit acc2856
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt.internal.util.ManagedLogger

import org.scalajs.jsenv.nodejs.NodeJSEnv

val usedScalaCompiler = "3.3.1"
val usedScalaCompiler = "3.4.0"
val usedTastyRelease = usedScalaCompiler
val scala2Version = "2.13.12"

Expand Down
37 changes: 24 additions & 13 deletions tasty-query/shared/src/main/scala/tastyquery/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,32 @@ object Trees {
tpt.toType

case None =>
val methodType = meth.tpe.widenTermRef match
case mt: MethodType if !mt.resultType.isInstanceOf[MethodicType] =>
mt
case mt =>
throw InvalidProgramStructureException(s"Unexpected type for the `meth` part of a Lambda: $mt")

val paramCount = methodType.paramNames.size
val functionNTypeRef = defn.FunctionNClass(paramCount).staticRef

if methodType.isResultDependent then
val parent = functionNTypeRef.appliedTo(List.fill(paramCount + 1)(defn.AnyType))
TermRefinement(parent, isStable = false, nme.m_apply, methodType)
else functionNTypeRef.appliedTo(methodType.paramTypes :+ methodType.resultType.asInstanceOf[Type])
convertMethodTypeToFunctionType(meth.tpe.widenTermRef)
end calculateType

private def convertMethodTypeToFunctionType(tpe: TermType)(using Context): Type =
tpe match
case tpe: MethodType if tpe.resultType.isInstanceOf[Type] =>
val paramCount = tpe.paramNames.size
val functionNTypeRef = defn.FunctionNClass(paramCount).staticRef

if tpe.isResultDependent then
val parent = functionNTypeRef.appliedTo(List.fill(paramCount + 1)(defn.AnyType))
TermRefinement(parent, isStable = false, nme.m_apply, tpe)
else functionNTypeRef.appliedTo(tpe.paramTypes :+ tpe.resultType.asInstanceOf[Type])

case tpe: PolyType if tpe.resultType.isInstanceOf[MethodType] =>
val polyFunctionClass = defn.PolyFunctionClass.getOrElse {
throw InvalidProgramStructureException(
s"Found a polymorphic Lambda but PolyFunction is not on the classpath"
)
}
TermRefinement(polyFunctionClass.staticRef, isStable = false, nme.m_apply, tpe)

case _ =>
throw InvalidProgramStructureException(s"Unexpected type for the `meth` part of a Lambda: ${tpe.showBasic}")
end convertMethodTypeToFunctionType

/** The class symbol of the SAM type of this lambda.
*
* A `Lambda` can be considered as an anonymous class of the form `new tpt { ... }`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private[tasties] object TastyFormat:
* compatibility, but remains backwards compatible, with all
* preceeding `MinorVersion`.
*/
final val MinorVersion: Int = 3
final val MinorVersion: Int = 4

/** Natural Number. The `ExperimentalVersion` allows for
* experimentation with changes to TASTy without committing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class PositionSuite extends RestrictedUnpicklingSuite {
"(x: Int) => x + 1",
"() => ()",
"T] => T => T", // TODO Improve this
"T] => (x: T) => x", // TODO Improve this
"[T] => (x: T) => x",
"(x: Any) => x.type",
"x => x"
)
Expand Down
58 changes: 18 additions & 40 deletions tasty-query/shared/src/test/scala/tastyquery/ReadTreeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -631,22 +631,25 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {
val test1Def = findTree(tree) { case test1Def @ DefDef(SimpleName("test1"), _, _, _, _) =>
test1Def
}
val matchTree = findTree(test1Def) { case mt: Match =>
mt
}

val forExpressionMatch1: StructureCheck = {
case CaseDef(
Unapply(
TypeApply(Select(Ident(SimpleName("Tuple2")), SignedName(SimpleName("unapply"), _, _)), _),
Nil,
List(
Bind(i, WildcardPattern(TypeRefInternal(_, SimpleTypeName("Int"))), _),
Bind(SimpleName("i"), WildcardPattern(TypeRefInternal(_, SimpleTypeName("Int"))), _),
WildcardPattern(TypeRefInternal(_, SimpleTypeName("String")))
)
),
None,
Literal(Constant(true))
_
) =>
}
assert(containsSubtree(forExpressionMatch1)(clue(test1Def)))
assert(containsSubtree(forExpressionMatch1)(clue(matchTree)))
}

testUnpickle("singletonType", "simple_trees.SingletonType") { tree =>
Expand Down Expand Up @@ -959,50 +962,25 @@ class ReadTreeSuite extends RestrictedUnpicklingSuite {
Some(
Block(
List(
ClassDef(
_,
Template(
_,
List(
Apply(Select(New(TypeWrapper(_)), _), List()),
TypeWrapper(TypeRefInternal(_, SimpleTypeName("PolyFunction")))
),
None,
List(
DefDef(
SimpleName("apply"),
List(
Right(List(TypeParam(SimpleTypeName("T"), _, _))),
Left(List(ValDef(SimpleName("x"), TypeIdent(SimpleTypeName("T")), None, _)))
),
TypeIdent(SimpleTypeName("T")),
Some(Ident(SimpleName("x"))),
_
)
)
DefDef(
SimpleName("$anonfun"),
List(
Right(List(TypeParam(SimpleTypeName("T"), _, _))),
Left(List(ValDef(SimpleName("x"), _, _, _)))
),
_
_,
_,
defSym
)
),
Typed(
Apply(Select(New(TypeIdent(_)), _), List()),
TypeWrapper(
ty.TermRefinement(
TypeRefInternal(_, SimpleTypeName("PolyFunction")),
SimpleName("apply"),
polyType @ ty.PolyType(
List(SimpleTypeName("T") -> _),
ty.MethodType(List(SimpleName("x") -> (tref1: TypeParamRef)), tref2: TypeParamRef)
)
)
)
)
Lambda(defRef @ Ident(SimpleName("$anonfun")), None)
)
),
_
) if Seq(tref1, tref2).forall(tref => (tref.binder eq polyType) && tref.paramNum == 0) =>
) if defRef.symbol == defSym =>
}
assert(containsSubtree(polyIDMatch)(clue(tree)))
val polyIDVal = findValDefTree(tree, termName("polyID"))
assert(containsSubtree(polyIDMatch)(clue(polyIDVal)))

val dependentIDMatch: StructureCheck = {
case ValDef(
Expand Down
6 changes: 6 additions & 0 deletions tasty-query/shared/src/test/scala/tastyquery/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ object TestUtils:
}
end findLocalValDef

def findValDefTree(body: Tree, name: TermName): ValDef =
findTree(body) {
case vd: ValDef if vd.name == name => vd
}
end findValDefTree

def findTree[A](body: Tree)(test: PartialFunction[Tree, A]): A =
object finder extends TreeTraverser:
var result: Option[A] = None
Expand Down
3 changes: 2 additions & 1 deletion tasty-query/shared/src/test/scala/tastyquery/TypeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,8 @@ class TypeSuite extends UnrestrictedUnpicklingSuite {
testTypeApply("testAsInstanceOfInt", nme.m_asInstanceOf, _.isRef(defn.IntClass))
testTypeApply("testAsInstanceOfProduct", nme.m_asInstanceOf, _.isRef(ProductClass))

testTypeApply("testTypeCast", termName("$asInstanceOf$"), _.isRef(defn.IntClass))
// FIXME #436: the type test should be _.widenTermRef.isRef(defn.IntClass)
testTypeApply("testTypeCast", termName("$asInstanceOf$"), _.isInstanceOf[TermRef])

testApplyTypeApply(
"testGetClassAny",
Expand Down
2 changes: 1 addition & 1 deletion test-sources/src/main/scala/simple_trees/AnyMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AnyMethods:

class Bag extends scala.reflect.Selectable

def testTypeCast(bag: Bag { val m: Int }): Any = bag.m // bag.selectDynamic("m").$asInstanceOf$[Int]
def testTypeCast(bag: Bag { val m: Int }): Any = bag.m // bag.selectDynamic("m").$asInstanceOf$[bag.m.type]

def testGetClassAny(x: Any): Any = x.getClass()
def testGetClassProduct(x: Product): Class[? <: Product] = x.getClass()
Expand Down

0 comments on commit acc2856

Please sign in to comment.