Author: Julien Plissonneau Duquène <sre4ever@free.fr>
Description: Fix compiler run-time compatibility with legacy JDKs
Forwarded: not-needed
Last-Update: 2025-03-26

Mask methods introduced with newer JDKs (tested: up to 21) that are conflicting
with Kotlin built-in stdlib extensions, or mute warnings.

This is necessary to be able to use the compiler libraries with older JREs when
built with the current default JDK (21), e.g. the JDK 8 and 11.

> w: libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt: (15,
> 38): Extension is shadowed by a member: public open fun sort(p0:
> Comparator<in T!>!): Unit

> w: libraries/stdlib/jvm/src/kotlin/util/Exceptions.kt: (48, 22): Extension is
> shadowed by a member: public final fun addSuppressed(p0: Throwable!): Unit

> w: libraries/stdlib/src/kotlin/comparisons/Comparisons.kt: (294, 30):
> Extension is shadowed by a member: public open fun reversed():
> Comparator<T!>!

> w: libraries/stdlib/src/kotlin/text/Strings.kt: (234, 32): Extension is
> shadowed by a member: public open fun isEmpty(): Boolean

> w:
> compiler/frontend/src/org/jetbrains/kotlin/util/javaslang/javaslangAdapters.kt:
> (31, 19): Extension is shadowed by a member: public open fun getOrNull(): T!

> w: libraries/stdlib/jdk8/src/kotlin/streams/Streams.kt: (60, 26): Extension
> is shadowed by a member: public open fun toList(): (Mutable)List<T!>!

Some of the changes are partial backports:
 - from v1.4.30:
   - 5cc12b49fcaf (tag: build-1.4.30-dev-1219) Hide
     java.lang.CharSequence::isEmpty from Kotlin built-in class
 - from v1.5.0:
   - a6b51da3081b (tag: build-1.5.0-dev-1490) Fix compilation in the case of
     JDK_16 pointing to JDK 1.8
 - from v1.9.20:
   - b0c5d26d9257 Add new JDK-21 List methods to HIDDEN_METHOD_SIGNATURES

--- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt
+++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt
@@ -162,6 +162,7 @@
         return descriptor.valueParameters.isEmpty()
                 && descriptor.typeParameters.isEmpty()
                 && descriptor.visibility.isVisibleOutside()
+                && !descriptor.isHiddenForResolutionEverywhereBesideSupercalls
     }
 
     private fun isGoodSetMethod(descriptor: FunctionDescriptor, getMethod: FunctionDescriptor): Boolean {
@@ -178,6 +179,7 @@
         return parameter.varargElementType == null
                 && descriptor.typeParameters.isEmpty()
                 && descriptor.visibility.isVisibleOutside()
+                && !descriptor.isHiddenForResolutionEverywhereBesideSupercalls
     }
 
     private fun FunctionDescriptor.findOverridden(condition: (FunctionDescriptor) -> Boolean): FunctionDescriptor? {
--- a/compiler/tests-java8/tests/org/jetbrains/kotlin/serialization/builtins/AdditionalBuiltInsMembersSignatureListsTest.kt
+++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/serialization/builtins/AdditionalBuiltInsMembersSignatureListsTest.kt
@@ -79,6 +79,17 @@
     }
 
     private val LATE_JDK_SIGNATURES = mapOf(
-        "java/lang/String" to setOf("isBlank()Z", "lines()Ljava/util/stream/Stream;", "repeat(I)Ljava/lang/String;")
+        "java/lang/String" to setOf("isBlank()Z", "lines()Ljava/util/stream/Stream;", "repeat(I)Ljava/lang/String;"),
+        "java/lang/CharSequence" to setOf("isEmpty()Z"),
+        "java/util/List" to setOf(
+            // From JDK 21
+            //"addFirst(Ljava/lang/Object;)V",
+            //"addLast(Ljava/lang/Object;)V",
+            //"getFirst()Ljava/lang/Object;",
+            //"getLast()Ljava/lang/Object;",
+            //"removeFirst()Ljava/lang/Object;",
+            //"removeLast()Ljava/lang/Object;",
+            "reversed()Ljava/util/List;"
+        )
     )
 }
--- a/core/descriptors.jvm/src/org/jetbrains/kotlin/builtins/jvm/JvmBuiltInsSettings.kt
+++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/builtins/jvm/JvmBuiltInsSettings.kt
@@ -351,7 +351,21 @@
             signatures {
                 buildPrimitiveValueMethodsSet() +
 
-                        inJavaUtil("List", "sort(Ljava/util/Comparator;)V") +
+                        inJavaUtil(
+                            "List",
+                            "sort(Ljava/util/Comparator;)V",
+                            // From JDK 21
+                            //"addFirst(Ljava/lang/Object;)V",
+                            //"addLast(Ljava/lang/Object;)V",
+                            //"getFirst()Ljava/lang/Object;",
+                            //"getLast()Ljava/lang/Object;",
+                            //"removeFirst()Ljava/lang/Object;",
+                            //"removeLast()Ljava/lang/Object;",
+                            "reversed()Ljava/util/List;"
+                        ) +
+
+                        // JDK 16+
+                        inClass("java/util/stream/Stream", "toList()Ljava/util/List;") +
 
                         inJavaLang(
                             "String",
@@ -378,7 +392,9 @@
                         inJavaLang("Double", "isInfinite()Z", "isNaN()Z") +
                         inJavaLang("Float", "isInfinite()Z", "isNaN()Z") +
 
-                        inJavaLang("Enum", "getDeclaringClass()Ljava/lang/Class;", "finalize()V")
+                        inJavaLang("Enum", "getDeclaringClass()Ljava/lang/Class;", "finalize()V") +
+                        inJavaLang("CharSequence", "isEmpty()Z")
+
             }
 
         private fun buildPrimitiveValueMethodsSet(): Set<String> =
--- a/compiler/frontend/src/org/jetbrains/kotlin/util/javaslang/javaslangAdapters.kt
+++ b/compiler/frontend/src/org/jetbrains/kotlin/util/javaslang/javaslangAdapters.kt
@@ -28,5 +28,6 @@
 operator fun <T> Tuple2<T, *>.component1(): T = _1()
 operator fun <T> Tuple2<*, T>.component2(): T = _2()
 
+@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // ignore
 fun <T> Option<T>.getOrNull(): T? = getOrElse(null as T?)
 fun <K, V> ImmutableMap<K, V>.getOrNull(k: K): V? = get(k)?.getOrElse(null as V?)
--- a/libraries/stdlib/jdk8/src/kotlin/streams/Streams.kt
+++ b/libraries/stdlib/jdk8/src/kotlin/streams/Streams.kt
@@ -57,6 +57,7 @@
  * Returns a [List] containing all elements produced by this stream.
  */
 @SinceKotlin("1.2")
+@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // with JDK 16+
 public fun <T> Stream<T>.toList(): List<T> = collect(Collectors.toList<T>())
 
 /**
--- a/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt
+++ b/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt
@@ -11,7 +11,7 @@
 
 @Deprecated("Use sortWith(comparator) instead.", ReplaceWith("this.sortWith(comparator)"), level = DeprecationLevel.ERROR)
 @kotlin.internal.InlineOnly
-@Suppress("UNUSED_PARAMETER")
+@Suppress("UNUSED_PARAMETER", "EXTENSION_SHADOWED_BY_MEMBER")
 public inline fun <T> MutableList<T>.sort(comparator: Comparator<in T>): Unit = throw NotImplementedError()
 
 @Deprecated("Use sortWith(Comparator(comparison)) instead.", ReplaceWith("this.sortWith(Comparator(comparison))"), level = DeprecationLevel.ERROR)
--- a/libraries/stdlib/jvm/src/kotlin/util/Exceptions.kt
+++ b/libraries/stdlib/jvm/src/kotlin/util/Exceptions.kt
@@ -45,4 +45,5 @@
  * When supported by the platform adds the specified exception to the list of exceptions that were
  * suppressed in order to deliver this exception.
  */
-public fun Throwable.addSuppressed(exception: Throwable) = IMPLEMENTATIONS.addSuppressed(this, exception)
\ No newline at end of file
+@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
+public fun Throwable.addSuppressed(exception: Throwable) = IMPLEMENTATIONS.addSuppressed(this, exception)
--- a/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt
+++ b/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt
@@ -291,6 +291,7 @@
  *
  *  @sample samples.comparisons.Comparisons.reversed
  */
+@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
 public fun <T> Comparator<T>.reversed(): Comparator<T> = when (this) {
     is ReversedComparator -> this.comparator
     NaturalOrderComparator -> @Suppress("UNCHECKED_CAST") (ReverseOrderComparator as Comparator<T>)
--- a/libraries/stdlib/src/kotlin/text/Strings.kt
+++ b/libraries/stdlib/src/kotlin/text/Strings.kt
@@ -231,6 +231,7 @@
  * @sample samples.text.Strings.stringIsEmpty
  */
 @kotlin.internal.InlineOnly
+@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
 public inline fun CharSequence.isEmpty(): Boolean = length == 0
 
 /**
