Nullability assertions for extension receiver

In Kotlin 1.1 and before, there were no nullability assertions on
extension receivers, because receiver is resolved with NO_EXPECTED_TYPE.
So, if an expression of platform type is passed as an extension receiver
to a non-private function, it would fail with IllegalArgumentException.
However, if the function is private, then we generated no parameter
assertions under assumption that such function can be called from Kotlin
only, and all arguments are checked on the call site. Thus 'null' could
propagate indefinitely.

In Kotlin 1.2, we do the following:
- Generate nullability assertions for expression receivers.
NB nullability assertions are stored for ReceiverValue instances, not
for expressions: given expression can act as receiver in different
calls, each with an expected receiver type of its own.
- Generate nullability assertions for extension receivers of private
operator functions.
NB it still can throw NPE for some particular "optimized" cases, but at
least those nulls would not propagate indefinitely.

This behavior is disabled by an "advanced" command-line option
'-Xno-receiver-assertions'.
This commit is contained in:
Dmitry Petrov
2017-07-21 17:42:18 +03:00
parent 2427b2cc6c
commit 5d44e095c8
26 changed files with 563 additions and 21 deletions
@@ -11592,6 +11592,81 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
}
}
@TestMetadata("compiler/testData/codegen/box/nullabilityAssertions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class NullabilityAssertions extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInNullabilityAssertions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/nullabilityAssertions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt")
public void testDestructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt");
doTest(fileName);
}
@TestMetadata("incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv11.kt")
public void testIncWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv11() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv11.kt");
doTest(fileName);
}
@TestMetadata("incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv12.kt")
public void testIncWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator_lv12.kt");
doTest(fileName);
}
@TestMetadata("incWithNullabilityAssertionOnExtensionReceiver_lv11.kt")
public void testIncWithNullabilityAssertionOnExtensionReceiver_lv11() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/incWithNullabilityAssertionOnExtensionReceiver_lv11.kt");
doTest(fileName);
}
@TestMetadata("incWithNullabilityAssertionOnExtensionReceiver_lv12.kt")
public void testIncWithNullabilityAssertionOnExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/incWithNullabilityAssertionOnExtensionReceiver_lv12.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnExtensionReceiver_lv11.kt")
public void testNullabilityAssertionOnExtensionReceiver_lv11() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnExtensionReceiver_lv11.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnExtensionReceiver_lv12.kt")
public void testNullabilityAssertionOnExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnExtensionReceiver_lv12.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnInlineFunExtensionReceiver_lv11.kt")
public void testNullabilityAssertionOnInlineFunExtensionReceiver_lv11() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnInlineFunExtensionReceiver_lv11.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnInlineFunExtensionReceiver_lv12.kt")
public void testNullabilityAssertionOnInlineFunExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnInlineFunExtensionReceiver_lv12.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnMemberExtensionReceiver_lv12.kt")
public void testNullabilityAssertionOnMemberExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnMemberExtensionReceiver_lv12.kt");
doTest(fileName);
}
@TestMetadata("nullabilityAssertionOnPrivateMemberExtensionReceiver_lv12.kt")
public void testNullabilityAssertionOnPrivateMemberExtensionReceiver_lv12() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/nullabilityAssertions/nullabilityAssertionOnPrivateMemberExtensionReceiver_lv12.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/objectIntrinsics")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)