71 Commits

Author SHA1 Message Date
Leonid Startsev dbb7ab1760 Add @Deprecated annotation to a synthetic $serializer class
in K2 so it matches K1 behavior.

Deprecation is required, so the declaration will not be visible to user in completion in the IDE.

#KT-65757 Fixed
2024-03-19 10:57:27 +00:00
Leonid Startsev 7c8c65d291 Call a specified function in intrinsic if polymorphic serializer is provided for interface.
This prioritizes module contents over default polymorphic serializer.

See https://github.com/Kotlin/kotlinx.serialization/issues/2060 and https://github.com/Kotlin/kotlinx.serialization/pull/2565
2024-03-18 17:21:16 +00:00
Leonid Startsev 69ad68ca83 Make intrinsics more consistent with reflective serializer() lookup
Intrinsic should not look into use-site `@Serializable(with)` annotations.

#KT-64920 Fixed
2024-03-15 17:29:48 +00:00
Marco Pennekamp 708ed81eb2 [Test] Avoid importing unused @Nested annotations in generated tests
- Unused `Nested` imports frequently cause unused import warnings in the
  IDE, which are especially annoying in after-commit warning/error
  analysis.
2024-02-27 20:30:06 +00:00
Dmitriy Novozhilov acf2296590 [Test] Regenerate tests after two previous commits 2024-02-16 12:48:24 +00:00
Sergey.Shanshin 9a8ebd5c21 [KxSerialization] Cover all combinations of language and serialization features with tests for enums
Resolves https://github.com/Kotlin/kotlinx.serialization/issues/2324


Merge-request: KT-MR-13775
Merged-by: Sergei Shanshin <Sergey.Shanshin@jetbrains.com>
2024-02-14 13:15:01 +00:00
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
Dmitrii Gridin b6d373d8e5 Update copyright to 2024 2024-01-05 13:43:17 +00:00
Leonid Startsev 7700921646 Add K2 configurations for specific kotlinx.serialization tests
#KT-64447 Fixed
2024-01-05 11:38:35 +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
Leonid Startsev 51e3e9af94 Use a correct type for SERIALIZER_TYPE_INCOMPATIBLE diagnostic.
This diagnostic used an incorrect type for rendering (outer class type instead of a property type)
and didn't expand type argument of `KSerializer`.

Also add an additional test case for generic parameters.

#KT-63570 Fixed
2023-11-28 13:30:45 +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
Sergey.Shanshin 3629a9db30 [KxSerialization] Added inspection on abstract custom serializer
Serialization requires an instance of the serializer, which cannot be obtained with the passed interface, abstract or sealed class.
Therefore, the specifying of such classes in `Serializable` annotation must be prohibited.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2173
Relates #KT-58036

Merge-request: KT-MR-10753
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-07-04 18:31:35 +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
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
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
Sergey.Shanshin d8d643b7d7 [KxSerialization] Fix "IllegalAccessError: Update to static final field"
Fixes #KT-57647

For value classes, if you add code to companion anonymous init block in IR, it will be executed in the instance constructor. This causes an error when initializing static fields, because writing to them can only occur from the <clinit> method.

The solution is to transfer the static field initialization code from an anonymous init block to the IR initializer of this field

Merge-request: KT-MR-9633
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-04-21 12:37:32 +00: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
Dmitriy Novozhilov 507ef3e951 [FIR] Properly handle declarations from other source sessions in FirPredicateBasedProvider
^KT-57083 Fixed
2023-03-16 15:13:19 +00:00
Leonid Startsev 5cf1c01273 Expand type aliases in K2 when checking if serializable class' properties
have serializers.

#KT-56594 Fixed
#KT-56480 Fixed
2023-03-14 12:11:32 +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
Dmitriy Novozhilov da581f38e1 [Test] Require specifying parser for FIR test. Unify names for FIR tests
Now all tests with `Fir` in name are named accordingly to parser which
  is used in them -- `FirPsi` or `FirLightTree`. This is needed to keep
  consistency between different types of tests, because there is no
  single default in parser mode between different scenarios of using FIR
2023-02-24 11:15:26 +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
Dmitriy Novozhilov 6783621eb0 [FIR] Change API for generating nested classes from plugins
`FirDeclarationGenerationExtension.generateClassLikeDeclaration` was split
  into two functions: one for generating top level classes, and one for
  nested classes. Such change reduces verbosity and error-proness of
  this extension and also allows to smoothly run plugins on local classes

^KT-55248 Fixed
2023-01-12 17:45:39 +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
Alexander Udalov 3c4b5529af Update year to 2023 in COPYRIGHT_HEADER.txt
This commit is the result of changing the year to 2023 in
COPYRIGHT_HEADER.txt and running all `generate*` tasks in
`generators/build.gradle.kts`.
2023-01-02 22:52:15 +01: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 f627d81d53 Make diagnostic for @MetaSerializable on nested annotation
K2 plugin API has a limitation that meta annotations from plugin predicates
can't be used on nested annotations because their resolve process includes supertypes
resolve, that can be affected by the plugin itself. Therefore, @MetaSerializable
can't be applied to nested annotation classes in K2, which is reflected by
this diagnostic.

For old FE, diagnostic is lowered to WARNING with deprecation message.
2022-11-22 10:53:28 +00:00