Skip to content

Commit a897bf3

Browse files
committed
Ref to def-def is impossible without eta-expansion
1 parent 2b7920b commit a897bf3

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

jsoniter-scala-macros/shared/src/main/scala-3/JsonCodeMaker.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,8 @@ object JsonCodecMaker {
14261426
val fieldIndexAccessors = mutable.LinkedHashMap.empty[TypeRepr, DefDef]
14271427

14281428

1429-
def withFieldsFor(tpe: TypeRepr)(f: => Seq[String]): Expr[Int => String] = {
1429+
def withFieldsByIndexFor(tpe: TypeRepr)(f: => Seq[String]): Term = {
1430+
// [Int => String], we don't want eta-expand without reason, so let this will be just index.
14301431
val defDef = fieldIndexAccessors.getOrElseUpdate(tpe, {
14311432
val name = "f" + fieldIndexAccessors.size
14321433
val mt = MethodType(List("i"))(_ => List(TypeRepr.of[Int]), _ => TypeRepr.of[String] )
@@ -1442,7 +1443,7 @@ object JsonCodecMaker {
14421443
Some(body)
14431444
})
14441445
})
1445-
Ref(defDef.symbol).asExprOf[Int => String]
1446+
Ref(defDef.symbol)// .asExprOf[Int => String]
14461447
}
14471448

14481449
val equalsMethods = mutable.LinkedHashMap.empty[TypeRepr, DefDef]
@@ -1693,7 +1694,7 @@ object JsonCodecMaker {
16931694
val checkReqVars =
16941695
if (required.isEmpty) Nil
16951696
else {
1696-
val names = withFieldsFor(tpe)(classInfo.fields.map(_.mappedName))
1697+
val nameByIndex = withFieldsByIndexFor(tpe)(classInfo.fields.map(_.mappedName))
16971698
val reqMasks = classInfo.fields.grouped(32).toSeq.map(_.zipWithIndex.foldLeft(0) { case (acc, (f, i)) =>
16981699
if (required(f.mappedName)) acc | (1 << i)
16991700
else acc
@@ -1702,9 +1703,11 @@ object JsonCodecMaker {
17021703
val n = Ref(nValDef.symbol).asExprOf[Int]
17031704
val m = Expr(reqMasks(i))
17041705
val fieldName =
1705-
if (i == 0) '{ $names(java.lang.Integer.numberOfTrailingZeros($n & $m)) }
1706-
else '{ $names(java.lang.Integer.numberOfTrailingZeros($n & $m) + ${Expr(i << 5)}) }
1707-
'{ if ( ($n & $m) != 0) $in.requiredFieldError($fieldName) }
1706+
if (i == 0)
1707+
Apply(nameByIndex,List( '{java.lang.Integer.numberOfTrailingZeros($n & $m) }.asTerm) )
1708+
else
1709+
Apply(nameByIndex,List( '{ java.lang.Integer.numberOfTrailingZeros($n & $m) + ${Expr(i << 5)} }.asTerm ))
1710+
'{ if ( ($n & $m) != 0) $in.requiredFieldError(${fieldName.asExprOf[String]}) }
17081711
}.toList
17091712
}
17101713
val readVars: Seq[ValDef] = classInfo.fields.map { f =>
@@ -1734,7 +1737,7 @@ object JsonCodecMaker {
17341737
else classInfo.fields :+ FieldInfo(Symbol.noSymbol, n, Symbol.noSymbol, None, TypeRepr.of[String], isStringified = true)
17351738
}
17361739

1737-
def genReadCollisions(fs: collection.Seq[FieldInfo], tmpVars: Map[String,ValDef], l:Expr[Int]): Expr[Unit] =
1740+
def genReadCollisions(fs: collection.Seq[FieldInfo], tmpVars: Map[String,ValDef], l:Expr[Int])(using Quotes): Expr[Unit] =
17381741
val s0: Expr[Unit] = unexpectedFieldHandler(in,l)
17391742
fs.foldRight(s0){ (f, acc) =>
17401743
val readValue: Expr[Unit] =
@@ -1755,7 +1758,8 @@ object JsonCodecMaker {
17551758
'{ if ($in.isCharBufEqualsTo($l, ${Expr(f.mappedName)})) $readValue else $acc }
17561759
}
17571760

1758-
def readFieldsBlock(l: Expr[Int]):Expr[Unit] =
1761+
def readFieldsBlock(l: Expr[Int])(using Quotes):Expr[Unit] =
1762+
// using Quotes for workarround agains https://github.com/lampepfl/dotty/issues/14137
17591763
if (readFields.size <= 8 && readFields.map(fiLength).sum <= 64)
17601764
genReadCollisions(readFields, readVarsMap, l)
17611765
else {

0 commit comments

Comments
 (0)