Skip to content

Commit

Permalink
surface (fix): Support Scala 3 opaque type aliases with type args
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Dec 23, 2023
1 parent d1f3bf4 commit 55348c4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
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]
) 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]"
}

0 comments on commit 55348c4

Please sign in to comment.