Files
kotlin/compiler/testData/diagnostics/testsWithStdLib/purelyImplementedCollection/mapsWithNullableKey.kt
Denis Zharkov 8b49a1d660 Add PurelyImplements annotation
It's parameter is FQ-name of class (currently only from builtins) that added as supertype to annotated Java class.

Parameters of annotated class used as non-flexible arguments of added supertype, that helps to propagate more precise types when using in Kotlin.
Some standard JDK collections loaded as they annotated with PurelyImplements.

See tests for clarification.
Before: ArrayList<Int>.add(x: Int!) // possible to add null
After: ArrayList<Int>.add(x: Int)   // impossible to add null

 #KT-7628 Fixed
 #KT-7835 Fixed
2015-07-09 16:36:47 +03:00

55 lines
1.5 KiB
Kotlin
Vendored

// !DIAGNOSTICS: -UNUSED_VARIABLE
import java.util.*
fun bar(): String? = null
val nullableInt: Int? = null
fun hashMapTest() {
var x: HashMap<String?, Int> = HashMap<String?, Int>()
x.put(null, <!NULL_FOR_NONNULL_TYPE!>null<!>)
x.put("", <!NULL_FOR_NONNULL_TYPE!>null<!>)
x.put(bar(), 1)
x.put("", 1)
x[null] = 1
x[bar()] = 1
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>x[""]<!> = nullableInt
x[""] = 1
val b1: MutableMap<String?, Int?> = <!TYPE_MISMATCH!>x<!>
val b2: MutableMap<String?, Int> = x
val b3: Map<String?, Int> = x
val b4: Map<String?, Int?> = x
val b5: Map<String, Int?> = <!TYPE_MISMATCH!>x<!>
val b6: Int = <!TYPE_MISMATCH!>x[""]<!>
val b7: Int = <!TYPE_MISMATCH!>x[null]<!>
val b8: Int = <!TYPE_MISMATCH!>x.get("")<!>
val b9: Int? = x.get("")
}
fun treeMapTest() {
var x: TreeMap<String?, Int> = TreeMap<String?, Int>()
x.put(null, <!NULL_FOR_NONNULL_TYPE!>null<!>)
x.put("", <!NULL_FOR_NONNULL_TYPE!>null<!>)
x.put(bar(), 1)
x.put("", 1)
x[null] = 1
x[bar()] = 1
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>x[""]<!> = nullableInt
x[""] = 1
val b1: MutableMap<String?, Int?> = <!TYPE_MISMATCH!>x<!>
val b2: MutableMap<String?, Int> = x
val b3: Map<String?, Int> = x
val b4: Map<String?, Int?> = x
val b5: Map<String, Int?> = <!TYPE_MISMATCH!>x<!>
val b6: Int = <!TYPE_MISMATCH!>x[""]<!>
val b7: Int = <!TYPE_MISMATCH!>x.get("")<!>
val b8: Int? = x.get("")
}