Skip to content

Commit

Permalink
Fix partially zio#30 (only Scala 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-yu committed Mar 27, 2022
1 parent a993f0a commit fc9d748
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ final class TagMacro(using override val qctx: Quotes) extends InspectorBase {
private def summonCombinedTag[T <: AnyKind: Type](typeRepr: TypeRepr): Expr[Tag[T]] =
typeRepr.dealias match {

case x if x.typeSymbol.isTypeParam => summonTag(x)

case AppliedType(ctor, args) =>
val ctorTag = createTagExpr(ctor.asType)
val argsTags = Expr.ofList(args.map(mkLtt))
Expand All @@ -64,27 +62,42 @@ final class TagMacro(using override val qctx: Quotes) extends InspectorBase {
val ltts: Expr[List[LightTypeTag]] = Expr.ofList(ltts0)
'{ Tag.unionTag[T](classOf[Any], $ltts) }

case x if x.typeSymbol.isTypeParam => summonTag(x)

case _ =>
throw new Exception(s"Unsupported type: $typeRepr")
}

private def summonTag[any <: AnyKind](typeRepr: TypeRepr): Expr[Tag[any]] =
summonTagEither(typeRepr) match {
case Right(tag) => tag.asInstanceOf[Expr[Tag[any]]]
case Left(message) => report.errorAndAbort(message)
}

private def summonTagEither[any <: AnyKind](typeRepr: TypeRepr): Either[String, Expr[Tag[any]]] =
typeRepr.asType match {
case given Type[a] =>
val message = s"Cannot find implicit Tag[${Type.show[a]}]"
Expr.summon[Tag[a]].getOrElse(report.errorAndAbort(message)).asInstanceOf[Expr[Tag[any]]]
Expr.summon[Tag[a]] match {
case Some(tag) => Right(tag.asInstanceOf[Expr[Tag[any]]])
case None => Left(message)
}
}

private def summonable[any <: AnyKind](typeRepr: TypeRepr): Boolean =
summonTagEither(typeRepr).isRight

/**
* Returns true if the given type contains no type parameters
* (this means the type is not "weak" https://stackoverflow.com/questions/29435985/weaktypetag-v-typetag)
*/
private def allPartsStrong(typeRepr: TypeRepr): Boolean =
typeRepr.dealias match {
case x if x.typeSymbol.isTypeParam => false
case x if x.typeSymbol.isTypeParam && summonable(x) => false
case AppliedType(tpe, args) => allPartsStrong(tpe) && args.forall(allPartsStrong)
case AndType(lhs, rhs) => allPartsStrong(lhs) && allPartsStrong(rhs)
case OrType(lhs, rhs) => allPartsStrong(lhs) && allPartsStrong(rhs)
case x if x.typeSymbol.isTypeParam => false
case TypeRef(tpe, name) => allPartsStrong(tpe)
case TermRef(tpe, name) => allPartsStrong(tpe)
case ThisType(tpe) => allPartsStrong(tpe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class TagTest extends SharedTagTest with TagAssertions {
assertNotChild(Tag[Animal | String].tag, Tag[Dog | String].tag)
}

"Support HKTag for unapplied type lambdas with type bounds" in {
trait X
trait XAble[A <: X]
class Y extends X

def getTag[F[_ <: X]: Tag.auto.T] = Tag[F[Y]]

assertSame(getTag[XAble].tag, Tag[XAble[Y]].tag)
}
}

}
Expand Down

0 comments on commit fc9d748

Please sign in to comment.