From 0149e8048c374a97f278b3a3725d307660fddf03 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Tue, 9 Dec 2014 18:46:50 +0300 Subject: [PATCH] Capture notnullable type if type variable is nullable --- .../capturedTypes/captureForNullableTypes.kt | 14 ++++---------- .../capturedTypes/captureForNullableTypes.txt | 3 +-- .../captureFromNullableTypeVariable.kt | 13 +++++++++++++ .../captureFromNullableTypeVariable.txt | 5 +++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ .../calls/inference/ConstraintSystemImpl.kt | 8 +++++++- 6 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.kt create mode 100644 compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.txt diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.kt index fa6156ae15a..32c88fd9626 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.kt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.kt @@ -3,10 +3,10 @@ fun bar(a: Array): Array = null!! -fun test1(a: Array) { - val r: Array = bar(a) +fun test1(a: Array) { + val r: Array = bar(a) val t = bar(a) - t checkType { it : _> } + t checkType { it : _> } } fun foo(l: Array): Array> = null!! @@ -15,10 +15,4 @@ fun test2(a: Array) { val r: Array> = foo(a) val t = foo(a) t checkType { it : _>> } -} - -fun test3(a: Array) { - val r: Array> = foo(a) - val t = foo(a) - t checkType { it : _>> } -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.txt b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.txt index 02493a2504f..a05fa5ebcad 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.txt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureForNullableTypes.txt @@ -2,6 +2,5 @@ package internal fun bar(/*0*/ a: kotlin.Array): kotlin.Array internal fun foo(/*0*/ l: kotlin.Array): kotlin.Array> -internal fun test1(/*0*/ a: kotlin.Array): kotlin.Unit +internal fun test1(/*0*/ a: kotlin.Array): kotlin.Unit internal fun test2(/*0*/ a: kotlin.Array): kotlin.Unit -internal fun test3(/*0*/ a: kotlin.Array): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.kt new file mode 100644 index 00000000000..90bfdf8b7b3 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.kt @@ -0,0 +1,13 @@ +// !CHECK_TYPE + +fun Array.filterNotNull(): List = throw Exception() + +fun test1(a: Array) { + val list = a.filterNotNull() + list checkType { it : _> } +} + +fun test2(vararg a: Int?) { + val list = a.filterNotNull() + list checkType { it : _> } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.txt b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.txt new file mode 100644 index 00000000000..e1c97e937e3 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.txt @@ -0,0 +1,5 @@ +package + +internal fun test1(/*0*/ a: kotlin.Array): kotlin.Unit +internal fun test2(/*0*/ vararg a: kotlin.Int? /*kotlin.Array*/): kotlin.Unit +internal fun kotlin.Array.filterNotNull(): kotlin.List diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 29fd9e0827e..049ce363015 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -5106,6 +5106,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("captureFromNullableTypeVariable.kt") + public void testCaptureFromNullableTypeVariable() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/captureFromNullableTypeVariable.kt"); + doTest(fileName); + } + @TestMetadata("captureTypeOnlyOnTopLevel.kt") public void testCaptureTypeOnlyOnTopLevel() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/captureTypeOnlyOnTopLevel.kt"); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt index 0dda4bcd93b..a457e45963f 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt @@ -380,7 +380,13 @@ public class ConstraintSystemImpl : ConstraintSystem { constraintPosition: ConstraintPosition ) { val typeBounds = getTypeBounds(parameterType) - val capturedType = createCapturedType(constrainingTypeProjection) + val typeProjection = if (parameterType.isMarkedNullable()) { + TypeProjectionImpl(constrainingTypeProjection.getProjectionKind(), TypeUtils.makeNotNullable(constrainingTypeProjection.getType())) + } + else { + constrainingTypeProjection + } + val capturedType = createCapturedType(typeProjection) typeBounds.addBound(EXACT_BOUND, capturedType, constraintPosition) }