K1: Implement a checker that disallows to have different member scopes for expect open and its actual

^KT-22841 Fixed
Review: https://jetbrains.team/p/kt/reviews/11603/timeline

The commit also introduces `@AllowDifferentMembersInActual` annotation in
stdlib which allows to suppress the diagnostic
This commit is contained in:
Nikita Bobko
2023-07-11 18:00:53 +02:00
committed by Space Team
parent 797ca34a34
commit 25c082f02b
155 changed files with 3875 additions and 40 deletions
@@ -0,0 +1,37 @@
package kotlin
import kotlin.annotation.AnnotationTarget.*
/**
* Kotlin reports a compilation error for cases when non-final `expect class` and its counterpart `actual class` have different set of
* non-private members (e.g. when a new member declared in `actual class` that wasn't presented in its `expect class`, or when
* modality is different).
*
* By marking `actual class` with this annotation, you suppress the compilation error mentioned above.
*
* ## Safety Risks
*
* Using this annotation causes undefined behaviour, it's not known what can break because of that. Be prepared that at some point this
* annotation might become deprecated, and you'd need to migrate your code.
*
* If you use this annotation and can't migrate, consider describing your use cases in
* [KT-22841](https://youtrack.jetbrains.com/issue/KT-22841) comments.
*
* ## Migration
*
* Please consider the following alternatives:
* - Make members in `expect class` and `actual class` the same
* - If the member was declared only in `actual class` then you'd need to declare it in the `expect class` as well
* - If the member is already declared in the `expect class` then make sure that the member is the same in the `actual class` (modality is
* the same, visibility is the same, return type is the same, etc.)
* - Make the `expect class` `final`. You might need to rewrite your code to use composition instead of inheritance.
* - Mark the members under the question as `private`
*
* [AllowDifferentMembersInActual] is supposed to be used for the transition period.
*/
@Retention(AnnotationRetention.SOURCE)
@Target(CLASS, TYPEALIAS)
@MustBeDocumented
@ExperimentalMultiplatform
@SinceKotlin("1.9")
public annotation class AllowDifferentMembersInActual
@@ -32,4 +32,7 @@ public expect interface KClass<T : Any> : KClassifier {
@SinceKotlin("1.1")
public fun isInstance(value: Any?): Boolean
override fun equals(other: Any?): Boolean // KT-24971
override fun hashCode(): Int // KT-24971
}