For-loop range expression (collection) can not be null

This commit is contained in:
Andrey Breslav
2012-08-24 15:36:55 +04:00
parent b315bee7b2
commit 66faa0c03a
24 changed files with 39 additions and 60 deletions
@@ -332,18 +332,6 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
Call iteratorCall = calls.getFirst();
OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResults = calls.getSecond();
// We allow the loop range to be null (nothing happens), so we make the receiver type non-null
if (!iteratorResolutionResults.isSuccess()) {
ExpressionReceiver nonNullReceiver = new ExpressionReceiver(loopRange.getExpression(), TypeUtils.makeNotNullable(loopRange.getType()));
Pair<Call,OverloadResolutionResults<FunctionDescriptor>> callsNonNull = makeAndResolveFakeCall(nonNullReceiver, context, iterator);
Call iteratorCallWithNonNullReceiver = callsNonNull.getFirst();
OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResultsWithNonNullReceiver = callsNonNull.getSecond();
if (iteratorResolutionResultsWithNonNullReceiver.isSuccess()) {
iteratorResolutionResults = iteratorResolutionResultsWithNonNullReceiver;
iteratorCall = iteratorCallWithNonNullReceiver;
}
}
if (iteratorResolutionResults.isSuccess()) {
ResolvedCall<FunctionDescriptor> iteratorResolvedCall = iteratorResolutionResults.getResultingCall();
context.trace.record(LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRangeExpression, iteratorResolvedCall);
@@ -12,10 +12,5 @@ fun box() : String {
System.out?.println(sum)
if(sum != 10) return "b failed"
val d : Array<Int?>? = null
for (el in d) {
sum = sum + (el ?: 7)
}
return "OK"
}
@@ -10,10 +10,5 @@ fun box() : String {
}
if(sum != 10) return "a failed"
val b : IntArray? = null
for (el in b) {
sum = sum + el
}
return "OK"
}
@@ -2,7 +2,7 @@ abstract class ClassValAbstract {
abstract var a: Int
class object {
val methods = (this as java.lang.Object).getClass()?.getClassLoader()?.loadClass("ClassValAbstract")?.getMethods()
val methods = (this as java.lang.Object).getClass()?.getClassLoader()?.loadClass("ClassValAbstract")?.getMethods()!!
}
}
@@ -11,7 +11,7 @@ class Test() {
fun box(): String {
val test = Test()
for (method in javaClass<Test>().getMethods()) {
for (method in javaClass<Test>().getMethods()!!) {
val anns = method?.getAnnotations() as Array<Annotation>
if (!anns.isEmpty()) {
for (ann in anns) {
@@ -17,7 +17,7 @@ fun <T> fff(x: T) : T { return x }
fun <T> id(value: T): T = value
fun foreach(array: Array<Int>?, action: (Int)-> Unit) {
fun foreach(array: Array<Int>, action: (Int)-> Unit) {
for (el in array) {
action(el) //exception through compilation (see below)
}
@@ -5,7 +5,7 @@ fun Int.plus(a: Int?) = this + a.sure()
public open class PerfectNumberFinder() {
open public fun isPerfect(number : Int) : Boolean {
var factors : List<Int?>? = ArrayList<Int?>()
var factors : List<Int?> = ArrayList<Int?>()
factors?.add(1)
factors?.add(number)
for (i in 2..(Math.sqrt((number).toDouble()) - 1).toInt())
@@ -0,0 +1,16 @@
class Coll {
fun iterator(): It = It()
}
class It {
fun next() = 1
fun hasNext() = false
}
fun test(c: Coll?) {
for (x in <!ITERATOR_MISSING!>c<!>) {}
if (c != null) {
for(x in c) {}
}
}
@@ -874,6 +874,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve", new File("compiler/testData/diagnostics/tests/controlStructures"), "kt", false);
}
@TestMetadata("forLoopWithNullableRange.kt")
public void testForLoopWithNullableRange() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlStructures/forLoopWithNullableRange.kt");
}
@TestMetadata("ForWithoutBraces.kt")
public void testForWithoutBraces() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlStructures/ForWithoutBraces.kt");
@@ -76,7 +76,7 @@ public inline fun <in T> Array<T>.plus(elements: Array<T>): List<T> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun <in T> Array<T?>?.requireNoNulls() : List<T> {
public inline fun <in T> Array<T?>.requireNoNulls() : List<T> {
val list = ArrayList<T>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun BooleanArray.plus(elements: BooleanArray): List<Boolean> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun BooleanArray?.requireNoNulls() : List<Boolean> {
public inline fun BooleanArray.requireNoNulls() : List<Boolean> {
val list = ArrayList<Boolean>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun ByteArray.plus(elements: ByteArray): List<Byte> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun ByteArray?.requireNoNulls() : List<Byte> {
public inline fun ByteArray.requireNoNulls() : List<Byte> {
val list = ArrayList<Byte>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun CharArray.plus(elements: CharArray): List<Char> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun CharArray?.requireNoNulls() : List<Char> {
public inline fun CharArray.requireNoNulls() : List<Char> {
val list = ArrayList<Char>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun DoubleArray.plus(elements: DoubleArray): List<Double> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun DoubleArray?.requireNoNulls() : List<Double> {
public inline fun DoubleArray.requireNoNulls() : List<Double> {
val list = ArrayList<Double>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun FloatArray.plus(elements: FloatArray): List<Float> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun FloatArray?.requireNoNulls() : List<Float> {
public inline fun FloatArray.requireNoNulls() : List<Float> {
val list = ArrayList<Float>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun IntArray.plus(elements: IntArray): List<Int> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun IntArray?.requireNoNulls() : List<Int> {
public inline fun IntArray.requireNoNulls() : List<Int> {
val list = ArrayList<Int>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun LongArray.plus(elements: LongArray): List<Long> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun LongArray?.requireNoNulls() : List<Long> {
public inline fun LongArray.requireNoNulls() : List<Long> {
val list = ArrayList<Long>()
for (element in this) {
if (element == null) {
@@ -76,7 +76,7 @@ public inline fun ShortArray.plus(elements: ShortArray): List<Short> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun ShortArray?.requireNoNulls() : List<Short> {
public inline fun ShortArray.requireNoNulls() : List<Short> {
val list = ArrayList<Short>()
for (element in this) {
if (element == null) {
@@ -67,7 +67,7 @@ public inline fun <in T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
*
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
*/
public inline fun <in T> Iterable<T?>?.requireNoNulls() : List<T> {
public inline fun <in T> Iterable<T?>.requireNoNulls() : List<T> {
val list = ArrayList<T>()
for (element in this) {
if (element == null) {
+1 -1
View File
@@ -14,7 +14,7 @@ import java.net.URL
public fun File.recurse(block: (File) -> Unit): Unit {
block(this)
if (this.isDirectory()) {
for (child in this.listFiles()) {
for (child in this.listFiles()!!) {
if (child != null) {
child.recurse(block)
}
@@ -62,7 +62,7 @@ public abstract class ChangeSupport {
}
}
if (allListeners != null) {
for (listener in allListeners) {
for (listener in allListeners!!) {
listener.onPropertyChange(event)
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ public var asserter: Asserter
get() {
if (_asserter == null) {
val klass = javaClass<Asserter>()
val loader = ServiceLoader.load(klass)
val loader = ServiceLoader.load(klass)!!
for (a in loader) {
if (a != null) {
_asserter = a
@@ -1,20 +0,0 @@
package test.language
import junit.framework.TestCase
import java.util.Collection
import kotlin.test.*
class NullableCollectionsTest : TestCase() {
fun testIterateOverNullCollectionsThrowsNPE() {
val c: Collection<String>? = null
// TODO currently this will throw a NPE
// should it either be a compile error or handle nulls gracefully?
failsWith<NullPointerException> {
for (e in c) {
println("Hey got $e")
}
}
}
}
@@ -128,7 +128,7 @@ import js.noImpl
}
}
for (pk in properties.values()) {
for (pk in properties.values()!!) {
// some properties might not have a getter defined
// so lets ignore those