x?.y is now nullable even if y is Unit + three tests + codegen test fix + source code fix #KT-7936 Fixed #KT-8347 Fixed

This commit is contained in:
Mikhail Glukhikh
2015-08-04 15:35:15 +03:00
parent 850580dc87
commit 5b31560808
11 changed files with 80 additions and 3 deletions
@@ -373,7 +373,7 @@ public class CallExpressionResolver {
//TODO move further
if (safeCall) {
if (selectorReturnType != null && !KotlinBuiltIns.isUnit(selectorReturnType)) {
if (selectorReturnType != null) {
if (TypeUtils.isNullableType(receiverType)) {
selectorReturnType = TypeUtils.makeNullable(selectorReturnType);
selectorReturnTypeInfo = selectorReturnTypeInfo.replaceType(selectorReturnType);
+3 -1
View File
@@ -15,7 +15,9 @@ interface ChannelPipeline {
}
class DefaultChannelPipeline : ChannelPipeline {
override fun print(any: Any) = System.out?.println(any)
override fun print(any: Any) {
System.out?.println(any)
}
}
@@ -0,0 +1,11 @@
class My {
fun other() {}
}
fun another() {}
fun foo(x: My?) {
// Both elvis parts should be alive
// See also KT-7936, KT-8347
x?.other() ?: another()
}
@@ -0,0 +1,12 @@
package
internal fun another(): kotlin.Unit
internal fun foo(/*0*/ x: My?): kotlin.Unit
internal final class My {
public constructor My()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal final fun other(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,7 @@
data class My(val x: Unit)
fun foo(my: My?): Int? {
val x = my?.x
// ?. is required here
return x?.hashCode()
}
@@ -0,0 +1,13 @@
package
internal fun foo(/*0*/ my: My?): kotlin.Int?
kotlin.data() internal final class My {
public constructor My(/*0*/ x: kotlin.Unit)
internal final val x: kotlin.Unit
internal final /*synthesized*/ fun component1(): kotlin.Unit
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Unit = ...): My
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,9 @@
fun foo(x: Int?) {
// Both parts of the Elvis should be alive, see KT-7936
x?.let {
smth()
}?: orElse()
}
fun smth() {}
fun orElse() {}
@@ -0,0 +1,5 @@
package
internal fun foo(/*0*/ x: kotlin.Int?): kotlin.Unit
internal fun orElse(): kotlin.Unit
internal fun smth(): kotlin.Unit
@@ -9417,6 +9417,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("elvisOnUnit.kt")
public void testElvisOnUnit() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullableTypes/elvisOnUnit.kt");
doTest(fileName);
}
@TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt")
public void testNullAssertOnTypeWithNullableUpperBound() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt");
@@ -9441,6 +9447,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("safeAccessOnUnit.kt")
public void testSafeAccessOnUnit() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullableTypes/safeAccessOnUnit.kt");
doTest(fileName);
}
@TestMetadata("safeCallOnTypeWithNullableUpperBound.kt")
public void testSafeCallOnTypeWithNullableUpperBound() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullableTypes/safeCallOnTypeWithNullableUpperBound.kt");
@@ -35,6 +35,12 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("elvisOnUnitInLet.kt")
public void testElvisOnUnitInLet() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/elvisOnUnitInLet.kt");
doTest(fileName);
}
@TestMetadata("instar.kt")
public void testInstar() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/instar.kt");
@@ -59,7 +59,7 @@ class MigrateAnnotationMethodCallInWholeFile : IntentionAction {
forEach {
(it.getPsiElement() as? JetCallExpression)?.let { MigrateAnnotationMethodCallFix.replaceWithSimpleCall(it) }
}
}
} ?: Unit
companion object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = MigrateAnnotationMethodCallInWholeFile()