Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

surface (fix): Support Scala 3 opaque type aliases with type args #3319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object ReflectSurfaceFactory extends LogSupport {
val tpe = cs.toType
ofType(tpe) match {
// Workaround for sbt's layered class loader, which cannot find the original classes using the reflect mirror
case Alias(_, _, AnyRefSurface) if cs.isTrait =>
case Alias(_, _, AnyRefSurface, _) if cs.isTrait =>
new GenericSurface(cls)
case other => other
}
Expand Down Expand Up @@ -436,7 +436,8 @@ object ReflectSurfaceFactory extends LogSupport {

val name = symbol.asType.name.decodedName.toString
val fullName = s"${prefix.typeSymbol.fullName}.${name}"
val a = Alias(name, fullName, inner)
val typeArgs = typeArgsOf(alias).map(surfaceOf(_)).toIndexedSeq
val a = Alias(name, fullName, inner, typeArgs)
a
}

Expand Down Expand Up @@ -619,7 +620,7 @@ object ReflectSurfaceFactory extends LogSupport {
// For example, trait MyTag, which has no implementation will be just an java.lang.Object
val name = t.typeSymbol.name.decodedName.toString
val fullName = s"${prefix.typeSymbol.fullName}.${name}"
Alias(name, fullName, AnyRefSurface)
Alias(name, fullName, AnyRefSurface, Seq.empty)
case t @ RefinedType(List(_, baseType), decl) =>
// For traits with extended methods
new GenericSurface(resolveClass(baseType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ private[surface] object SurfaceMacros {
}
val name = symbol.asType.name.decodedName.toString
val fullName = s"${prefix.typeSymbol.fullName}.${name}"
q"wvlet.airframe.surface.Alias(${name}, ${fullName}, $inner)"
val typeArgs = args.map(surfaceOf(_))
q"wvlet.airframe.surface.Alias(${name}, ${fullName}, ${inner}, Seq(..${typeArgs}))"
}

private val higherKindedTypeFactory: SurfaceFactory = {
Expand Down Expand Up @@ -626,7 +627,7 @@ private[surface] object SurfaceMacros {

private val genericSurfaceFactory: SurfaceFactory = {
case t if t =:= typeOf[Any] =>
q"wvlet.airframe.surface.Alias(${"Any"}, ${"scala.Any"}, wvlet.airframe.surface.AnyRefSurface)"
q"wvlet.airframe.surface.Alias(${"Any"}, ${"scala.Any"}, wvlet.airframe.surface.AnyRefSurface, Seq.empty)"
case t @ TypeRef(prefix, symbol, args) if !args.isEmpty =>
val typeArgs = typeArgsOf(t).map(surfaceOf(_))
q"new wvlet.airframe.surface.GenericSurface(classOf[$t], typeArgs = IndexedSeq(..$typeArgs))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q) {
val inner = surfaceOf(t.dealias)
val name = Expr(alias.name)
val fullName = Expr(fullTypeNameOf(t))
'{ Alias(${ name }, ${ fullName }, ${ inner }) }
val typeArgs = typeArgsOf(t.simplified).map(surfaceOf(_))
'{ Alias(${ name }, ${ fullName }, ${ inner }, ${ Expr.ofSeq(typeArgs) }) }
case t if t.typeSymbol.isType && t.typeSymbol.isAliasType && !belongsToScalaDefault(t) =>
val dealiased = t.dealias
// println(s"=== alias factory: ${t}, ${dealiased}, ${t.simplified}")
Expand All @@ -224,7 +225,8 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q) {
val s = t.typeSymbol
val name = Expr(s.name)
val fullName = Expr(fullTypeNameOf(t.asType))
'{ Alias(${ name }, ${ fullName }, ${ inner }) }
val typeArgs = typeArgsOf(t.simplified).map(surfaceOf(_))
'{ Alias(${ name }, ${ fullName }, ${ inner }, ${ Expr.ofSeq(typeArgs) }) }
}

private def higherKindedTypeFactory: Factory = {
Expand Down Expand Up @@ -437,7 +439,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q) {

private def genericTypeFactory: Factory = {
case t if t =:= TypeRepr.of[Any] =>
'{ Alias("Any", "scala.Any", AnyRefSurface) }
'{ Alias("Any", "scala.Any", AnyRefSurface, Seq.empty) }
case a: AppliedType =>
val typeArgs = a.args.map(surfaceOf(_))
'{ new GenericSurface(${ clsOf(a) }, typeArgs = ${ Expr.ofSeq(typeArgs) }.toIndexedSeq) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,20 @@ object Primitive {
case object BigInteger extends PrimitiveSurface(classOf[java.math.BigInteger])
}

case class Alias(override val name: String, override val fullName: String, ref: Surface)
extends GenericSurface(ref.rawType, ref.typeArgs, ref.params, ref.objectFactory) {
override def toString: String = s"${name}:=${ref.name}"
case class Alias(
override val name: String,
override val fullName: String,
ref: Surface,
override val typeArgs: Seq[Surface] = Seq.empty
) extends GenericSurface(ref.rawType, typeArgs, ref.params, ref.objectFactory) {
override def toString: String = {
val typeSuffix = if (typeArgs.isEmpty) {
""
} else {
s"[${typeArgs.map(_.name).mkString(",")}]"
}
s"${name}${typeSuffix}:=${ref.name}"
}
override def isAlias: Boolean = true
override def isPrimitive: Boolean = ref.isPrimitive
override def isOption: Boolean = ref.isOption
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.surface
import wvlet.airspec.AirSpec

object OpaqueTypeTest extends AirSpec:
opaque type Objc[A] <: Map[String, A] = Map[String, A]
type AliasType[A] = Map[String, A]

class Local0:
def related: AliasType[Any] = null

test("type alias with type args") {
val x2 = Surface.methodsOf[Local0]
val m1 = x2.find(_.name == "related").get
m1.returnType.toString shouldBe "AliasType[Any]:=Map[String,Any]"
}

class Local1:
def related: Objc[Any] = null

test("opaque type with type args") {
val x2 = Surface.methodsOf[Local1]
val m1 = x2.find(_.name == "related").get
m1.returnType.toString shouldBe "Objc[Any]:=Map[String,Any]"
}
Loading