From 6fa8083a70ad41d4560cab7fe5abfd892568b7a4 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 12 Oct 2015 19:54:12 +0300 Subject: [PATCH] Transform builtin Map declaration and adjust stdlib --- compiler/testData/builtin-classes.txt | 32 +++++++++++-------- .../j+k/collectionOverrides/mapGetOverride.kt | 13 ++++++++ .../collectionOverrides/mapGetOverride.txt | 28 ++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++ core/builtins/native/kotlin/Collections.kt | 20 ++++++------ .../load/java/lazy/descriptors/MemberIndex.kt | 3 +- .../kotlin/load/java/specialBuiltinMembers.kt | 14 ++++++-- js/js.libraries/src/core/javautil.kt | 14 ++++---- .../src/kotlin/collections/MapWithDefault.kt | 26 +++++++-------- .../stdlib/src/kotlin/collections/Maps.kt | 12 +++---- .../kotlin/collections/MutableCollections.kt | 31 ++++++++++++++++++ 11 files changed, 146 insertions(+), 53 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.kt create mode 100644 compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.txt diff --git a/compiler/testData/builtin-classes.txt b/compiler/testData/builtin-classes.txt index 6e2a52e4d8f..62caf1b1bdc 100644 --- a/compiler/testData/builtin-classes.txt +++ b/compiler/testData/builtin-classes.txt @@ -928,16 +928,19 @@ public final class LongRange : kotlin.Range, kotlin.Progression { + public abstract val entries: kotlin.Set> + public abstract fun (): kotlin.Set> public abstract val isEmpty: kotlin.Boolean public abstract fun (): kotlin.Boolean + public abstract val keys: kotlin.Set + public abstract fun (): kotlin.Set public abstract val size: kotlin.Int public abstract fun (): kotlin.Int - public abstract fun containsKey(/*0*/ key: kotlin.Any?): kotlin.Boolean - public abstract fun containsValue(/*0*/ value: kotlin.Any?): kotlin.Boolean - public abstract fun entrySet(): kotlin.Set> - public abstract operator fun get(/*0*/ key: kotlin.Any?): V? - public abstract fun keySet(): kotlin.Set - public abstract fun values(): kotlin.Collection + public abstract val values: kotlin.Collection + public abstract fun (): kotlin.Collection + public abstract fun containsKey(/*0*/ key: K): kotlin.Boolean + public abstract fun containsValue(/*0*/ value: @kotlin.UnsafeVariance() V): kotlin.Boolean + public abstract operator fun get(/*0*/ key: K): V? public interface Entry { public abstract val key: K @@ -1012,20 +1015,23 @@ public interface MutableListIterator : kotlin.ListIterator, kotlin.M } public interface MutableMap : kotlin.Map { + public abstract override /*1*/ val entries: kotlin.MutableSet> + public abstract override /*1*/ fun (): kotlin.MutableSet> public abstract override /*1*/ /*fake_override*/ val isEmpty: kotlin.Boolean public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Boolean + public abstract override /*1*/ val keys: kotlin.MutableSet + public abstract override /*1*/ fun (): kotlin.MutableSet public abstract override /*1*/ /*fake_override*/ val size: kotlin.Int public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Int + public abstract override /*1*/ val values: kotlin.MutableCollection + public abstract override /*1*/ fun (): kotlin.MutableCollection public abstract fun clear(): kotlin.Unit - public abstract override /*1*/ /*fake_override*/ fun containsKey(/*0*/ key: kotlin.Any?): kotlin.Boolean - public abstract override /*1*/ /*fake_override*/ fun containsValue(/*0*/ value: kotlin.Any?): kotlin.Boolean - public abstract override /*1*/ fun entrySet(): kotlin.MutableSet> - public abstract override /*1*/ /*fake_override*/ fun get(/*0*/ key: kotlin.Any?): V? - public abstract override /*1*/ fun keySet(): kotlin.MutableSet + public abstract override /*1*/ /*fake_override*/ fun containsKey(/*0*/ key: K): kotlin.Boolean + public abstract override /*1*/ /*fake_override*/ fun containsValue(/*0*/ value: V): kotlin.Boolean + public abstract override /*1*/ /*fake_override*/ fun get(/*0*/ key: K): V? public abstract fun put(/*0*/ key: K, /*1*/ value: V): V? public abstract fun putAll(/*0*/ m: kotlin.Map): kotlin.Unit - public abstract fun remove(/*0*/ key: kotlin.Any?): V? - public abstract override /*1*/ fun values(): kotlin.MutableCollection + public abstract fun remove(/*0*/ key: K): V? public interface MutableEntry : kotlin.Map.Entry { public abstract override /*1*/ /*fake_override*/ val key: K diff --git a/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.kt b/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.kt new file mode 100644 index 00000000000..54302d47afd --- /dev/null +++ b/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.kt @@ -0,0 +1,13 @@ +// !DIAGNOSTICS: -UNUSED_VALUE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE +// FILE: MyMap.java + +abstract public class MyMap extends java.util.AbstractMap { + String get(Object q) { } +} + +// FILE: main.kt + +fun foo(m: MyMap) { + var x: String? = m.get(1.0) + x = m[2.0] +} diff --git a/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.txt b/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.txt new file mode 100644 index 00000000000..dc122895256 --- /dev/null +++ b/compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.txt @@ -0,0 +1,28 @@ +package + +public fun foo(/*0*/ m: MyMap): kotlin.Unit + +public abstract class MyMap : java.util.AbstractMap { + public constructor MyMap() + public abstract override /*1*/ /*fake_override*/ val entries: kotlin.MutableSet> + public open override /*1*/ /*fake_override*/ val isEmpty: kotlin.Boolean + invisible_fake final override /*1*/ /*fake_override*/ var keySet: kotlin.(Mutable)Set! + public open override /*1*/ /*fake_override*/ val keys: kotlin.MutableSet + public open override /*1*/ /*fake_override*/ val size: kotlin.Int + invisible_fake final override /*1*/ /*fake_override*/ var values: kotlin.(Mutable)Collection! + public open override /*1*/ /*fake_override*/ val values: kotlin.MutableCollection + public open override /*1*/ /*fake_override*/ fun clear(): kotlin.Unit + protected/*protected and package*/ open override /*1*/ /*fake_override*/ fun clone(): kotlin.Any! + public open override /*1*/ /*fake_override*/ fun containsKey(/*0*/ key: kotlin.Double!): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun containsValue(/*0*/ value: kotlin.CharSequence!): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public/*package*/ open override /*1*/ /*synthesized*/ fun get(/*0*/ q: kotlin.Double!): kotlin.String? + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun put(/*0*/ key: kotlin.Double!, /*1*/ value: kotlin.CharSequence!): kotlin.CharSequence? + public open override /*1*/ /*fake_override*/ fun putAll(/*0*/ m: kotlin.Map): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun remove(/*0*/ key: kotlin.Double!): kotlin.CharSequence? + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + invisible_fake open override /*1*/ /*fake_override*/ fun eq(/*0*/ p0: kotlin.Any!, /*1*/ p1: kotlin.Any!): kotlin.Boolean +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index c1656ba8d4e..ec890a988a3 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -9185,6 +9185,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("mapGetOverride.kt") + public void testMapGetOverride() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/collectionOverrides/mapGetOverride.kt"); + doTest(fileName); + } + @TestMetadata("removeAt.kt") public void testRemoveAt() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/collectionOverrides/removeAt.kt"); diff --git a/core/builtins/native/kotlin/Collections.kt b/core/builtins/native/kotlin/Collections.kt index 2bd23ee8ec2..a12a01aac6f 100644 --- a/core/builtins/native/kotlin/Collections.kt +++ b/core/builtins/native/kotlin/Collections.kt @@ -284,33 +284,33 @@ public interface Map { /** * Returns `true` if the map contains the specified [key]. */ - public fun containsKey(key: Any?): Boolean + public fun containsKey(key: K): Boolean /** * Returns `true` if the map maps one or more keys to the specified [value]. */ - public fun containsValue(value: Any?): Boolean + public fun containsValue(value: @UnsafeVariance V): Boolean /** * Returns the value corresponding to the given [key], or `null` if such a key is not present in the map. */ - public operator fun get(key: Any?): V? + public operator fun get(key: K): V? // Views /** * Returns a [Set] of all keys in this map. */ - public fun keySet(): Set + public val keys: Set /** * Returns a [Collection] of all values in this map. Note that this collection may contain duplicate values. */ - public fun values(): Collection + public val values: Collection /** * Returns a [Set] of all key/value pairs in this map. */ - public fun entrySet(): Set> + public val entries: Set> /** * Represents a key/value pair held by a [Map]. @@ -348,7 +348,7 @@ public interface MutableMap : Map { * * @return the previous value associated with the key, or `null` if the key was not present in the map. */ - public fun remove(key: Any?): V? + public fun remove(key: K): V? // Bulk Modification Operations /** @@ -362,9 +362,9 @@ public interface MutableMap : Map { public fun clear(): Unit // Views - override fun keySet(): MutableSet - override fun values(): MutableCollection - override fun entrySet(): MutableSet> + override val keys: MutableSet + override val values: MutableCollection + override val entries: MutableSet> /** * Represents a key/value pair held by a [MutableMap]. diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/MemberIndex.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/MemberIndex.kt index a93ce8e5b6c..43bcaf140b5 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/MemberIndex.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/MemberIndex.kt @@ -41,7 +41,8 @@ object EMPTY_MEMBER_INDEX : MemberIndex { private val ADDITIONAL_MEMBER_NAMES_MAP = mapOf( FqName("java.util.List") to listOf(Name.identifier("removeAt")), - FqName("java.lang.CharSequence") to listOf(Name.identifier("get")) + FqName("java.lang.CharSequence") to listOf(Name.identifier("get")), + FqName("java.util.Map") to listOf(Name.identifier("keys"), Name.identifier("entries")) ) open class ClassMemberIndex(val jClass: JavaClass, val memberFilter: (JavaMember) -> Boolean) : MemberIndex { diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/specialBuiltinMembers.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/specialBuiltinMembers.kt index 9b9205ccd00..54323a7f754 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/specialBuiltinMembers.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/specialBuiltinMembers.kt @@ -35,7 +35,10 @@ object BuiltinSpecialProperties { private val PROPERTY_FQ_NAME_TO_JVM_GETTER_NAME_MAP = mapOf( FqName("kotlin.Collection.size") to Name.identifier("size"), FqName("kotlin.Map.size") to Name.identifier("size"), - FqName("kotlin.CharSequence.length") to Name.identifier("length") + FqName("kotlin.CharSequence.length") to Name.identifier("length"), + FqName("kotlin.Map.keys") to Name.identifier("keySet"), + FqName("kotlin.Map.values") to Name.identifier("values"), + FqName("kotlin.Map.entries") to Name.identifier("entrySet") ) private val GETTER_JVM_NAME_TO_PROPERTIES_SHORT_NAME_MAP: Map> = @@ -71,7 +74,12 @@ object BuiltinSpecialProperties { object BuiltinMethodsWithSpecialJvmSignature { private val ERASED_COLLECTION_PARAMETER_FQ_NAMES = setOf(FqName("kotlin.Collection.containsAll")) private val GENERIC_PARAMETERS_FQ_NAMES = setOf( - FqName("kotlin.Collection.contains"), FqName("kotlin.MutableCollection.remove") + FqName("kotlin.Collection.contains"), + FqName("kotlin.MutableCollection.remove"), + FqName("kotlin.Map.containsKey"), + FqName("kotlin.Map.containsValue"), + FqName("kotlin.Map.get"), + FqName("kotlin.MutableMap.remove") ) private val ERASED_VALUE_PARAMETERS_FQ_NAMES = @@ -121,7 +129,7 @@ object BuiltinSpecialMethods { val REMOVE_AT_FQ_NAME = FqName("kotlin.MutableList.removeAt") val FQ_NAMES_TO_JVM_MAP: Map = mapOf( - REMOVE_AT_FQ_NAME to Name.identifier("remove"), + REMOVE_AT_FQ_NAME to Name.identifier("remove"), FqName("kotlin.CharSequence.get") to Name.identifier("charAt") ) diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 8722d214825..80f6d39d1a5 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -105,18 +105,18 @@ public open class HashMap(initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, override val size: Int get() = noImpl override val isEmpty: Boolean get() = noImpl @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") - override fun get(key: Any?): V? = noImpl - override fun containsKey(key: Any?): Boolean = noImpl + override fun get(key: K): V? = noImpl + override fun containsKey(key: K): Boolean = noImpl @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") override fun put(key: K, value: V): V? = noImpl override fun putAll(m: Map): Unit = noImpl @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") - override fun remove(key: Any?): V? = noImpl + override fun remove(key: K): V? = noImpl override fun clear(): Unit = noImpl - override fun containsValue(value: Any?): Boolean = noImpl - override fun keySet(): MutableSet = noImpl - override fun values(): MutableCollection = noImpl - override fun entrySet(): MutableSet> = noImpl + override fun containsValue(value: V): Boolean = noImpl + override val keys: MutableSet get() = noImpl + override val values: MutableCollection get() = noImpl + override val entries: MutableSet> get() = noImpl } @library diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 32bc496c047..9bb7b44519b 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -66,12 +66,12 @@ private class MapWithDefaultImpl(public override val map: Map, pr override fun toString(): String = map.toString() override val size: Int get() = map.size() override val isEmpty: Boolean get() = map.isEmpty() - override fun containsKey(key: Any?): Boolean = map.containsKey(key) - override fun containsValue(value: Any?): Boolean = map.containsValue(value) - override fun get(key: Any?): V? = map.get(key) - override fun keySet(): Set = map.keySet() - override fun values(): Collection = map.values() - override fun entrySet(): Set> = map.entrySet() + override fun containsKey(key: K): Boolean = map.containsKey(key) + override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value) + override fun get(key: K): V? = map.get(key) + override val keys: Set get() = map.keys + override val values: Collection get() = map.values + override val entries: Set> get() = map.entries override fun getOrImplicitDefault(key: K): V = map.getOrElse(key, { default(key) }) } @@ -82,15 +82,15 @@ private class MutableMapWithDefaultImpl(public override val map: MutableMa override fun toString(): String = map.toString() override val size: Int get() = map.size() override val isEmpty: Boolean get() = map.isEmpty() - override fun containsKey(key: Any?): Boolean = map.containsKey(key) - override fun containsValue(value: Any?): Boolean = map.containsValue(value) - override fun get(key: Any?): V? = map.get(key) - override fun keySet(): MutableSet = map.keySet() - override fun values(): MutableCollection = map.values() - override fun entrySet(): MutableSet> = map.entrySet() + override fun containsKey(key: K): Boolean = map.containsKey(key) + override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value) + override fun get(key: K): V? = map.get(key) + override val keys: MutableSet get() = map.keys + override val values: MutableCollection get() = map.values + override val entries: MutableSet> get() = map.entries override fun put(key: K, value: V): V? = map.put(key, value) - override fun remove(key: Any?): V? = map.remove(key) + override fun remove(key: K): V? = map.remove(key) override fun putAll(m: Map) = map.putAll(m) override fun clear() = map.clear() diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 0872343a40c..47e87b72be3 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -14,12 +14,12 @@ private object EmptyMap : Map, Serializable { override val size: Int get() = 0 override val isEmpty: Boolean get() = true - override fun containsKey(key: Any?): Boolean = false - override fun containsValue(value: Any?): Boolean = false - override fun get(key: Any?): Nothing? = null - override fun entrySet(): Set> = EmptySet - override fun keySet(): Set = EmptySet - override fun values(): Collection = EmptyList + override fun containsKey(key: Any): Boolean = false + override fun containsValue(value: Nothing): Boolean = false + override fun get(key: Any): Nothing? = null + override val entries: Set> get() = EmptySet + override val keys: Set get() = EmptySet + override val values: Collection get() = EmptyList private fun readResolve(): Any = EmptyMap } diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 80e3a1e3bd0..ab6349eb5c1 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -34,6 +34,37 @@ public fun MutableCollection.remove(o: Any?): Boolean = remove(o as E) @Deprecated("Use 'length' property instead", ReplaceWith("this.length")) public fun CharSequence.length(): Int = length +@Deprecated("Use explicit cast to Map instead", ReplaceWith("(this as Map).get(o)")) +public inline operator fun Map.get(o: Any?): V? = get(o as K) + +@Deprecated("Use explicit cast to Map instead", ReplaceWith("(this as Map).containsKey(o)")) +public inline fun Map.containsKey(o: Any?): Boolean = containsKey(o as K) + +@Deprecated("Use explicit cast to Map instead", ReplaceWith("(this as Map).containsValue(o)")) +public inline fun Map.containsValue(o: Any?): Boolean = containsValue(o as V) + +@Deprecated("Use 'keys' instead", ReplaceWith("this.keys()")) +public inline fun Map.keySet(): Set = keys + +@kotlin.jvm.JvmName("mutableKeys") +@Deprecated("Use 'keys' instead", ReplaceWith("this.keys")) +public inline fun MutableMap.keySet(): MutableSet = keys + +@Deprecated("Use 'entries' instead", ReplaceWith("this.entries")) +public inline fun Map.entrySet(): Set> = entries + +@kotlin.jvm.JvmName("mutableEntrySet") +@Deprecated("Use 'entries' instead", ReplaceWith("this.entries")) +public inline fun MutableMap.entrySet(): MutableSet> = entries + +@Deprecated("Use 'values' properties instead", ReplaceWith("this.values")) +public inline fun Map.values(): Collection = values + +@kotlin.jvm.JvmName("mutableValues") +@Deprecated("Use 'values' properties instead", ReplaceWith("this.values")) +public inline fun MutableMap.values(): MutableCollection = values + + /** * Adds the specified [element] to this mutable collection. */