Skip to content

Commit

Permalink
Merge pull request #406 from sjrd/fix-path-dependent-opaque-erasure
Browse files Browse the repository at this point in the history
Fix #403: Take the prefix of opaque type aliases into account in erasure.
  • Loading branch information
sjrd authored Nov 30, 2023
2 parents b8008da + 9eb273a commit ee609d3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
21 changes: 6 additions & 15 deletions tasty-query/shared/src/main/scala/tastyquery/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ private[tastyquery] object Erasure:
else ClassRef(cls).arrayOf()
case tpe: TypeRef =>
tpe.optSymbol match
case Some(sym: TypeMemberSymbol) =>
sym.typeDef match
case TypeMemberDefinition.TypeAlias(alias) => arrayOf(alias)
case TypeMemberDefinition.AbstractType(bounds) => arrayOfBounds(bounds)
case TypeMemberDefinition.OpaqueTypeAlias(_, alias) => arrayOf(alias)
case Some(sym: TypeMemberSymbol) if sym.isOpaqueTypeAlias =>
arrayOf(tpe.translucentSuperType)
case _ =>
arrayOfBounds(tpe.bounds)
tpe.bounds match
case bounds: AbstractTypeBounds => arrayOfBounds(bounds)
case TypeAlias(alias) => arrayOf(alias)
case tpe: TypeParamRef => arrayOfBounds(tpe.bounds)
case tpe: Type => preErase(tpe, keepUnit = false).arrayOf()
case tpe: WildcardTypeArg => arrayOfBounds(tpe.bounds)
Expand All @@ -81,15 +80,7 @@ private[tastyquery] object Erasure:
if !keepUnit && cls.isUnit then ClassRef(defn.ErasedBoxedUnitClass)
else ClassRef(cls)
case tpe: TypeRef =>
tpe.optSymbol match
case Some(sym: TypeMemberSymbol) =>
sym.typeDef match
case TypeMemberDefinition.OpaqueTypeAlias(_, alias) =>
preErase(alias, keepUnit)
case _ =>
preErase(tpe.underlying, keepUnit)
case _ =>
preErase(tpe.underlying, keepUnit)
preErase(tpe.translucentSuperType, keepUnit)
case tpe: SingletonType =>
preErase(tpe.underlying, keepUnit)
case tpe: TypeParamRef =>
Expand Down
2 changes: 1 addition & 1 deletion tasty-query/shared/src/main/scala/tastyquery/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ object Types {
private[tastyquery] override def translucentSuperType(using Context): Type = optSymbol match
case Some(sym: TypeMemberSymbol) =>
sym.typeDef match
case TypeMemberDefinition.OpaqueTypeAlias(_, alias) => alias
case TypeMemberDefinition.OpaqueTypeAlias(_, alias) => alias.asSeenFrom(prefix, sym.owner)
case _ => underlying
case _ =>
underlying
Expand Down
10 changes: 10 additions & 0 deletions tasty-query/shared/src/test/scala/tastyquery/SignatureSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ class SignatureSuite extends UnrestrictedUnpicklingSuite:
assertSigned(mOpaqueWithBounds, "(scala.Null):scala.Null")
}

testWithContext("path-dependent-opaque-type-alias") {
val PathDependentOpaqueBarClass = ctx.findStaticClass("simple_trees.PathDependentOpaque.Bar")

val bar = PathDependentOpaqueBarClass.findNonOverloadedDecl(termName("bar"))
assertSigned(bar, "(java.lang.String):scala.Unit")

val get = PathDependentOpaqueBarClass.findNonOverloadedDecl(termName("get"))
assertSigned(get, "():java.lang.String")
}

testWithContext("scala2-case-class-varargs") {
val StringContext = ctx.findTopLevelClass("scala.StringContext")

Expand Down
19 changes: 19 additions & 0 deletions test-sources/src/main/scala/simple_trees/PathDependentOpaque.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package simple_trees

object PathDependentOpaque:
trait Foo[T]:
opaque type Type <: T = T

protected def make(t: T): Type = t
end Foo

class Bar extends Foo[String]:
def bar(x: Type): Unit = ()

def get(): Type = make("hello")
end Bar

def test(): Unit =
val b = new Bar()
b.bar(b.get())
end PathDependentOpaque

0 comments on commit ee609d3

Please sign in to comment.