Commit Graph

133 Commits

Author SHA1 Message Date
Ivan Kylchik 1dec9fc675 [JVM_IR] Move methods that generate LN into LineNumberMapper class 2023-07-20 09:01:43 +00:00
Dmitrii Gridin 7620289f6f [FIR] fix contract violation from meta annotations
^KT-60448 Fixed
2023-07-18 16:50:25 +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
Sergey.Shanshin 5ba1b36796 [KxSerialization] Change visibility for synthetic functions
- synthetic constructor became internal for final classes
- write$Self became internal for final classes

Resolves https://github.com/Kotlin/kotlinx.serialization/issues/2209

Merge-request: KT-MR-9068
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-07-14 17:36:22 +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
Sergej Jaskiewicz 6281faa063 [IR] Move IrFactory#createBlockBody overloads to extension methods
This is to prepare for IrFactory auto-generation (KT-59308).
2023-07-04 15:19:28 +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
Aleksander Kuznetsov 53c488079f [serialization] permit multiple occurrences of lazy function in sources
IDEA projects may have duplicate dependencies on kotlin stdlib. In that case, `IrPluginContext.referenceFunctions` may return more than one copy of each standard function, including three overloads of `kotlin.lazy`. And here's when serialization plugin may fail with an exception.

 It affects Kotlin debugger -- if such a project or module also has serialization plugin enabled, then it completely breaks new evaluation, meaning it's impossible to compute any expression.

Fixes IDEA-314785

Merge-request: KT-MR-10262
Merged-by: Alexander Kuznetsov <Aleksander.Kuznetsov@jetbrains.com>
2023-06-06 12:43:25 +00:00
Vladimir Dolzhenko 7f74950993 Adjust name of IntelliJ Kotlin Plugin team
Merge-request: KT-MR-10425
Merged-by: Vladimir Dolzhenko <Vladimir.Dolzhenko@jetbrains.com>
2023-06-06 07:10:22 +00: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
Dmitrii Gridin 5b3f617378 [FIR] add memberRequiredPhase to declaredMemberScope
^KT-56551
2023-05-19 12:43:45 +00:00
Yahor Berdnikau eef7c3e834 Publish serialization plugin as kotlin-serialization-compiler-plugin
With this change it will have consistent naming with other compiler plugins.

'-embeddable' was also renamed.

'dist' should contain two identical jar files:
- 'serialization-compiler-plugin.jar'
- 'kotlinx-serialization-compiler-plugin.jar'

^KT-58530 In Progress
2023-05-12 09:31:41 +00:00
Sergej Jaskiewicz 1a371350ea [IR] Make IrClassifierSymbol a sealed interface 2023-05-10 11:51:25 +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
Mikhail Glukhikh dc38ce24f7 K2: fix field annotation splitting and frontend checks
Related to KT-57135
2023-04-25 12:18:26 +00: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 f29e505bd3 Use correct check in FirSerializationPluginClassChecker to determine if the given type is a enum class.
Otherwise, findTypeSerializerOrContextUnchecked works incorrectly when analyzing code
compiled with Kotlin 1.7.20 and serialization 1.4.1.

#KT-57704 Fixed
2023-04-19 16:13:28 +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
Yahor Berdnikau a58d6fd13a Publish 'kotlinx-serialization-compiler-plugin' separately
- Also publish 'kotlinx-serialization-compiler-plugin-embeddable' to be
used with 'kotlin-compiler-embeddable'.
- Now Serialization Maven and Gradle plugins do not embed plugin into
itself, but declare on published one normal dependency.
- 'kotlin-serialization-unshaded' publication is deprecated.

^KT-52811 In Progress
2023-04-17 14:40:50 +00:00
Dmitriy Novozhilov 94f77add49 [FIR] Try to load properties in order according to kotlinx.serialization metadata extension
Move metadata extension with property order from kotlinx.serialization to core

After fix of KT-54792 properties will be deserialized in declaration order
  if corresponding class was compiled with modern compiler. But this order
  is needed for kotlinx.serialization for binaries compiled with any
  kotlin compiler >= 1.4. Since we don't plan to add any extension points
  into (de)serialization into FIR, we need to take into account existing
  metadata extension from kotlinx.serialization in compiler itself

^KT-57769 Fixed
2023-04-11 13:43:39 +00:00
Igor Yakovlev 8102744e00 [Serialization] Fix serialization test data 2023-04-04 11:04:18 +00:00
Igor Yakovlev 81ceb856bb [Serialization] Fix invalid int array creation 2023-04-04 11:04:18 +00:00
Igor Yakovlev 8cb1fca299 [Serialization] Add wasm target platform to serialization plugin 2023-04-04 11:04:17 +00:00
Igor Yakovlev e990c8efd2 [Serialization] Fix invalid ir type for serialize generator 2023-04-04 11:04:17 +00:00
Igor Yakovlev b4d6482581 [Serialization] Fix invalid ir type for deserialize generator 2023-04-04 11:04:17 +00:00
Dmitriy Novozhilov 5ccf1eddab [K2 Serialization] Remove obsolete formatting suppression 2023-03-29 15:18:37 +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
Dmitrii Gridin 706ff6b61f [FIR] pre-resolve some scope callables before creating a scope
In a lot of cases, when we want to process the
declaration in scope, it should be resolved to at least TYPES phase.

To avoid doing it manually in all our variety of scopes, we do it when the scope it created.
It was implicitly working manually before as lazy resolve did a lot of extra work
on resolving a declaration it was not supposed to resolve.
Now it's not the case, and we have to explicitly resolve all the declarations we need.

^KT-56543

Co-authored-by: Ilya Kirillov <ilya.kirillov@jetbrains.com>
2023-03-24 09:57:46 +00:00
Ivan Kylchik 2b387e7a10 [FIR LT] Get correct startOffset value for declarations with comments
#KT-56913 Fixed
#KT-56926 Fixed
2023-03-21 16:33:00 +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 7a79de6d16 [K/JS] Move JS IR box tests setup into separated function 2023-03-19 22:53:05 +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
Roman Golyshev ff7c095fb3 [FIR][Serialization] Use resolvedCompilerAnnotationsWithClassIds to avoid recursive resolve
`resolvedAnnotationsWithClassIds` requires resolve to TYPES phase,
which is unavailable during compiler plugins stages. Trying to resolve
to that phases will cause an infinite recursive resolve loop

Also, call `resolvedAnnotationsWithArguments` only when
`neededArguments` is true

^KTIJ-24893 Fixed
2023-03-13 17:05:26 +00:00
Sergey.Shanshin 4e56079c59 [KxSerialization] Fix "SyntheticAccessorLowering should not attempt to modify other files"
Added additional check for generated serializer to use cached child serializers

Fixes #KT-56990

Merge-request: KT-MR-9057
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-03-02 13:34:48 +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
Kirill Rakhman eee66ab43f [FIR] Remove duplicate annotations from primary ctor params/properties
If an annotation doesn't specify an explicit use-site target,
previously it was added to both, the primary constructor value parameter
and the property in the FIR. Then, in FIR2IR, only the "correct" one was
added to the IR. Move up the deduplication logic into the frontend.

^KT-56177 Fixed
2023-02-28 10:19:17 +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
Sergey.Shanshin 63025030e3 [KxSerialization] Fix SERIALIZER_NOT_FOUND diagnostic for enums
Fixes #KT-56738

Merge-request: KT-MR-8938
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-02-22 19:38:18 +00:00
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
Leonid Startsev 5cd817955b Actualize README.md for kotlinx-serialization plugin 2023-02-15 13:39:58 +00:00
Sergey.Shanshin 498c1abbc6 [KxSerialization] Fix SERIALIZER_NOT_FOUND diagnostic for enums
If enum class from dependencies is used, which was compiled by a new version of the plugin, which uses a factory and does not create a nested `$serializer` class, then an `SERIALIZER_NOT_FOUND` diagnostic error is thrown for such an enumeration.
This happens if the old serialization runtime is used for the current project - in this case, the serializer is taken from the nested class `$serializer`. Since it is missing, the diagnostics does not work correctly.
It is acceptable for enumerations to ignore this error, because we know that enumerations are always serializable.

Merge-request: KT-MR-8818
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
2023-02-14 22:10:30 +00:00