News

The K2 Compiler Is Going Stable in Kotlin 2.0

Read this post in other languages:

We’ve been working on a new frontend for the Kotlin compiler (code-named “K2”) for quite a while. The frontend is the part of the compiler that parses your code and performs the semantic analysis, data flow analysis, call resolution, and type inference. This is the part of the compiler that you, the developer, interact with the most. It’s also the part of the compiler that continuously runs inside your IDE, reports all error and warning messages, and helps you with tasks like auto completion, semantic-aware inspections, intentions, and refactorings. The new frontend is already available for preview – we’re continually polishing and stabilizing it and plan to make it the default compiler frontend in a future Kotlin release. We’ve decided to name this future release Kotlin 2.0. Keep reading to learn more about the roadmap for the Kotlin 2.0 release, our motivation, and what to expect from it.

What is the Kotlin 2.0 release roadmap?

We plan to release one more traditionally versioned release as Kotlin 1.9, which will also include updates from the ongoing development of the K2 compiler frontend as it approaches stabilization. There will be no Kotlin 1.10. The next major version after Kotlin 1.9 will be Kotlin 2.0.

Why Kotlin 2.0?

The frontend is a major part of the compiler, and the K2 compiler frontend is not just a refactoring of the old compiler frontend, but a complete rewrite from scratch based on the new architecture. We’ve been publishing technical details on the differences in various blogs, videos, and conference talks. The Road to the K2 Compiler is a good place to start if you want more information. In short, the new architecture is faster and more extensible by design, addresses technical debt in the old compiler frontend, fixes bugs and corner cases in various places, and paves the road for future extensions of the language.

For programming languages, the major version is often incremented when major features or breaking changes are introduced. However, for software programs, a major version can be incremented whenever anything is substantially rewritten to signify a major rework, even if the feature set stays mostly the same. Despite the fact that Kotlin is a language, we’ve decided to follow this software practice. The Kotlin compiler, first and foremost, is a big piece of software. The version of the Kotlin language is defined by the version of its compiler. We do provide Kotlin specifications as a reference to the language, but not as its definition.

Will it break my code?

Kotlin changes with every language version update. New language features get introduced and old bugs get fixed. We always try to minimize the number and impact of breaking changes and follow the Kotlin language evolution process. Kotlin 2.0 will be no different in that respect.

We are consciously avoiding introducing any new features in Kotlin 2.0. The rewrite of the Kotlin compiler frontend is a considerable change and introduces a number of improvements just by itself. For one, the new data flow analysis algorithm is more precise and leads to better smart casts than were possible before – check out these issues with concrete examples. This turns red code that will not compile in Kotlin 1.9 on the old frontend into green code that compiles in Kotlin 2.0, which is, technically, a defining aspect of the new Kotlin language feature. However, here it is simply a side-effect of a new compiler architecture.

In addition, tons of long-standing bugs have been fixed and, inevitably, some of those fixes might break code that accidentally relied on incorrect compiler behavior. We’re carefully studying the impact of all the changes in behavior between the old and new K2 frontends and making additional efforts to replicate the old behavior whenever possible, even when we don’t like it, if it might affect a sizable number of Kotlin users.

We are committed to minimizing the impact of migrating your code from Kotlin 1.9 to Kotlin 2.0. We will keep you updated on the Kotlin 2.0 stabilization process and provide you with ample time to try out Kotlin 2.0 on your projects in advance so you can report any regressions that we might have missed in our testing. Our current work is focused on a beta-quality release of the K2 compiler frontend later this year, which you can track in this roadmap issue.

What about IDE support for Kotlin 2.0? 

The Kotlin IDE plugin is bundled into the IntelliJ IDEA and Android Studio updates. It includes a copy of the Kotlin compiler frontend for the semantic analysis of your code, but that is not the version of the Kotlin compiler that is used to actually compile your code – the actual version of the compiler is determined by the settings in your build files. So, when you compile your code with Kotlin language version 2.0, you’ll be using the new compiler frontend and will get all of the performance benefits of the new compiler architecture. However, the IDE might be still using the old frontend to inspect your code.

In order to give you the performance benefits of the new compiler frontend in your IDE, we are writing a completely new version of the Kotlin IDE plugin that is built on top of the K2 compiler frontend. We are currently working towards a public preview of this new IDE plugin. You can track this issue for updates.

What to expect after Kotlin 2.0?

The new architecture of the Kotlin 2.0 compiler frontend enables clean implementation of many cool new features. Some of those features, like KT-10468 context receivers, are already available for preview in the old compiler frontend, but they won’t be turned on in Kotlin 2.0 by default. Instead, they will remain in the preview stage. Stabilization of new features will happen in future Kotlin 2.x releases, along with the implementation of other new things we have in our pipeline. As usual, we will share regular updates on our plans via the Kotlin roadmap.

When will we have Kotlin 3.0? We don’t know yet. We are tentatively discussing lots of big changes to the language in the future, especially the most popular requests from our last Kotlin Features Survey. It is certainly possible that some of these changes could be significant enough to warrant the next major Kotlin version.

How can you help?

Try out the new compiler frontend and give us your feedback! Starting with Kotlin 1.8.20, you can enable a preview of the Kotlin 2.0 language version via the regular Kotlin language version flag.

Enable Kotlin K2 Compiler

Learn more

image description