Introduce PureReifiable annotation

It prevents reporting unsafe substitution warning on expressions
like 'arrayOf(arrayOf(""))'
This commit is contained in:
Denis Zharkov
2016-01-21 19:09:46 +03:00
parent 32755a269c
commit 6c0cd70a22
16 changed files with 137 additions and 21 deletions
+3 -1
View File
@@ -16,6 +16,8 @@
package kotlin
import kotlin.internal.PureReifiable
/**
* Returns a string representation of the object. Can be called with a null receiver, in which case
* it returns the string "null".
@@ -31,4 +33,4 @@ public operator fun String?.plus(other: Any?): String
/**
* Returns an array of objects of the given type with the given [size], initialized with null values.
*/
public fun arrayOfNulls<reified T>(size: Int): Array<T?>
public fun <reified @PureReifiable T> arrayOfNulls(size: Int): Array<T?>
+5 -3
View File
@@ -16,11 +16,13 @@
package kotlin
import kotlin.internal.PureReifiable
/**
* Returns an array with the specified [size], where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
public inline fun <reified @PureReifiable T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
for (i in 0..size - 1)
result[i] = init(i)
@@ -126,14 +128,14 @@ public inline fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray
/**
* Returns an empty array of the specified type [T].
*/
public inline fun <reified T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
public inline fun <reified @PureReifiable T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
// Array "constructor"
/**
* Returns an array containing the specified elements.
*/
public inline fun <reified T> arrayOf(vararg elements: T) : Array<T> = elements as Array<T>
public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T) : Array<T> = elements as Array<T>
// "constructors" for primitive types array
/**
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.internal
/**
* Specifies that the corresponding type parameter is not used for unsafe operations such as casts or 'is' checks
* That means it's completely safe to use generic types as argument for such parameter.
*/
@Target(AnnotationTarget.TYPE_PARAMETER)
@Retention(AnnotationRetention.BINARY)
internal annotation class PureReifiable
@@ -58,7 +58,8 @@ public abstract class KotlinBuiltIns {
COLLECTIONS_PACKAGE_FQ_NAME,
RANGES_PACKAGE_FQ_NAME,
ANNOTATION_PACKAGE_FQ_NAME,
ReflectionTypesKt.getKOTLIN_REFLECT_FQ_NAME()
ReflectionTypesKt.getKOTLIN_REFLECT_FQ_NAME(),
BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("internal"))
);
protected final ModuleDescriptorImpl builtInsModule;