Skip to content

Commit e3cf77f

Browse files
rsshplokhotnyuk
authored andcommitted
workarround against scala/scala3#14137
1 parent 37fb0a0 commit e3cf77f

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ object JsonCodecMaker {
16551655
discriminator: Option[ReadDiscriminator],
16561656
in: Expr[JsonReader],
16571657
default: Expr[A],
1658-
): Expr[A] = {
1658+
)(using Quotes): Expr[A] = {
16591659
val tpe = types.head
16601660
val classInfo = getClassInfo(tpe)
16611661
checkFieldNameCollisions(tpe, cfg.discriminatorFieldName.fold(Seq.empty[String]) { n =>
@@ -1678,11 +1678,12 @@ object JsonCodecMaker {
16781678
val rhs = Literal(IntConstant(if (i == lastParamVarIndex) lastParamVarBits else -1))
16791679
ValDef(sym, Some(rhs))
16801680
}
1681-
val checkAndResetFieldPresenceFlags:Map[String, Expr[Int] => Expr[Unit]] = {
1681+
1682+
val checkAndResetFieldPresenceFlags: Map[String, Quotes ?=> Expr[Int] => Expr[Unit]] = {
16821683
classInfo.fields.zipWithIndex.map { case (f, i) =>
16831684
val n = Ref(paramVars(i >> 5).symbol).asExprOf[Int]
16841685
val m = Expr(1 << i)
1685-
(f.mappedName, (l:Expr[Int]) => '{
1686+
(f.mappedName, (q: Quotes) ?=> (l:Expr[Int]) => '{
16861687
if (($n & $m) != 0)
16871688
${Assign(n.asTerm , '{ $n ^ $m }.asTerm).asExprOf[Unit]}
16881689
else
@@ -1776,16 +1777,16 @@ object JsonCodecMaker {
17761777
val optDiscriminatorVar = discriminator.map{ _.valDef }
17771778

17781779

1779-
def blockWithVars(next: Term): Term =
1780+
def blockWithVars[A:Type](next: Expr[A])(using Quotes): Expr[A] =
17801781
Block(
17811782
readVars.toList ++
17821783
paramVars.toList ++
17831784
optDiscriminatorVar.toList,
1784-
next
1785-
)
1785+
next.asTerm
1786+
).asExprOf[A]
17861787

17871788
'{ if ($in.isNextToken('{')) {
1788-
${blockWithVars(
1789+
${blockWithVars[A](
17891790
'{if (!$in.isNextToken('}')) {
17901791
$in.rollbackToken()
17911792
var l = -1
@@ -1796,7 +1797,7 @@ object JsonCodecMaker {
17961797
if (!$in.isCurrentToken('}')) $in.objectEndOrCommaError()
17971798
}
17981799
${Block(checkReqVars.map(_.asTerm), construct).asExprOf[A]}
1799-
}.asTerm).asExprOf[A]
1800+
}).asExprOf[A]
18001801
}
18011802
} else $in.readNullOrTokenError($default, '{')
18021803
}
@@ -1878,7 +1879,7 @@ object JsonCodecMaker {
18781879
isStringified: Boolean,
18791880
optDiscriminator: Option[ReadDiscriminator],
18801881
in: Expr[JsonReader]
1881-
): Expr[C] = {
1882+
)(using Quotes): Expr[C] = {
18821883
val tpe = types.head
18831884
val implCodec = findImplicitValueCodec(types)
18841885
val methodKey = DecoderMethodKey(tpe, isStringified && (isCollection(tpe) || isOption(tpe)), optDiscriminator.isDefined)
@@ -2943,12 +2944,17 @@ object JsonCodecMaker {
29432944

29442945
val codec = rootTpe.asType match {
29452946
case '[rootType] =>
2946-
val codecExpr = '{
2947+
def codecExpr(using Quotes) = '{
29472948
new JsonValueCodec[rootType] {
29482949
def nullValue: rootType = ${genNullValue[rootType](rootTpe :: Nil)}
29492950

29502951
def decodeValue(in: JsonReader, default: rootType): rootType = {
2951-
${genReadVal(rootTpe :: Nil, 'default, cfg.isStringified, None, 'in)}
2952+
${genReadVal(
2953+
rootTpe :: Nil,
2954+
'default,
2955+
cfg.isStringified,
2956+
None,
2957+
'in)}
29522958
}
29532959

29542960
def encodeValue(x: rootType, out: JsonWriter): Unit = {
@@ -2957,6 +2963,7 @@ object JsonCodecMaker {
29572963

29582964
}
29592965
}
2966+
29602967
val needDefs: List[Statement] = (decodeMethodDefs.values.toList: List[Statement]) ++
29612968
(encodeMethodDefs.values.toList: List[Statement]) ++
29622969
(fieldIndexAccessors.values.toList: List[Statement]) ++

0 commit comments

Comments
 (0)