From 9da229896bdd99d5871058c9c6a90da856ec8b68 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 17 Apr 2017 19:04:10 +0700 Subject: [PATCH] stdlib: Fix variance for map and collection extensions --- .../kotlin/kotlin/collections/Collections.kt | 9 -------- .../main/kotlin/kotlin/collections/HashMap.kt | 2 +- .../main/kotlin/kotlin/collections/Maps.kt | 22 +++++++++---------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index 2ca9cc0da72..049022ec5d5 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -309,15 +309,6 @@ public fun CharArray.asList(): List { } } - -@FixmeVariance -public fun > Iterable.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - @Fixme internal fun List.optimizeReadOnlyList() = this diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index 9669bb6e0fb..f3a0fbdf6eb 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -45,7 +45,7 @@ class HashMap private constructor( INITIAL_MAX_PROBE_DISTANCE, 0) - constructor(m: Map) : this(m.size) { + constructor(m: Map) : this(m.size) { putAll(m) } diff --git a/runtime/src/main/kotlin/kotlin/collections/Maps.kt b/runtime/src/main/kotlin/kotlin/collections/Maps.kt index 6109d7e863f..7f4b89f7a04 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Maps.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Maps.kt @@ -487,7 +487,8 @@ public fun > Sequence>.toMap(destina * * The returned map preserves the entry iteration order of the original map. */ -public fun Map.toMap(): Map = when (size) { +@SinceKotlin("1.1") +public fun Map.toMap(): Map = when (size) { 0 -> emptyMap() // 1 -> toSingletonMap() else -> toMutableMap() @@ -498,7 +499,8 @@ public fun Map.toMap(): Map = when (size) { * * The returned map preserves the entry iteration order of the original map. */ -public fun Map.toMutableMap(): MutableMap = HashMap(this) +@SinceKotlin("1.1") +public fun Map.toMutableMap(): MutableMap = HashMap(this) /** * Populates and returns the [destination] mutable map with key-value pairs from the given map. @@ -512,9 +514,8 @@ public fun > Map.toMap(destination: M * The returned map preserves the entry iteration order of the original map. * The [pair] is iterated in the end if it has a unique key. */ -@FixmeVariance -public operator fun Map.plus(pair: Pair): Map - = if (this.isEmpty()) mapOf(pair) else HashMap(this).apply { put(pair.first, pair.second) } +public operator fun Map.plus(pair: Pair): Map + = if (this.isEmpty()) mapOf(pair) else HashMap(this).apply { put(pair.first, pair.second) } /** * Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs]. @@ -522,8 +523,7 @@ public operator fun Map.plus(pair: Pair): Map * The returned map preserves the entry iteration order of the original map. * Those [pairs] with unique keys are iterated in the end in the order of [pairs] collection. */ -@FixmeVariance -public operator fun Map.plus(pairs: Iterable>): Map +public operator fun Map.plus(pairs: Iterable>): Map = if (this.isEmpty()) pairs.toMap() else HashMap(this).apply { putAll(pairs) } /** @@ -532,8 +532,7 @@ public operator fun Map.plus(pairs: Iterable>): * The returned map preserves the entry iteration order of the original map. * Those [pairs] with unique keys are iterated in the end in the order of [pairs] array. */ -@FixmeVariance -public operator fun Map.plus(pairs: Array>): Map +public operator fun Map.plus(pairs: Array>): Map = if (this.isEmpty()) pairs.toMap() else HashMap(this).apply { putAll(pairs) } /** @@ -542,7 +541,7 @@ public operator fun Map.plus(pairs: Array>): * The returned map preserves the entry iteration order of the original map. * Those [pairs] with unique keys are iterated in the end in the order of [pairs] sequence. */ -public operator fun Map.plus(pairs: Sequence>): Map +public operator fun Map.plus(pairs: Sequence>): Map = HashMap(this).apply { putAll(pairs) }.optimizeReadOnlyMap() /** @@ -551,8 +550,7 @@ public operator fun Map.plus(pairs: Sequence>): * The returned map preserves the entry iteration order of the original map. * Those entries of another [map] that are missing in this map are iterated in the end in the order of that [map]. */ -@FixmeVariance -public operator fun Map.plus(map: Map): Map +public operator fun Map.plus(map: Map): Map = HashMap(this).apply { putAll(map) } /**