@@ -879,7 +879,7 @@ object JsonCodecMaker {
879
879
case ' [t] =>
880
880
val sym = symbol(" ct" + classTags.size, TypeRepr .of[ClassTag [t]])
881
881
val ct = Expr .summon[ClassTag [t]].fold(fail(s " Can't summon ClassTag[ ${tpe.show}] " ))(_.asTerm)
882
- ValDef (sym, Some (ct.changeOwner(sym) ))
882
+ ValDef (sym, Some (ct))
883
883
}).symbol)
884
884
885
885
def inferImplicitValue [T : Type ](typeToSearch : TypeRepr ): Option [Expr [T ]] = Implicits .search(typeToSearch) match
@@ -928,15 +928,15 @@ object JsonCodecMaker {
928
928
else if (precision == MathContext .UNLIMITED .getPrecision) ' { (MathContext .UNLIMITED : java.math.MathContext ) }
929
929
else Ref (mathContexts.getOrElseUpdate(precision, {
930
930
val sym = symbol(" mc" + mathContexts.size, TypeRepr .of[MathContext ])
931
- ValDef (sym, Some (' { new MathContext ($ {Expr (cfg.bigDecimalPrecision)}, java.math.RoundingMode .HALF_EVEN ) }.asTerm.changeOwner(sym) ))
931
+ ValDef (sym, Some (' { new MathContext ($ {Expr (cfg.bigDecimalPrecision)}, java.math.RoundingMode .HALF_EVEN ) }.asTerm))
932
932
}).symbol).asExprOf[MathContext ]
933
933
934
934
val scalaEnumCaches = new mutable.LinkedHashMap [TypeRepr , ValDef ]
935
935
936
936
def withScalaEnumCacheFor [K : Type , T : Type ](tpe : TypeRepr )(using Quotes ): Expr [ConcurrentHashMap [K , T ]] =
937
937
Ref (scalaEnumCaches.getOrElseUpdate(tpe, {
938
938
val sym = symbol(" ec" + scalaEnumCaches.size, TypeRepr .of[ConcurrentHashMap [K , T ]])
939
- ValDef (sym, Some (' { new ConcurrentHashMap [K , T ] }.asTerm.changeOwner(sym) ))
939
+ ValDef (sym, Some (' { new ConcurrentHashMap [K , T ] }.asTerm))
940
940
}).symbol).asExprOf[ConcurrentHashMap [K , T ]]
941
941
942
942
case class JavaEnumValueInfo (value : Symbol , name : String , transformed : Boolean )
@@ -996,7 +996,10 @@ object JsonCodecMaker {
996
996
def genNew (argss : List [List [Term ]]): Term = {
997
997
val args = argss.flatten
998
998
if (isGeneric) Expr .ofTupleFromSeq(args.map(_.asExpr)).asTerm
999
- else Apply (TypeApply (Select .unique(New (Inferred (tupleTpe)), " <init>" ), typeArgs.map(x => Inferred (x))), args)
999
+ else {
1000
+ val constructorNoTypes = Select (New (Inferred (tupleTpe)), tupleTpe.typeSymbol.primaryConstructor)
1001
+ Apply (TypeApply (constructorNoTypes, typeArgs.map(Inferred (_))), args)
1002
+ }
1000
1003
}
1001
1004
}
1002
1005
@@ -1644,10 +1647,7 @@ object JsonCodecMaker {
1644
1647
val nullValues = new mutable.LinkedHashMap [TypeRepr , ValDef ]
1645
1648
1646
1649
def withNullValueFor [T : Type ](tpe : TypeRepr )(f : => Expr [T ]): Expr [T ] =
1647
- Ref (nullValues.getOrElseUpdate(tpe, {
1648
- val sym = symbol(" c" + nullValues.size, tpe)
1649
- ValDef (sym, Some (f.asTerm.changeOwner(sym)))
1650
- }).symbol).asExprOf[T ]
1650
+ Ref (nullValues.getOrElseUpdate(tpe, ValDef (symbol(" c" + nullValues.size, tpe), Some (f.asTerm))).symbol).asExprOf[T ]
1651
1651
1652
1652
val fieldIndexAccessors = new mutable.LinkedHashMap [TypeRepr , DefDef ]
1653
1653
@@ -1663,7 +1663,7 @@ object JsonCodecMaker {
1663
1663
i += 1
1664
1664
CaseDef (Literal (IntConstant (i)), None , Literal (StringConstant (n)))
1665
1665
}
1666
- Some (Match (param.asExprOf[Int ].asTerm, cases).changeOwner(sym) )
1666
+ Some (Match (param.asExprOf[Int ].asTerm, cases))
1667
1667
})
1668
1668
}).symbol)
1669
1669
@@ -1898,10 +1898,7 @@ object JsonCodecMaker {
1898
1898
def genReadLeafClass [T : Type ](subTpe : TypeRepr )(using Quotes ): Expr [T ] =
1899
1899
val useDiscriminator = cfg.discriminatorFieldName.isDefined
1900
1900
if (subTpe =:= tpe) {
1901
- val typeInfo =
1902
- if (isNamedTuple(tpe)) getNamedTupleInfo(tpe)
1903
- else getClassInfo(tpe)
1904
- genReadNonAbstractScalaClass(typeInfo, types, useDiscriminator, in, genNullValue[T ](types))
1901
+ genReadNonAbstractScalaClass(getClassInfo(tpe), types, useDiscriminator, in, genNullValue[T ](types))
1905
1902
} else genReadVal(subTpe :: types, genNullValue[T ](subTpe :: types), isStringified, useDiscriminator, in)
1906
1903
1907
1904
def genReadCollisions [T : Type ](subTpes : collection.Seq [TypeRepr ], l : Expr [Int ])(using Quotes ): Expr [T ] =
@@ -2122,7 +2119,7 @@ object JsonCodecMaker {
2122
2119
cfg.discriminatorFieldName.map { fieldName =>
2123
2120
if (cfg.checkFieldDuplication) {
2124
2121
val sym = symbol(" pd" , TypeRepr .of[Boolean ], Flags .Mutable )
2125
- ReadDiscriminator (Some (ValDef (sym, Some (Literal (BooleanConstant (true )).changeOwner(sym) ))))
2122
+ ReadDiscriminator (Some (ValDef (sym, Some (Literal (BooleanConstant (true ))))))
2126
2123
} else ReadDiscriminator (None )
2127
2124
}
2128
2125
} else None
@@ -2142,7 +2139,6 @@ object JsonCodecMaker {
2142
2139
2143
2140
def blockWithVars (next : Term ): Term =
2144
2141
Block (readVars ++ paramVars.toList ++ optDiscriminatorVar.toList, next.changeOwner(Symbol .spliceOwner))
2145
- .changeOwner(Symbol .spliceOwner) // All owners should be from top Symbol.spliceOwner because vals are created with this owner
2146
2142
2147
2143
val readNonEmpty = blockWithVars(' {
2148
2144
if (! $in.isNextToken('}' )) {
@@ -2154,10 +2150,10 @@ object JsonCodecMaker {
2154
2150
}
2155
2151
if (! $in.isCurrentToken('}' )) $in.objectEndOrCommaError()
2156
2152
}
2157
- $ {Block (checkReqVars, construct).changeOwner( Symbol .spliceOwner). asExprOf[T ]}
2153
+ $ {Block (checkReqVars, construct).asExprOf[T ]}
2158
2154
}.asTerm)
2159
- If (' { $in.isNextToken('{' ) }.asTerm.changeOwner( Symbol .spliceOwner) , readNonEmpty,
2160
- ' { $in.readNullOrTokenError($default, '{' ) }.asTerm.changeOwner( Symbol .spliceOwner) ).asExprOf[T ]
2155
+ If (' { $in.isNextToken('{' ) }.asTerm, readNonEmpty,
2156
+ ' { $in.readNullOrTokenError($default, '{' ) }.asTerm).asExprOf[T ]
2161
2157
}
2162
2158
2163
2159
def genReadConstType [T : Type ](tpe : TypeRepr , isStringified : Boolean , in : Expr [JsonReader ])(using Quotes ): Expr [T ] = tpe match
@@ -2694,7 +2690,6 @@ object JsonCodecMaker {
2694
2690
i += 1
2695
2691
te.asType match
2696
2692
case ' [t] =>
2697
- val sym = symbol(" _r" + i, te)
2698
2693
val nullVal = genNullValue[t](te :: types)
2699
2694
val rhs =
2700
2695
if (i == 1 ) genReadVal(te :: types, nullVal, isStringified, false , in)
@@ -2703,19 +2698,19 @@ object JsonCodecMaker {
2703
2698
$ {genReadVal(te :: types, nullVal, isStringified, false , in)}
2704
2699
} else $in.commaError()
2705
2700
}
2706
- ValDef (sym, Some (rhs.asTerm.changeOwner(sym) ))
2701
+ ValDef (symbol( " _r " + i, te), Some (rhs.asTerm))
2707
2702
}
2708
2703
val readCreateBlock = Block (valDefs, ' {
2709
2704
if ($in.isNextToken(']' )) $ {
2710
2705
val size = indexedTypes.size
2711
2706
if (size == 0 ) Expr (EmptyTuple )
2712
2707
else if (size > 22 ) Expr .ofTupleFromSeq(valDefs.map(x => Ref (x.symbol).asExprOf[Any ]))
2713
2708
else {
2714
- Apply ( TypeApply ( Select .unique (New (Inferred (tTpe)), " <init> " ),
2715
- indexedTypes.map(x => Inferred (x ))), valDefs.map(x => Ref (x.symbol))).asExpr
2709
+ val constructorNoTypes = Select (New (Inferred (tTpe)), tTpe.typeSymbol.primaryConstructor)
2710
+ Apply ( TypeApply (constructorNoTypes, indexedTypes.map(Inferred (_ ))), valDefs.map(x => Ref (x.symbol))).asExpr
2716
2711
}
2717
2712
} else $in.arrayEndError()
2718
- }.asTerm)
2713
+ }.asTerm.changeOwner( Symbol .spliceOwner) )
2719
2714
' {
2720
2715
if ($in.isNextToken('[' )) $ {readCreateBlock.asExprOf[T ]}
2721
2716
else $in.readNullOrTokenError($default, '[' )
@@ -2900,8 +2895,8 @@ object JsonCodecMaker {
2900
2895
}
2901
2896
val allWriteFields = optDiscriminator.fold(writeFields)(_.write(out) :: writeFields)
2902
2897
Block (valDefs,
2903
- Block (' { $out.writeObjectStart() }.asTerm :: allWriteFields.map(_.asTerm.changeOwner( Symbol .spliceOwner) ),
2904
- ' { $out.writeObjectEnd() }.asTerm)
2898
+ Block (' { $out.writeObjectStart() }.asTerm :: allWriteFields.map(_.asTerm),
2899
+ ' { $out.writeObjectEnd() }.asTerm).changeOwner( Symbol .spliceOwner)
2905
2900
).asExprOf[Unit ]
2906
2901
2907
2902
def getWriteConstType (tpe : TypeRepr , isStringified : Boolean , out : Expr [JsonWriter ])(using Quotes ): Expr [Unit ] =
@@ -3203,12 +3198,12 @@ object JsonCodecMaker {
3203
3198
i += 1
3204
3199
te.asType match
3205
3200
case ' [t] =>
3206
- val select =
3201
+ val getter =
3207
3202
if (size > 22 ) {
3208
- val getter = Select .unique(xTerm, " productElement" ).appliedTo(Literal (IntConstant (i - 1 ))).asExprOf[Any ]
3209
- ' { $getter .asInstanceOf [t] }.asExprOf[t]
3203
+ val select = Select .unique(xTerm, " productElement" ).appliedTo(Literal (IntConstant (i - 1 ))).asExprOf[Any ]
3204
+ ' { $select .asInstanceOf [t] }.asExprOf[t]
3210
3205
} else Select .unique(xTerm, " _" + i).asExprOf[t]
3211
- genWriteVal(select , te :: types, isStringified, None , out).asTerm
3206
+ genWriteVal(getter , te :: types, isStringified, None , out).asTerm
3212
3207
}
3213
3208
Block (' { $out.writeArrayStart() }.asTerm :: writeFields, ' { $out.writeArrayEnd() }.asTerm).asExprOf[Unit ]
3214
3209
} else if (isEnumOrModuleValue(tpe) && ! (cfg.alwaysEmitDiscriminator && hasSealedParent(tpe))) withEncoderFor(methodKey, m, out) { (out, x) =>
@@ -3222,10 +3217,7 @@ object JsonCodecMaker {
3222
3217
subTpe.asType match
3223
3218
case ' [st] =>
3224
3219
if (subTpe =:= tpe) {
3225
- val typeInfo =
3226
- if (isNamedTuple(tpe)) getNamedTupleInfo(tpe)
3227
- else getClassInfo(tpe)
3228
- genWriteNonAbstractScalaClass(vx.asExprOf[st], typeInfo, types, discriminator, out)
3220
+ genWriteNonAbstractScalaClass(vx.asExprOf[st], getClassInfo(tpe), types, discriminator, out)
3229
3221
} else genWriteVal(vx.asExprOf[st], subTpe :: types, isStringified, discriminator, out)
3230
3222
3231
3223
val leafClasses = adtLeafClasses(tpe)
0 commit comments