Skip to content

Commit

Permalink
Fix issue by using widen.dealias
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSpanel committed Sep 1, 2022
1 parent 0bf2179 commit 0a1f8c5
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ object QuicklensMacros {
}

def termMethodByNameUnsafe(term: Term, name: String): Symbol = {
term.tpe.typeSymbol
term.tpe.widen.dealias.typeSymbol
.memberMethod(name)
.headOption
.getOrElse(report.errorAndAbort(noSuchMember(term, name)))
}

def termAccessorMethodByNameUnsafe(term: Term, name: String): (Symbol, Int) = {
val caseParamNames = term.tpe.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val caseParamNames = term.tpe.widen.dealias.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val idx = caseParamNames.indexOf(name)
term.tpe.typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term, name)))
term.tpe.widen.dealias.typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term, name)))
-> (idx + 1)
}

Expand All @@ -160,7 +160,7 @@ object QuicklensMacros {
obj: Term,
fields: Seq[(PathSymbol.Field, Seq[PathTree])]
): Term = {
val objSymbol = obj.tpe.typeSymbol
val objSymbol = obj.tpe.widen.dealias.typeSymbol
if objSymbol.flags.is(Flags.Case) || // necessary - some enums actually need case class-like handling
!(objSymbol.flags.is(Flags.Enum) ||
(objSymbol.flags.is(Flags.Sealed) && (objSymbol.flags.is(Flags.Trait) || objSymbol.flags.is(Flags.Abstract))))
Expand All @@ -175,15 +175,15 @@ object QuicklensMacros {
idx -> namedArg
}.toMap

val fieldsIdxs = 1.to(obj.tpe.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).length)
val fieldsIdxs = 1.to(objSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).length)
val args = fieldsIdxs.map { i =>
argsMap.getOrElse(
i,
Select(obj, termMethodByNameUnsafe(obj, "copy$default$" + i.toString))
)
}.toList

obj.tpe.widen match {
obj.tpe.widen.dealias match {
// if the object's type is parametrised, we need to call .copy with the same type parameters
case AppliedType(_, typeParams) => Apply(TypeApply(Select(obj, copy), typeParams.map(Inferred(_))), args)
case _ => Apply(Select(obj, copy), args)
Expand Down

0 comments on commit 0a1f8c5

Please sign in to comment.