Commit Graph

51 Commits

Author SHA1 Message Date
Sergey.Shanshin 84ad12be57 [KxSerialization] Added inspections on custom serializer parameters
Added inspections to check:
- custom serializer on class has as many parameters in primary constructor as the serializable class of type arguments
- all parameters in custom serializer has `KSerializer` type
- property in serializable class not parametrized by type parameter
- custom serializer on property of serializable class have no parameters in primary constructor
2024-02-12 15:54:11 +00:00
Sergey.Shanshin 96d7dc4fa6 [KxSerialization] Fixed access private custom serializer on property
When a custom serializer is specified on a type and this type is used in a property of another serializable class, then on the JVM this leads to an error accessing the custom serializer class - because it is private and located in another package.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2495


Merge-request: KT-MR-12877
Merged-by: Sergei Shanshin <Sergey.Shanshin@jetbrains.com>
2024-01-16 20:04:17 +00:00
Leonid Startsev fba2f5ea4e Expand most kotlinx.serialization tests on JS backend
to enhance and increase test coverage of the plugin.
2024-01-10 12:17:34 +00:00
Dmitriy Novozhilov c2cbbecfe9 [K2 Serialization] Store serializable properties in metadata extension
This solution is temporary (see KT-64694 for details)

^KT-64312 Fixed
2024-01-05 11:30:57 +00:00
Leonid Startsev 6c6f2ccacd Handle non-reified type parameters of function inside serializer<T>() intrinsic
to match an error message thrown from KType-based serializer<T>().

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2528
2023-12-20 13:50:39 +00:00
Dmitriy Novozhilov 139e1223ea [Serialization] Workaround registering static writeSelf method in metadata
`IrGeneratedDeclarationsRegistrar` assumes that all generated functions
  are correct from a Kotlin point of view. But `writeSelf` method on JVM
  is a static method outside any object/companion object

So to properly calculate containing class for this method we should
  generate a dispatch receiver parameter, register the method in metadata,
  and then remove the parameter (to make function static)
2023-12-05 10:21:40 +02:00
Leonid Startsev 662bff7351 Make $serializer generic constructor public
If a serializable class has generic type parameters, its serializer is not an object
and has a specialized constructor. This constructor was public in K1 and should
be public in K2 so it can be called from other serializable classes
(in case class is e.g., part of sealed hierarchy).

#KT-63402 Fixed
2023-11-28 13:31:08 +00:00
Sergey.Shanshin 6417f6456f [KxSerialization] Implemented addition of annotations to the named companion
For a more efficient lookup of a companion with the functions of obtaining a serializer, a special annotation is added to the named companions.


Merge-request: KT-MR-11003
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-07-28 15:53:09 +00:00
Sergey.Shanshin a45fe2d8a8 [KxSerialization] Implemented generation of companions for interfaces
For interfaces with custom serializer (@Serializer(`SerializerType::class)`) now the `serializer()` function is generated in the companion so that user can get a serializer by type

Merge-request: KT-MR-11040
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-07-17 19:44:01 +00:00
Leonid Startsev f3833fdcf8 Add serializer(vararg KSerializer<*>) override from SerializerFactory when necessary
SerializerFactory is an implementation detail for Kotlin/JS and Native:
it should be added as a supertype to a companion object of certain serializable classes
and `serializer(vararg KSerializer<*>)` function from it should be implemented.
Existing implementation added the supertype, but did not add proper override to FirClass
of a companion, which led to various warnings and errors like 'Abstract function 'serializer' is not implemented in non-abstract companion object'.

Also implemented the addition of SerializerFactory supertype to user-defined companions within @Serializable and @MetaSerializable when necessary.

Also set up proper box tests for FIR+Kotlin/JS combination.

#KT-58501 Fixed
#KT-59768 Fixed
2023-07-06 11:38:51 +00:00
Mikhail Glukhikh 8e882ea797 K2 plugins: create companions with same expect/actual as their owners
#KT-59299 Fixed
2023-06-16 17:55:45 +00:00
Sergey.Shanshin 441b22c0fd [KxSerialization] Fixed error accessing the descriptor from companion
Relates #KT-57647

If a serialization descriptor is used in the companion, then when accessing the child elements, an array of cached child serializers may be read.
Since the serialization plugin adds its declarations to the class after the declarations from the source code, the initialization of the array of child serializers occurs after the execution of the user code.
This can lead to N PE errors when trying to access an unfilled cached child serializer.

The solution is to change the order of declarations so that the declaration with the property of cached child serializers comes first.

Merge-request: KT-MR-10545
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-06-09 15:32:28 +00:00
OliverO2 027593cd78 [KxSerialization] Fix NPE when accessing delegated property
Kotlin 1.7.20 added optimizations for delegated properties on the JVM,
which broke serialization for optimized properties. Commit bfeff81
tried to fix that, but broke non-optimized delegated properties. This
commit restores correct serialization for optimized and non-optimized
properties, also ensuring that it only affects the JVM target.

 #KT-58954 Fixed
 #KT-59113 Fixed
2023-06-06 17:17:39 +02:00
Sergey.Shanshin 13e2a3ae94 [KxSerialization] Fix NPE if sealed class has self-referencing in generic parameter
Fixes #KT-58918

If the serializable class contains itself as a generic parameter of the property, and its serializer is not an object (for example, for a sealed class), in this case there is a race in the initialization of the serializer and child serializers.

Merge-request: KT-MR-10363
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-06-01 12:01:04 +00:00
Dmitriy Novozhilov d579798169 [Serialization] Ensure that KT-57770 is fixed
^KT-57770 Fixed
2023-05-10 07:30:29 +00:00
Sergey.Shanshin c9bde57e44 [KxSerialization] Fix NPE if sealed class has self-referencing property
Fixes Kotlin/kotlinx.serialization#2294

If the serializable class contains itself as a property, and its serializer is not an object (for example, for a sealed class), in this case there is a race in the initialization of the serializer and child serializers.

To avoid this the serialization class should not cache its own serializer as a child.

Merge-request: KT-MR-10019
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-05-09 09:59:11 +00:00
AngryGami bfeff81867 Presence of (transient) delegated field in the serialized class breaks deserialization (#5103)
Do not include delegated field into generated constructor even though it
might have backing field.

Test that shows issue was added. Properties that are explicitly marked
as delegated are now excluded.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2091
2023-04-26 12:28:57 +02:00
Leonid Startsev ef9074e24d Correctly support nullability in type arguments for serializer<T>() intrinsic.
Nullability info should be added to TYPE_OF operation marker.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2265
2023-04-19 12:31:19 +00:00
Leonid Startsev fa8f38c2c8 Don't fail if there is no serializer for type parameters of contextual serializer
Return null instead.
Such behaviour is needed to support cachedChildSerializers logic.
Since this field creator doesn't provide genericGetter (because it's
static),
type param serializer can't be retrieved, and the whole contextual
serializer shouldn't be cached.

#KT-58067 Fixed
2023-04-19 10:09:06 +00:00
Leonid Startsev d9e16fb76e Do not create cacheableChildSerializers unless it is necessary
Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2260
2023-04-18 11:04:56 +00:00
Dmitriy Novozhilov fb97f4db87 [FIR] Don't create generated nested classifier scope for non-source classes
^KT-57626 Fixed
2023-03-29 15:18:37 +00:00
Leonid Startsev 6ee20574e1 Correctly handle @Repeatable @SerialInfo annotations on classes
that were affected by deduplication of inherited serial info annotations.

Prohibit combination of @InheritableSerialInfo and @Repeatable.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2099
2023-03-20 18:15:09 +00:00
Artem Kobzar 50a462fed1 [K/JS] Exclude kotlinx.serialization synthetic declarations from JsExport
^KT-57024 Fixed
2023-02-28 18:11:36 +00:00
Leonid Startsev c564dd973b Add declarations from serialization plugin to FIR metadata declarations provider.
These declarations should not be visible to users (and therefore are not added to FIR),
but plugin itself can reference them in already compiled serializable classes,
and therefore they should be available in metadata:

- synthetic deserialization constructor
- static write$Self function

See also:
^KT-55885
2023-02-16 15:36:18 +00:00
Leonid Startsev 80ad6a4cd7 Support plugin intrinsics for serializer() function in K2.
- Add IrPluginContext to JvmBackendContext, so plugin intrinsics can
reference external functions properly.

- Do not use module.findClassAcrossModuleDependencies as Descriptor API does not work for FIR.

- Add asm listing tests in serialization plugin for K2

- Remove Delegated.kt asm listing test as we have similar test in boxIr group.

#KT-56553 Fixed
2023-02-14 13:43:09 +00:00
Leonid Startsev e0dce31cde Support situation when argument for serializer<T>() function has SerialInfo annotations.
Because SerializationJvmIrIntrinsicSupport does not instantiate annotations yet,
this info could be lost. As a workaround, it is possible to call Companion.serializer()
functions instead of direct serializer instantiation, as they are plugin-generated
and correctly handle annotations.

Note that for some cases (enums & interfaces) this WA is not enough, so additional work
is needed later.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2179
2023-02-07 16:41:01 +00:00
Leonid Startsev dc0cd61b6f Handle @Serializable classes that implement interfaces by delegation
by filtering out delegated properties that end up in irClass.properties() list

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2157
2023-01-19 11:12:31 +00:00
Sergey.Shanshin d063db3ce0 Fixed missing fallback serializer in contextual serializer
Fixes Kotlin/kotlinx.serialization#2158

Merge-request: KT-MR-8338
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-01-18 15:47:26 +00:00
Ivan Kochurkin 31e2f0ba95 [FIR][Serialization] Set containingClassForStaticMemberAttr in generateConstructors 2023-01-13 12:55:58 +00:00
Ivan Kochurkin 978f937fa9 [FIR Serialization] Fix serialization of generated nested classifiers
^KT-55759 Fixed
2023-01-11 16:07:02 +01:00
Leonid Startsev 2634303055 Fix compilation error when @Serializable expect class has explicit companion
as it may have no primary constructor (only actual class has it).

Refactor & rename Fir2IrGeneratorUtils.kt as having 'Fir' in stacktrace
when actual K2 compiler is not used is very confusing.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2134
#KT-55683 Fixed
2023-01-11 13:00:15 +00:00
Leonid Startsev bf60819824 Check for name when searching Companion.serializer() function to generate
Name check was forgotten during refactoring, so any user function with
matching signature could be overwritten by the plugin, while correct
function would not have body.

#KT-55682 Fixed
2023-01-09 18:43:10 +00:00
Leonid Startsev 0b4f534d35 Skip properties from Java classes for which the getter type is unknown
thus avoiding NPE when auto-generating external serializers.

#KT-55681 Fixed
2023-01-09 18:43:10 +00:00
Leonid Startsev a1894ed027 Update runtime dependency in kotlinx.serialization plugin tests to 1.4.1 2023-01-09 18:25:42 +00:00
Dmitriy Novozhilov ce2880614b [FIR2IR] Run constant evaluation before IR extensions
Test `constValInSerialName` in serialization still fails but because of
  different reason

^KT-54994 Fixed
2022-12-21 11:09:30 +00:00
Evgeniy.Zhelenskiy f6c63c6e4f [IR, Serialization] Support kotlinx-based (de)serialization of MFVC, nullable MFVC value assignment to nonnull variable
Signed-off-by: Evgeniy.Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com>

#KT-1179
2022-12-16 17:20:51 +01:00
Leonid Startsev 1c4614e93b Handle situation where KSerializer is absent from immediate supertypes
of the class used in @UseSerializers: Use methods to receive
full list of supertypes.

K1 supertypes() call returned all supertypes, while
IrClass.supertypes and FirClassSymbol.resolvedSuperTypes return only immediate ones.

This lead to a difference in behavior between K1 and K2, and regression
after plugin backend was rewritten from descriptors to IR.

#KT-55340 Fixed
2022-12-08 12:15:16 +00:00
Leonid Startsev 9586bf74e0 Use .fullyExpandedType to get serializable annotation from inside typealias
to correctly handle 'global customization via typealias' feature
in K2 serialization plugin.
2022-11-22 10:53:28 +00:00
Leonid Startsev 1b9fdeadfe Do not enable serialization plugin intrinsics in K2
because they lead to an unbound symbol errors
2022-11-21 16:15:46 +00:00
Leonid Startsev 715a730819 Insert a runtime exception if star-projection type is encountered in serializer<T>() intrinsic
This makes behavior consistent with non-intrinsified version of this function and serializer(KType).

#KT-54878 Fixed
2022-11-21 16:15:46 +00:00
Ilya Chernikov 5b3816cce5 Test infra: refactor IGNORE_BACKEND directive
treat it as a general one, introduce *_K1 and *_K2 variants for
more specific ignoring
2022-11-12 16:28:23 +01:00
Leonid Startsev f1b1837f40 Rework SerialInfo$Impl annotation implementation to be available in FIR:
- IR plugin does not use it anymore,
regular annotation instantiation feature is used
(insertion of IrConstructorCall(annotationCtorSymbol)).

- IR plugin still generates `SerialInfo$Impl` class to be compatible
with previously compiled code & libraries.

- Custom implementation over descriptors in IR plugin is removed;
plugin now delegates to the existing lowering with some tweaks.

- $Impl descriptor is removed from FE 1.0 because it is no longer needed
for IR plugin; it shouldn't be exposed to users so FIR doesn't need it either.

- Since language lowering is now used, it is possible to correctly instantiate
SerialInfo annotations with default values even if they are from other modules
and even if they were not processed by the plugin.

#KT-48733 Fixed
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1574
2022-10-17 16:10:42 +00:00
Leonid Startsev 242b83edbd Prioritize @Serializable annotation on type usage over class usage
In `val p: @Serializable(X) Foo` and `@Serializable(Y) class Foo` situation,
now X would be chosen as serializer.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1895
2022-10-14 14:15:37 +00:00
Sergey.Shanshin 503f4d924d Added support of MetaSerializable to the FIR 2022-10-12 20:10:58 +00:00
Leonid Startsev 596949a501 Correctly handle star projections according to logic of the old FE.
#KT-54297 Fixed

Properly substitute surrogate UnitSerializer in the Companion.serializer()
function generated on classes that use polymorphic or sealed serializer
by default. (Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1692)
2022-10-12 10:36:26 +00:00
Leonid Startsev 2a626b27d3 Correctly determine the type of serializable property
when supertype of serializable class is generic and also serializable,
and contains the property with type with its generic parameter.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1264
#KT-43910 Fixed
#KT-49660 Fixed
2022-10-12 10:34:53 +00:00
Leonid Startsev a20ce06102 Support @Serializable(ContextualSerializer) on class
It was fixed some time ago (https://github.com/Kotlin/kotlinx.serialization/issues/957)
but for some reason only for old backend.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1776
2022-09-28 12:40:20 +00:00
Leonid Startsev aac9a06474 Also support intrinsification for SerializersModule.serializer<T>()
method that fallbacks to module.getContextual().
2022-09-16 14:34:28 +00:00
Leonid Startsev a59f5f407b Introduce support for plugin-defined intrinsics in JVM IR backend:
Add intrinsic for kotlinx.serialization.serializer<T>() function.

Plugin intrinsic for old backend is removed because it is too hard
and unjustifiable to unify them.
2022-09-16 14:34:28 +00:00
Dmitriy Novozhilov a2ffba275c [Serialization] Migrate all tests to new test infrastructure 2022-09-05 10:20:05 +00:00