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

Work around Scala 3+js bug https://github.com/lampepfl/dotty/issues/17542, make SafeType a wrapper over AnyTag #1944

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait MirrorProvider {
object MirrorProvider {
object Impl extends MirrorProvider {
override def runtimeClass(tpe: SafeType): Option[Class[?]] = {
if (tpe.hasPreciseClass) Some(tpe.cls) else None
if (tpe.hasPreciseClass) Some(tpe.closestClass) else None
}
override def canBeProxied(tpe: SafeType): Boolean = {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait MirrorProvider {
object MirrorProvider {
object Impl extends MirrorProvider {
override def runtimeClass(tpe: SafeType): Option[Class[?]] = {
if (tpe.hasPreciseClass) Some(tpe.cls) else None
if (tpe.hasPreciseClass) Some(tpe.closestClass) else None
}
override def canBeProxied(tpe: SafeType): Boolean = {
runtimeClass(tpe).exists(c => !Modifier.isFinal(c.getModifiers))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final case class Functoid[+A](get: Provider) {
Functoid(newProvider)
}

@inline private def getRetTag: Tag[A @uncheckedVariance] = Tag(get.ret.cls, get.ret.tag)
@inline private def getRetTag: Tag[A @uncheckedVariance] = Tag(get.ret.closestClass, get.ret.tag)
}

object Functoid extends FunctoidMacroMethods with FunctoidLifecycleAdapters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ object DIKey {
def named(id: Identifier): IdKey[id.Id] = IdKey(tpe, id.id, mutatorIndex)(id.idContract)

override def withTpe(tpe: SafeType): DIKey.TypeKey = copy(tpe = tpe)
override def toString: String = formatWithIndex(s"{type.${tpe.toString}}", mutatorIndex)
override def toString: String = formatWithIndex(s"{type.${tpe.tag.repr}}", mutatorIndex)
}

final case class IdKey[I: IdContract](tpe: SafeType, id: I, mutatorIndex: Option[Int] = None) extends BasicKey {
val idContract: IdContract[I] = implicitly
def withMutatorIndex(index: Option[Int]): IdKey[I] = copy(mutatorIndex = index)
override def withTpe(tpe: SafeType): DIKey.IdKey[I] = copy(tpe = tpe)
override def toString: String = formatWithIndex(s"{type.${tpe.toString}@${idContract.repr(id)}}", mutatorIndex)
override def toString: String = formatWithIndex(s"{type.${tpe.tag.repr}@${idContract.repr(id)}}", mutatorIndex)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@ package izumi.distage.model.reflection

import izumi.fundamentals.platform.functional.Identity
import izumi.reflect.macrortti.LightTypeTag
import izumi.reflect.{Tag, TagK, WeakTag}
import izumi.reflect.{AnyTag, Tag, TagK, WeakTag}

final case class SafeType private[izumi] (
tag: LightTypeTag,
/*private[distage] val */ cls: Class[?],
) {
override def toString: String = tag.repr
final class SafeType(private[distage] val anyTag: AnyTag) {
@inline def tag: LightTypeTag = anyTag.tag
@inline def closestClass: Class[?] = anyTag.closestClass
@inline def hasPreciseClass: Boolean = anyTag.hasPreciseClass
@inline def =:=(that: SafeType): Boolean = anyTag =:= that.anyTag
@inline def <:<(that: SafeType): Boolean = anyTag <:< that.anyTag

override def hashCode: Int = tag.hashCode()
override def equals(obj: Any): Boolean = {
obj match {
case that: SafeType =>
tag =:= that.tag
anyTag == that.anyTag
case _ =>
false
}
}
def =:=(that: SafeType): Boolean = tag =:= that.tag
def <:<(that: SafeType): Boolean = tag <:< that.tag

final def hasPreciseClass: Boolean = {
try tag.shortName == cls.getSimpleName
catch {
case i: InternalError if i.getMessage == "Malformed class name" =>
tag.shortName == cls.getName.reverse.takeWhile(c => c != '$' && c != '.').reverse
}
@inline override def hashCode: Int = {
anyTag.hashCode()
}

/**
* The only difference between SafeType and AnyTag is `toString`:
* - SafeType's toString does not have the wrapping 'Tag/HKTag' label
* - SafeType's toString uses `.tag.repr` instead of `.tag.toString`
*/
@inline override def toString: String = {
anyTag.tag.repr
}
}

object SafeType {
def get[T: Tag]: SafeType = SafeType(Tag[T].tag, Tag[T].closestClass)
def getK[K[_]: TagK]: SafeType = SafeType(TagK[K].tag, TagK[K].closestClass)
def unsafeGetWeak[T](implicit weakTag: WeakTag[T]): SafeType = SafeType(weakTag.tag, weakTag.closestClass)
def get[T: Tag]: SafeType = new SafeType(Tag[T])
def getK[K[_]: TagK]: SafeType = new SafeType(TagK[K])
def unsafeGetWeak[T](implicit weakTag: WeakTag[T]): SafeType = new SafeType(WeakTag[T])

lazy val identityEffectType: SafeType = SafeType.getK[Identity]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SafeTypeTest extends AnyWordSpec {
assert(t1.hashCode == t2.hashCode)
assert(t1.toString == t2.toString)

def mk[F[_]: TagK, T: Tag: ClassTag] = SafeType(Tag[F[T]].tag.typeArgs.head, scala.reflect.classTag[T].runtimeClass)
def mk[F[_]: TagK, T: Tag: ClassTag] = new SafeType(Tag(scala.reflect.classTag[T].runtimeClass, Tag[F[T]].tag.typeArgs.head))
val t3 = mk[List, Int]

assert(t1 == t3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ abstract class ProxyStrategyDefaultImplPlatformSpecific(
} else if (param.wasGeneric) {
classOf[Any]
} else {
param.key.tpe.cls
param.key.tpe.closestClass
}

val declaredKey = param.key
Expand All @@ -91,7 +91,7 @@ abstract class ProxyStrategyDefaultImplPlatformSpecific(
param match {
case param if forwardRefs.contains(realKey) || forwardRefs.contains(declaredKey) =>
// substitute forward references by `null`
Right((clazz, TypeUtil.defaultValue(param.key.tpe.cls)))
Right((clazz, TypeUtil.defaultValue(param.key.tpe.closestClass)))
case param =>
context.fetchKey(declaredKey, param.isByName) match {
case Some(v) =>
Expand All @@ -104,7 +104,7 @@ abstract class ProxyStrategyDefaultImplPlatformSpecific(
}

private def noArgsConstructor(tpe: SafeType): Boolean = {
val constructors = tpe.cls.getConstructors
val constructors = tpe.closestClass.getConstructors
constructors.isEmpty || constructors.exists(_.getParameters.isEmpty)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class PlanInterpreterNonSequentialRuntimeImpl(
private[this] def prioritize(ops: Iterable[ExecutableOp], integrationPaths: Set[DIKey]): Seq[ExecutableOp] = ArraySeq.unsafeWrapArray {
ops.toArray.sortBy {
op =>
val repr = op.target.tpe.toString
val repr = op.target.tpe.tag.repr
op match {
case _: ImportDependency =>
(-10, repr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ResourceRewriter(

@inline private[this] def quickSubtypeCheck(tgt: SafeType, implType: SafeType): Boolean = {
if (tgt.hasPreciseClass && implType.hasPreciseClass) {
tgt.cls.isAssignableFrom(implType.cls)
tgt.closestClass.isAssignableFrom(implType.closestClass)
} else {
implType <:< tgt
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object RoleProvider {
}

protected def mkRoleBinding(roleBinding: ImplBinding, roleDescriptor: RoleDescriptor): RoleBinding = {
val runtimeClass = roleBinding.key.tpe.cls
val runtimeClass = roleBinding.key.tpe.closestClass
val implType = roleBinding.implementation.implType
RoleBinding(roleBinding, runtimeClass, implType, roleDescriptor)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ object RoleProvider {
}

protected def reflectCompanionDescriptor(role: SafeType): Option[RoleDescriptor] = {
val roleClassName = role.cls.getName
val roleClassName = role.closestClass.getName
try {
Some(TypeUtil.instantiateObject[RoleDescriptor](Class.forName(s"$roleClassName$$")))
} catch {
Expand Down
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object V {
// izumi
val izumi_reflect = "2.3.6"
val izumi_reflect = "2.3.7"

// foundation
val collection_compat = "2.10.0"
Expand Down