From 6c243fba5c8982f3cf355f5c53226ab69e9f50be Mon Sep 17 00:00:00 2001 From: Kai <450507+neko-kai@users.noreply.github.com> Date: Tue, 30 May 2023 01:11:01 +0100 Subject: [PATCH] Fix #391, fix regression with variance on Scala 3.3.0. Use fixes from https://github.com/lampepfl/dotty/pull/17568 --- .../reflect/dottyreflection/Inspector.scala | 7 +++++-- .../dottyreflection/ReflectionUtil.scala | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/Inspector.scala b/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/Inspector.scala index 37b86d53..d100adac 100644 --- a/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/Inspector.scala +++ b/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/Inspector.scala @@ -74,13 +74,16 @@ abstract class Inspector(protected val shift: Int, val context: Queue[Inspector. case Nil => makeNameReferenceFromType(appliedType.tycon) case _ => - val symbolVariances = appliedType.tycon.typeSymbol.typeMembers.map(extractVariance) + val symbolVariances = appliedType.tycon.typeSymbol.typeMembers.collect { + case s if s.isTypeParam => + extractVariance(s) + } val variances = if (symbolVariances.sizeCompare(appliedType.args) < 0) { appliedType.tycon match { case typeParamRef: ParamRef => typeParamRef._underlying match { case TypeBounds(_, hi) => - hi._declaredVariancesIfHKTypeLambda.fold(Nil)(_.map(extractVariance)) + hi._paramVariancesIfHKTypeLambda.fold(Nil)(_.map(extractVariance)) case _ => Nil } diff --git a/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/ReflectionUtil.scala b/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/ReflectionUtil.scala index 1ab34d77..c8ff6671 100644 --- a/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/ReflectionUtil.scala +++ b/izumi-reflect/izumi-reflect/src/main/scala-3/izumi/reflect/dottyreflection/ReflectionUtil.scala @@ -84,6 +84,7 @@ private[dottyreflection] trait ReflectionUtil { this: InspectorBase => } import ReflectionUtil.reflectiveUncheckedNonOverloadedSelectable + import InternalContext.InternalContext extension (typeRef: TypeRef | ParamRef) { protected final def _underlying: TypeRepr = { @@ -102,14 +103,16 @@ private[dottyreflection] trait ReflectionUtil { this: InspectorBase => // ) // underlying.asInstanceOf[TypeRepr] - typeRef.asInstanceOf[InternalTypeRefOrParamRef].underlying(using qctx._ctx) + typeRef.asInstanceOf[InternalTypeRefOrParamRef].underlying(qctx._ctx) } } extension (typeRepr: TypeRepr) { - protected final def _declaredVariancesIfHKTypeLambda: Option[List[Flags]] = { + protected final def _paramVariancesIfHKTypeLambda: Option[List[Flags]] = { try { - Some(typeRepr.asInstanceOf[InternalHKTypeLambda].declaredVariances) + val params = typeRepr.asInstanceOf[InternalHKTypeLambda].typeParams + val flags = params.map(_.paramVariance(qctx._ctx)) + Some(flags) } catch { case _: NoSuchMethodException => None } @@ -208,14 +211,20 @@ private[dottyreflection] trait ReflectionUtil { this: InspectorBase => } type InternalTypeRefOrParamRef = { - def underlying(using InternalContext): TypeRepr + def underlying(ctx: InternalContext): TypeRepr } type InternalHKTypeLambda = { - def declaredVariances: List[Flags] + val typeParams: List[InternalLambdaParam] } - opaque type InternalContext = Any + type InternalLambdaParam = { + def paramVariance(ctx: InternalContext): Flags + } + + object InternalContext { + opaque type InternalContext = Any + } }