Fix the case of safe call and elvis operators: flexible types may be nullable
This commit is contained in:
+3
-2
@@ -56,7 +56,8 @@ import java.util.Collections;
|
||||
|
||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||
import static org.jetbrains.jet.lang.resolve.calls.context.ContextDependency.INDEPENDENT;
|
||||
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiversPackage.*;
|
||||
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiversPackage.createQualifier;
|
||||
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiversPackage.resolveAsStandaloneExpression;
|
||||
import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
|
||||
|
||||
public class CallExpressionResolver {
|
||||
@@ -300,7 +301,7 @@ public class CallExpressionResolver {
|
||||
//TODO move further
|
||||
if (expression.getOperationSign() == JetTokens.SAFE_ACCESS) {
|
||||
if (selectorReturnType != null && !KotlinBuiltIns.getInstance().isUnit(selectorReturnType)) {
|
||||
if (receiverType.isNullable()) {
|
||||
if (TypeUtils.isNullableType(receiverType)) {
|
||||
selectorReturnType = TypeUtils.makeNullable(selectorReturnType);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
fun <T> test(t: T): String {
|
||||
fun <T> test(t: T): String? {
|
||||
if (t != null) {
|
||||
return t<!UNNECESSARY_SAFE_CALL!>?.<!>toString()
|
||||
}
|
||||
return t?.toString()
|
||||
}
|
||||
|
||||
fun <T> T.testThis(): String {
|
||||
fun <T> T.testThis(): String? {
|
||||
if (this != null) {
|
||||
return this<!UNNECESSARY_SAFE_CALL!>?.<!>toString()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// FILE: p/J.java
|
||||
|
||||
package p;
|
||||
|
||||
public class J {
|
||||
public String s() { return null; }
|
||||
}
|
||||
|
||||
// FILE: k.kt
|
||||
import p.*
|
||||
|
||||
fun test(j: J) {
|
||||
j.s()?.length ?: ""
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// !CHECK_TYPE
|
||||
// FILE: p/J.java
|
||||
|
||||
package p;
|
||||
|
||||
public class J {
|
||||
public String s() { return null; }
|
||||
}
|
||||
|
||||
// FILE: k.kt
|
||||
import p.*
|
||||
|
||||
fun test(j: J) {
|
||||
j.s()?.length.checkType { it : _<Int?> }
|
||||
}
|
||||
@@ -7697,7 +7697,20 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
|
||||
@TestMetadata("dereference.kt")
|
||||
public void testDereference() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/platformTypes/dereference.kt");
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/dereference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("elvis.kt")
|
||||
public void testElvis() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/elvis.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("safeCall.kt")
|
||||
public void testSafeCall() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/safeCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall")
|
||||
@@ -7722,22 +7735,26 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
|
||||
@TestMetadata("javaCollectionToKotlin.kt")
|
||||
public void testJavaCollectionToKotlin() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaCollectionToKotlin.kt");
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaCollectionToKotlin.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("javaToJava.kt")
|
||||
public void testJavaToJava() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToJava.kt");
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToJava.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("javaToKotlin.kt")
|
||||
public void testJavaToKotlin() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToKotlin.kt");
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToKotlin.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinCollectionToJava.kt")
|
||||
public void testKotlinCollectionToJava() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/kotlinCollectionToJava.kt");
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/kotlinCollectionToJava.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("list.kt")
|
||||
|
||||
@@ -423,6 +423,9 @@ public class TypeUtils {
|
||||
if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) {
|
||||
return hasNullableSuperType(type);
|
||||
}
|
||||
if (TypesPackage.isFlexible(type) && isNullableType(((FlexibleType) type).getUpperBound())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user