Commit Graph

51 Commits

Author SHA1 Message Date
Dmitriy Novozhilov e5b4cf9f04 [Test] Update serialization tests according to KT-56755 2023-02-20 08:40:34 +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
Sergey.Shanshin c9b8160f1e [kxSerialization] Added diagnostic on useless Serializer annotation
Resolves Kotlin/kotlinx.serialization#2182

Merge-request: KT-MR-8718
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-02-14 22:00:33 +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 1984a1615e [Serialization] Use new DSL for generating declarations 2023-01-12 17:45:41 +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
Leonid Startsev a1894ed027 Update runtime dependency in kotlinx.serialization plugin tests to 1.4.1 2023-01-09 18:25:42 +00:00
Pavel Mikhailovskii 06d3e1d8bd KT-55005 Do not generate CHECKCAST before AASTORE 2022-12-21 15:06:07 +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
Steven Schäfer 6af616d3c3 FIR: make declarations marked with 'override' implicitly open
#KT-52236 Fixed
2022-12-14 21:46:41 +00:00
Ilya Kirillov 644d1bf0d0 [FIR] ignore tests which fail because of resolve contracts violation 2022-12-12 16:21:07 +00: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
Dmitriy Novozhilov 6864433581 [FIR] Support resolution of meta-annotations before supertypes
Now annotations are resolved with following algorithm:
1. On COMPILER_REQUIRED_ANNOTATIONS we resolve all annotations
     and store results if this is compiler annotation, plugin annotation,
     or annotation with meta-annotation (meta annotations are checked
     recursively with designated resolution if needed)
2. On TYPES stage we resolve all those annotations once again and if
     some annotation changes resolution then we keep type from p.1 and
     report error on this annotation, so user should disambiguate it

Ambiguity may occur because of nested annotations with same name as
  plugin annotations:

```
annotation class SomeAnnotation // (1) plugin annotation

open class Base {
    annotation class SomeAnnotation // (2)
}

class Derived : Base() {
    @SomeAnnotation // <-----------------
    class Inner
}
```
At COMPILER_REQUIRED_ANNOTATIONS annotation call will be resolved to (1)
  because at this stage supertypes are not resolved yet, and we consider
  only importing scopes. At the TYPES stage we will find correct
  annotation from supertype
2022-12-01 07:29:39 +00:00
Sergey.Shanshin 339b152390 Implemented caching of child serializers
Resolves Kotlin/kotlinx.serialization#1918

Merge-request: KT-MR-7578
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2022-11-23 19:53:44 +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
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 76fc7ce0ad Fix creating FirSerializableProperties for external classes.
External classes do not have primary constructor, so we need to use 0 as
value parameter count for them to be externally serializable when possible.
2022-11-22 10:53:27 +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
Sergey.Shanshin d4cfb002d0 Added support of named companion in serializable class to the FIR
Merge-request: KT-MR-7472
2022-10-27 12:48:29 +00:00
Leonid Startsev 4cf50d7d23 Add checker for companion objects inside serializable class:
Warning for old FE, error in FIR

#KT-54441 Fixed

Merged-by: Leonid Startsev <leonid.startsev@jetbrains.com>
2022-10-20 10:18:04 +00: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 5a1c2c56bd Added support of external serializers to the FIR
Co-authored-by: Leonid Startsev <leonid.startsev@jetbrains.com>
Merge-request: KT-MR-7334
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2022-10-13 15:25:39 +00:00
Sergey.Shanshin 503f4d924d Added support of MetaSerializable to the FIR 2022-10-12 20:10:58 +00:00
Leonid Startsev 090aec6b3b Prohibit Array<T> in @Serializable classes
because it's impossible to correctly create array reflectively without
knowing correct KClass.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1243
2022-10-12 11:48:21 +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
Sergey.Shanshin f4845b8dd9 Marked write$Self and $serializer from serialization as synthetic 2022-10-11 17:05:03 +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
Sergey.Shanshin 9210108d5a Added test on Serializable(with=...) in K2 2022-09-23 11:56:19 +00:00
Leonid Startsev ad2adadb36 Remove OperationType.PLUGIN_DEFINED type:
instead, plugins should emit the code similar to the TYPE_OF one with
a special call to MagicApiIntrinsics.voidMagicApiCall directly afterwards.

This is required because old compiler need to correctly inline code
rewritten by plugin.
2022-09-16 14:34:29 +00:00
Leonid Startsev eae588abea Improve line numbers handling in serialization intrinsic
Fix nullable serializers on top-level & method with incorrect receiver

Improve handling of different runtime versions
2022-09-16 14:34:29 +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
Leonid Startsev f9edbd825a Introduce support for plugin-defined intrinsics in old JVM backend:
(Old is created first because all intrinsics emit bytecode anyway)

Provide intrinsic for serializer<T>() function so it won't
invoke typeOf() construction and KType->KSerializer conversion
making it fast and truly reflectionless

Add support for recalculating stack size in plugin-defined intrinsics
since it is needed for correct work:
Unify method for recalculating stack size with existing typeOf intrinsic

Add testdata for IR for future intrinsic in IR
2022-09-16 14:34:28 +00:00
Dmitriy Novozhilov 671083c701 [Serialization] Implement serialization checker for K2 version of plugin
^KT-53178 Fixed
2022-09-07 07:57:38 +00:00
Dmitriy Novozhilov a6d3c6e5c6 [Serialization] Suppress OPT_IN errors in all diagnostic tests 2022-09-05 10:20:06 +00:00
Dmitriy Novozhilov 84b8ab1f9c [Serialization] Add DiagnosticsTests for FIR 2022-09-05 10:20:06 +00:00
Dmitriy Novozhilov a2ffba275c [Serialization] Migrate all tests to new test infrastructure 2022-09-05 10:20:05 +00:00