Skip to content

Commit b496eb7

Browse files
AlejandraPedrozasarahhaggartydaniCsorbaJBdanil-pavlovzamulla
authored
feat: Kotlin 2.2.0 release (#4911)
* update: update JVM target bytecode version 24 * update: updates for REPL in Kotlin 2.2.0 * Adding infromation to docs about JVM default method generation (#4867) * feat: adding infromation to docs about JVM default method generation * implementing review comments * implementing review comments for java to kotlin interop * implementing comments from TWr review * update: adding information about new context-sensitive resolution (#4865) * update: adding information about new context-sensitive resolution to documentation * implementing review comments * implementing TWr review comments * feat: stable non-local jumps (#4875) * feat: add section for read write annotations kotlin metadata (#4870) * feat: add note about experimental annotation support in Kotlin metadata * adding new section to increase visibility in docs * implementing comments from TWr review * update: ant warning (#4882) * update: new annotations defaulting rule and the new `all` meta-target (#4874) * feat: new compiler warning management (#4876) * feat: update JVM records for 2.2.0 * update: language updates for 2.2.0 release (#4873) * update: language updates for 2.2.0 release This PR contains doc updates about context parameters, guard conditions, and nested type aliases. * Daniel review * chore: fix typo Co-authored-by: Dániel Csorba <[email protected]> * add clarification about context receivers * Removing paragraph about context receivers --------- Co-authored-by: Dániel Csorba <[email protected]> * feat: add @JvmExposeBoxed annotation for 2.2.0 * update: Windows support in Native (#4879) * feat: add build tools API * feat: add BCV for KGP * chore: fix What's new version reference * update: update the statuses of language features for Kotlin 2.2.0 (#4901) * Update the statuses of language features for Kotlin 2.2 * fix: fix the status for Kotlin statics --------- Co-authored-by: Andrey Polyakov <[email protected]> * feat: add migration text from kotlinOptions{} * feat: add 2.2.0 compatibility guide document (#4877) * feat: first language tickets for compatibility guide This update includes the first batch of language tickets to be included in the 2.2.0 compatibility guide * updating compatibility guide draft with language and stdlib tickets * update: kotlin-android-extensions deprecation * adding build tools features to the compatibility guide * implementing build tools review comments * implementing additional small comment * Kotlin scripting update * feat: multiplatform compatibility issues * chore: header * fix: review suggestions Co-authored-by: Sarah Haggarty <[email protected]> * fix: review suggestions * implementing TWr review comments --------- Co-authored-by: Danil.Pavlov <[email protected]> Co-authored-by: Sarah Haggarty <[email protected]> Co-authored-by: Sarah Haggarty <[email protected]> * feat: new features in memory manager (#4880) * feat: Kotlin 2.2.0 release details and What's new (#4897) * Kotlin 2.2.0 release details and What's new * Adding What's new 2.2.0 * chore: resolve conflict in kr.tree * chore: fix links * Andrey review * feat: legacy DCE deprecation (#4885) --------- Co-authored-by: Sarah Haggarty <[email protected]> Co-authored-by: Dániel Csorba <[email protected]> Co-authored-by: Danil Pavlov <[email protected]> Co-authored-by: Aleksey Zamulla <[email protected]> Co-authored-by: Mikhail Zarechenskiy <[email protected]> Co-authored-by: Andrey Polyakov <[email protected]> Co-authored-by: Sarah Haggarty <[email protected]>
1 parent 515251e commit b496eb7

40 files changed

+3639
-392
lines changed

data/releases.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
url: https://github.com/JetBrains/kotlin/releases
22

33
latest:
4-
version: 2.1.21
5-
url: https://github.com/JetBrains/kotlin/releases/tag/v2.1.21
4+
version: 2.2.0
5+
url: https://github.com/JetBrains/kotlin/releases/tag/v2.2.0

docs/kr.tree

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
<toc-element topic="competitive-programming.md"/>
3434
</toc-element>
3535
<toc-element toc-title="What's new in Kotlin">
36-
<toc-element toc-title="Kotlin 2.1.20" accepts-web-file-names="whatsnew.html" topic="whatsnew2120.md"/>
36+
<toc-element toc-title="Kotlin 2.2.0" accepts-web-file-names="whatsnew.html" topic="whatsnew22.md"/>
3737
<toc-element toc-title="Kotlin 2.1.0" topic="whatsnew21.md"/>
38-
<toc-element toc-title="Kotlin 2.2.0-RC3" topic="whatsnew-eap.md"/>
38+
<toc-element toc-title="Kotlin 2.2.0-RC3" topic="whatsnew-eap.md" hidden="true"/>
3939
<toc-element toc-title="Earlier versions">
40+
<toc-element toc-title="Kotlin 2.1.20" topic="whatsnew2120.md"/>
4041
<toc-element toc-title="Kotlin 2.0.20" topic="whatsnew2020.md"/>
4142
<toc-element toc-title="Kotlin 2.0.0" topic="whatsnew20.md"/>
4243
<toc-element toc-title="Kotlin 1.9.20" topic="whatsnew1920.md"/>
@@ -119,6 +120,7 @@
119120
<toc-element topic="type-safe-builders.md"/>
120121
<toc-element topic="using-builders-with-builder-inference.md"/>
121122
</toc-element>
123+
<toc-element topic="context-parameters.md"/>
122124
</toc-element>
123125
<toc-element topic="null-safety.md"/>
124126
<toc-element topic="equality.md"/>
@@ -221,7 +223,6 @@
221223
<toc-element topic="dev-server-continuous-compilation.md"/>
222224
<toc-element accepts-web-file-names="debugging-kotlin-in-browser.html" topic="js-debugging.md"/>
223225
<toc-element accepts-web-file-names="running-tests.html" topic="js-running-tests.md"/>
224-
<toc-element topic="javascript-dce.md"/>
225226
<!-- The following page is used in the Mari link service. If you wish to change it here, change the link there too -->
226227
<toc-element toc-title="IR compiler" topic="js-ir-compiler.md"/>
227228
<toc-element topic="js-ir-migration.md"/>
@@ -304,9 +305,11 @@
304305
<toc-element topic="gradle-best-practices.md"/>
305306
<toc-element toc-title="Compiler options in KGP" topic="gradle-compiler-options.md"/>
306307
<toc-element toc-title="Compilation and caches in KGP" topic="gradle-compilation-and-caches.md"/>
308+
<toc-element toc-title="Binary compatibility validation in KGP" topic="gradle-binary-compatibility-validation.md"/>
307309
<toc-element topic="gradle-plugin-variants.md"/>
308310
</toc-element>
309311
<toc-element accepts-web-file-names="using-maven.html" topic="maven.md"/>
312+
<toc-element topic="build-tools-api.md"/>
310313
<toc-element accepts-web-file-names="using-ant.html" topic="ant.md"/>
311314
</toc-element>
312315
<toc-element toc-title="Dokka">
@@ -377,6 +380,7 @@
377380
<toc-element toc-title="Other resources">
378381
<toc-element topic="faq.md"/>
379382
<toc-element toc-title="Kotlin compatibility guides">
383+
<toc-element topic="compatibility-guide-22.md"/>
380384
<toc-element topic="compatibility-guide-21.md"/>
381385
<toc-element topic="compatibility-guide-20.md"/>
382386
<toc-element topic="compatibility-guide-19.md"/>

docs/topics/annotations.md

Lines changed: 155 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[//]: # (title: Annotations)
22

3-
Annotations are means of attaching metadata to code. To declare an annotation, put the `annotation` modifier in front of a class:
3+
Annotations are a means of attaching metadata to code. To declare an annotation, put the `annotation` modifier in front of a class:
44

55
```kotlin
66
annotation class Fancy
@@ -137,14 +137,14 @@ val f = @Suspendable { Fiber.sleep(10) }
137137

138138
## Annotation use-site targets
139139

140-
When you're annotating a property or a primary constructor parameter, there are multiple Java elements which are
140+
When you're annotating a property or a primary constructor parameter, there are multiple Java elements that are
141141
generated from the corresponding Kotlin element, and therefore multiple possible locations for the annotation in
142142
the generated Java bytecode. To specify how exactly the annotation should be generated, use the following syntax:
143143

144144
```kotlin
145-
class Example(@field:Ann val foo, // annotate Java field
146-
@get:Ann val bar, // annotate Java getter
147-
@param:Ann val quux) // annotate Java constructor parameter
145+
class Example(@field:Ann val foo, // annotate only the Java field
146+
@get:Ann val bar, // annotate only the Java getter
147+
@param:Ann val quux) // annotate only the Java constructor parameter
148148
```
149149

150150
The same syntax can be used to annotate the entire file. To do this, put an annotation with the target `file` at
@@ -157,7 +157,7 @@ package org.jetbrains.demo
157157
```
158158

159159
If you have multiple annotations with the same target, you can avoid repeating the target by adding brackets after the
160-
target and putting all the annotations inside the brackets:
160+
target and putting all the annotations inside the brackets (except for the `all` meta-target):
161161

162162
```kotlin
163163
class Example {
@@ -169,27 +169,168 @@ class Example {
169169
The full list of supported use-site targets is:
170170

171171
* `file`
172-
* `property` (annotations with this target are not visible to Java)
173172
* `field`
173+
* `property` (annotations with this target are not visible to Java)
174174
* `get` (property getter)
175175
* `set` (property setter)
176+
* `all` (an experimental meta-target for properties, see [below](#all-meta-target) for its purpose and usage)
176177
* `receiver` (receiver parameter of an extension function or property)
178+
179+
To annotate the receiver parameter of an extension function, use the following syntax:
180+
181+
```kotlin
182+
fun @receiver:Fancy String.myExtension() { ... }
183+
```
184+
177185
* `param` (constructor parameter)
178186
* `setparam` (property setter parameter)
179187
* `delegate` (the field storing the delegate instance for a delegated property)
180188

181-
To annotate the receiver parameter of an extension function, use the following syntax:
189+
### Defaults when no use-site targets are specified
190+
191+
If you don't specify a use-site target, the target is chosen according to the `@Target` annotation of the annotation
192+
being used.
193+
If there are multiple applicable targets, the first applicable target from the following list is used:
194+
195+
* `param`
196+
* `property`
197+
* `field`
198+
199+
Let's use the [`@Email` annotation from Jakarta Bean Validation](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/email):
200+
201+
```java
202+
@Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE})
203+
public @interface Email { }
204+
```
205+
206+
With this annotation, consider the following example:
182207

183208
```kotlin
184-
fun @receiver:Fancy String.myExtension() { ... }
209+
data class User(val username: String,
210+
// @Email is equivalent to @param:Email
211+
@Email val email: String) {
212+
// @Email is equivalent to @field:Email
213+
@Email val secondaryEmail: String? = null
214+
}
185215
```
186216

187-
If you don't specify a use-site target, the target is chosen according to the `@Target` annotation of the annotation
188-
being used. If there are multiple applicable targets, the first applicable target from the following list is used:
217+
Kotlin 2.2.0 introduced an experimental defaulting rule which should
218+
make propagating annotations to parameters, fields, and properties more predictable.
189219

190-
* `param`
191-
* `property`
192-
* `field`
220+
With the new rule, if there are multiple applicable targets, one or more is chosen as follows:
221+
222+
* If the constructor parameter target (`param`) is applicable, it is used.
223+
* If the property target (`property`) is applicable, it is used.
224+
* If the field target (`field`) is applicable while `property` isn't, `field` is used.
225+
226+
Using the same example:
227+
228+
```kotlin
229+
data class User(val username: String,
230+
// @Email is now equivalent to @param:Email @field:Email
231+
@Email val email: String) {
232+
// @Email is still equivalent to @field:Email
233+
@Email val secondaryEmail: String? = null
234+
}
235+
```
236+
237+
If there are multiple targets, and none of `param`, `property`, or `field` are applicable, the annotation is invalid.
238+
239+
To enable the new defaulting rule, use the following line in your Gradle configuration:
240+
241+
```kotlin
242+
// build.gradle.kts
243+
kotlin {
244+
compilerOptions {
245+
freeCompilerArgs.add("-Xannotation-default-target=param-property")
246+
}
247+
}
248+
```
249+
250+
Whenever you'd like to use the old behavior, you can:
251+
252+
* In a specific case, specify the necessary target explicitly, for example, using `@param:Annotation` instead of `@Annotation`.
253+
* For a whole project, use this flag in your Gradle build file:
254+
255+
```kotlin
256+
// build.gradle.kts
257+
kotlin {
258+
compilerOptions {
259+
freeCompilerArgs.add("-Xannotation-default-target=first-only")
260+
}
261+
}
262+
```
263+
264+
### `all` meta-target
265+
266+
<primary-label ref="experimental-opt-in"/>
267+
268+
The `all` target makes it easier to apply the same annotation not only to the parameter and the property or field, but also to the corresponding getter and setter.
269+
270+
Specifically, the annotation marked with `all` is propagated, if applicable:
271+
272+
* To the constructor parameter (`param`) if the property is defined in the primary constructor.
273+
* To the property itself (`property`).
274+
* To the backing field (`field`) if the property has one.
275+
* To the getter (`get`).
276+
* To the setter parameter (`setparam`) if the property is defined as `var`.
277+
* To the Java-only target `RECORD_COMPONENT` if the class has the `@JvmRecord` annotation.
278+
279+
Let's use the [`@Email` annotation from Jakarta Bean Validation](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/email),
280+
which is defined as follows:
281+
282+
```java
283+
@Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE})
284+
public @interface Email { }
285+
```
286+
287+
In the example below, this `@Email` annotation is applied to all relevant targets:
288+
289+
```kotlin
290+
data class User(
291+
val username: String,
292+
// Applies `@Email` to `param`, `field` and `get`
293+
@all:Email val email: String,
294+
// Applies `@Email` to `param`, `field`, `get`, and `set_param`
295+
@all:Email var name: String,
296+
) {
297+
// Applies `@Email` to `field` and `getter` (no `param` since it's not in the constructor)
298+
@all:Email val secondaryEmail: String? = null
299+
}
300+
```
301+
302+
You can use the `all` meta-target with any property, both inside and outside the primary constructor.
303+
304+
#### Limitations
305+
306+
The `all` target comes with some limitations:
307+
308+
* It does not propagate an annotation to types, potential extension receivers, or context receivers or parameters.
309+
* It cannot be used with multiple annotations:
310+
```kotlin
311+
@all:[A B] // forbidden, use `@all:A @all:B`
312+
val x: Int = 5
313+
```
314+
* It cannot be used with [delegated properties](delegated-properties.md).
315+
316+
#### How to enable
317+
318+
To enable the `all` meta-target in your project, use the following compiler option in the command line:
319+
320+
```Bash
321+
-Xannotation-target-all
322+
```
323+
324+
Or add it to the `compilerOptions {}` block of your Gradle build file:
325+
326+
```kotlin
327+
// build.gradle.kts
328+
kotlin {
329+
compilerOptions {
330+
freeCompilerArgs.add("-Xannotation-target-all")
331+
}
332+
}
333+
```
193334

194335
## Java annotations
195336

docs/topics/ant.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
[//]: # (title: Ant)
22

3+
> Starting with Kotlin 2.2.0, support for the Ant build system in Kotlin is deprecated.
4+
> We plan to remove Ant support in Kotlin 2.3.0.
5+
>
6+
> However, if you're interested in becoming an external maintainer for Ant,
7+
> leave a comment in [this YouTrack issue](https://youtrack.jetbrains.com/issue/KT-75875/).
8+
>
9+
{style="warning"}
10+
311
## Getting the Ant tasks
412

513
Kotlin provides three tasks for Ant:

docs/topics/build-tools-api.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
[//]: # (title: Build tools API)
2+
3+
<primary-label ref="experimental-general"/>
4+
5+
<tldr>Currently, the BTA supports Kotlin/JVM only.</tldr>
6+
7+
Kotlin 2.2.0 introduces an experimental Build tools API (BTA) that simplifies how build systems integrate with the
8+
Kotlin compiler.
9+
10+
Previously, adding full Kotlin support to a build system (like incremental compilation, Kotlin compiler plugins,
11+
daemons, and Kotlin Multiplatform) required significant effort. The BTA aims to reduce this complexity by providing
12+
a unified API between build systems and the Kotlin compiler ecosystem.
13+
14+
The BTA defines a single entry point that build systems can implement. This removes the need to deeply integrate with internal compiler details.
15+
16+
> The BTA itself is not yet publicly available for direct use in your own build tool integrations.
17+
> If you're interested in the proposal or want to share feedback, see the [KEEP](https://github.com/Kotlin/KEEP/issues/421).
18+
> Follow the status of its implementation in [YouTrack](https://youtrack.jetbrains.com/issue/KT-76255).
19+
>
20+
{style="warning"}
21+
22+
## Integration with Gradle
23+
24+
The Kotlin Gradle plugin (KGP) has experimental support for the BTA, and you need to opt in to use it.
25+
26+
> We'd appreciate your feedback on your experience with the KGP in [YouTrack](https://youtrack.jetbrains.com/issue/KT-56574).
27+
>
28+
{style="note"}
29+
30+
### How to enable
31+
32+
Add the following property to your `gradle.properties` file:
33+
34+
```kotlin
35+
kotlin.compiler.runViaBuildToolsApi=true
36+
```
37+
38+
### Configure different compiler versions
39+
40+
With the BTA, you can now use a different Kotlin compiler version than the version used by the KGP. This is useful when:
41+
42+
* You want to try new Kotlin features but haven't updated your build scripts yet.
43+
* You need the latest plugin fixes but want to stay on an older compiler version for now.
44+
45+
Here's an example of how to configure this in your `build.gradle.kts` file:
46+
47+
```kotlin
48+
import org.jetbrains.kotlin.buildtools.api.ExperimentalBuildToolsApi
49+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
50+
51+
plugins {
52+
kotlin("jvm") version "2.2.0"
53+
}
54+
55+
group = "org.jetbrains.example"
56+
version = "1.0-SNAPSHOT"
57+
58+
repositories {
59+
mavenCentral()
60+
}
61+
62+
kotlin {
63+
jvmToolchain(8)
64+
@OptIn(ExperimentalBuildToolsApi::class, ExperimentalKotlinGradlePluginApi::class)
65+
compilerVersion.set("2.1.21") // <-- different version than 2.2.0
66+
}
67+
```
68+
69+
#### Compatible Kotlin compiler and KGP versions
70+
71+
The BTA supports:
72+
73+
* The three previous major Kotlin compiler versions.
74+
* One major version forward.
75+
76+
For example, in KGP 2.2.0, the supported Kotlin compiler versions are:
77+
78+
* 1.9.25
79+
* 2.0.x
80+
* 2.1.x
81+
* 2.2.x
82+
* 2.3.x
83+
84+
#### Limitations
85+
86+
Using different compiler versions together with compiler plugins may lead to Kotlin compiler exceptions. The Kotlin team
87+
plans to address this in future Kotlin releases.
88+
89+
### Enable incremental compilation with "in process" strategy
90+
91+
The KGP supports three [compiler execution strategies](gradle-compilation-and-caches.md#defining-kotlin-compiler-execution-strategy).
92+
Ordinarily, the "in-process" strategy (which runs the compiler in the Gradle daemon) doesn't support incremental compilation.
93+
94+
With the BTA, the "in-process" strategy now supports incremental compilation. To enable it, add the following property to
95+
your `gradle.properties` file:
96+
97+
```kotlin
98+
kotlin.compiler.execution.strategy=in-process
99+
```
100+
101+
## Integration with Maven
102+
103+
From Kotlin 2.2.0, the BTA is enabled by default in the [`kotlin-maven-plugin`](maven.md).
104+
105+
Although the BTA doesn't give direct benefits for Maven users yet, it provides a solid foundation for developing features like:
106+
107+
* [Kotlin daemon support](https://youtrack.jetbrains.com/issue/KT-77587)
108+
* [Incremental compilation stabilization](https://youtrack.jetbrains.com/issue/KT-77086)

0 commit comments

Comments
 (0)