[FE 1.0] Postpone SafeCallsAreAlwaysNullable till 1.7

^KT-46860 Fixed
This commit is contained in:
Dmitriy Novozhilov
2021-09-01 10:13:45 +03:00
committed by teamcityserver
parent 5a5d25d350
commit 1e0878cde0
14 changed files with 96 additions and 51 deletions
@@ -3997,12 +3997,6 @@ public class FirDiagnosticTestSpecGenerated extends AbstractFirDiagnosticTestSpe
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.1.kt");
}
@Test
@TestMetadata("2.10.kt")
public void test2_10() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt");
}
@Test
@TestMetadata("2.2.kt")
public void test2_2() throws Exception {
@@ -4067,6 +4061,12 @@ public class FirDiagnosticTestSpecGenerated extends AbstractFirDiagnosticTestSpe
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.2.kt");
}
@Test
@TestMetadata("2.3.kt")
public void test2_3() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt");
}
@Test
public void testAllFilesPresentInPos() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
@@ -711,7 +711,7 @@ public class DefaultErrorMessages {
MAP.put(EXCEPTION_FROM_ANALYZER, "Internal Error occurred while analyzing this expression:\n{0}", THROWABLE);
MAP.put(MISSING_STDLIB, "{0}. Ensure you have the standard Kotlin library in dependencies", STRING);
MAP.put(UNNECESSARY_SAFE_CALL, "Unnecessary safe call on a non-null receiver of type {0}", RENDER_TYPE);
MAP.put(UNNECESSARY_SAFE_CALL, "Unnecessary safe call on a non-null receiver of type {0}. This expression will have nullable type in future releases", RENDER_TYPE);
MAP.put(UNEXPECTED_SAFE_CALL, "Safe-call is not allowed here");
MAP.put(UNNECESSARY_NOT_NULL_ASSERTION, "Unnecessary non-null assertion (!!) on a non-null receiver of type {0}", RENDER_TYPE);
MAP.put(NOT_NULL_ASSERTION_ON_LAMBDA_EXPRESSION, "Non-null assertion (!!) is called on a lambda expression");
@@ -3,4 +3,4 @@ fun test(s: String) = s?.length
// 0 IFNULL
// 0 IFNONNULL
// 0 intValue
// 1 valueOf
// 0 valueOf
@@ -12,5 +12,5 @@ val s: String = "test"
fun ff() {
val a = <!NO_COMPANION_OBJECT!>Test<!><!UNEXPECTED_SAFE_CALL!>?.<!>FOO
val b = <!EXPRESSION_EXPECTED_PACKAGE_FOUND!>foo<!><!UNEXPECTED_SAFE_CALL!>?.<!>s
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!>out<!UNSAFE_CALL!>.<!>println(a + b)
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!>out.println(a + b)
}
@@ -0,0 +1,46 @@
// !CHECK_TYPE
// Incorrect "type mismatch" error for generic extension safe call (required not-null, found nullable)
// FILE: B.java
public class B<T> {
public String gav() {
return "";
}
public static <T> B<T> create() {
return new B();
}
}
// FILE: A.kt
class A<T> {
fun gav() = ""
}
fun <R> foo(x: R) = x
fun <T> A<T>.bar() = ""
fun <T> B<T>.bar() = ""
fun foo(l: A<String>?) {
// No errors should be here
foo(l?.bar()) checkType { _<String?>() }
foo(l?.gav()) checkType { _<String?>() }
if (l != null) {
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
}
}
fun fooNotNull(l: A<String>) {
// No errors should be here
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
}
fun bar() {
val l = B.create<String>()
foo(l?.bar()) checkType { _<String?>() }
foo(l?.gav()) checkType { _<String?>() }
}
+4 -5
View File
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !CHECK_TYPE
// Incorrect "type mismatch" error for generic extension safe call (required not-null, found nullable)
@@ -28,15 +27,15 @@ fun foo(l: A<String>?) {
foo(l?.bar()) checkType { _<String?>() }
foo(l?.gav()) checkType { _<String?>() }
if (l != null) {
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { _<String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { _<String>() }
}
}
fun fooNotNull(l: A<String>) {
// No errors should be here
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>bar()) checkType { _<String>() }
foo(l<!UNNECESSARY_SAFE_CALL!>?.<!>gav()) checkType { _<String>() }
}
fun bar() {
@@ -11,7 +11,7 @@ interface B {
fun test(u: A?, x: A?, y: A?, z: A?, w: A, v: A?) {
u?.b?.foo()!! // was UNNECESSARY_SAFE_CALL everywhere, because result type (of 'foo()') wasn't made nullable
u!!.b<!UNNECESSARY_SAFE_CALL!>?.<!>foo()!!
u!!.b<!UNNECESSARY_SAFE_CALL!>?.<!>foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
x?.b!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
// x?.b is not null
x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.b<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
@@ -22,7 +22,7 @@ fun test(u: A?, x: A?, y: A?, z: A?, w: A, v: A?) {
// z?.nb is not null
z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.nb!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.b<!UNNECESSARY_SAFE_CALL!>?.<!>foo()!!
w.b<!UNNECESSARY_SAFE_CALL!>?.<!>foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.b<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.nb?.foo()!!
w.nb!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
@@ -52,19 +52,19 @@ fun foo(a: A?) {
a?.w.inc()
if (a != null) {
a<!UNNECESSARY_SAFE_CALL!>?.<!>l <!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>+=<!> 1
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0]<!>
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER, NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0]<!>++
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0]<!> = 1
a<!UNNECESSARY_SAFE_CALL!>?.<!>l += 1
a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0]
a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0]++
a<!UNNECESSARY_SAFE_CALL!>?.<!>l[0] = 1
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0]<!>[0]
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0]<!>[0]++
<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0]<!>[0] = 1
a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0][0]
a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0][0]++
a<!UNNECESSARY_SAFE_CALL!>?.<!>ll[0][0] = 1
// No warning is reported because
// 1. All kinds of green code with safe+call + invoke we identified fails with CCE if `a != null`, anyway
// 2. In case of null value, the behavior is intended (no call performed)
a<!UNNECESSARY_SAFE_CALL!>?.<!>q()
a<!UNNECESSARY_SAFE_CALL!>?.<!>w<!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>++<!>
a<!UNNECESSARY_SAFE_CALL!>?.<!>w++
(a<!UNNECESSARY_SAFE_CALL!>?.<!>l) += 1
(a<!UNNECESSARY_SAFE_CALL!>?.<!>l)[0]
@@ -8,5 +8,5 @@ fun ff() {
val a = Test.FOO
val b = <!NO_COMPANION_OBJECT!>Test<!><!UNEXPECTED_SAFE_CALL!>?.<!>FOO
System.out.println(a + b)
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!>out<!UNSAFE_CALL!>.<!>println(a + b)
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!>out.println(a + b)
}
@@ -9,17 +9,17 @@ package testPackCase1
fun case1(a: A, c: C) {
<!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> c<!>
a?.b <!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>plusAssign(c)<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += c<!>
a?.b .<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
val x = {
<!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> c<!>
a?.b<!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>plusAssign(c)<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += c<!>
a?.b.<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
}()
<!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> { c }()<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += { c }()<!>
a?.b<!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: fqName is unknown; typeCall: unresolved")!>plusAssign({ c }())<!>
a?.b.<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign({ c }())<!>
}
class A(val b: B)
@@ -3,14 +3,14 @@
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-448
* MAIN LINK: overload-resolution, building-the-overload-candidate-set-ocs, operator-call -> paragraph 1 -> sentence 2
* SECONDARY LINKS: statements, assignments, operator-assignments -> paragraph 2 -> sentence 1
* statements, assignments, operator-assignments -> paragraph 2 -> sentence 2
* statements, assignments, operator-assignments -> paragraph 2 -> sentence 3
* NUMBER: 10
* NUMBER: 3
* DESCRIPTION: Non-extension member callables
*/
@@ -20,17 +20,17 @@ package testPackCase1
fun case1(a: A, c: C) {
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> c<!>
a?.b <!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += c<!>
a?.b .<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
val x = {
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> c<!>
a?.b<!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += c<!>
a?.b.<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign(c)<!>
}()
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b <!UNSAFE_OPERATOR_CALL!>+=<!> { c }()<!>
<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>a?.b += { c }()<!>
a?.b<!UNSAFE_CALL!>.<!><!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign({ c }())<!>
a?.b.<!DEBUG_INFO_CALL("fqName: testPackCase1.B.plusAssign; typeCall: operator function")!>plusAssign({ c }())<!>
}
class A(val b: B)
@@ -4361,11 +4361,6 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.1.kt");
}
@TestMetadata("2.10.kt")
public void test2_10() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt");
}
@TestMetadata("2.2.kt")
public void test2_2() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.2.kt");
@@ -4424,6 +4419,11 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.2.kt");
}
@TestMetadata("2.3.kt")
public void test2_3() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt");
}
public void testAllFilesPresentInPos() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@@ -209,7 +209,6 @@ enum class LanguageFeature(
UnrestrictedBuilderInference(KOTLIN_1_6),
ProperTypeInferenceConstraintsProcessing(KOTLIN_1_6, kind = BUG_FIX),
ClassTypeParameterAnnotations(KOTLIN_1_6),
SafeCallsAreAlwaysNullable(KOTLIN_1_6),
TypeInferenceOnCallsWithSelfTypes(KOTLIN_1_6),
WarnAboutNonExhaustiveWhenOnAlgebraicTypes(KOTLIN_1_6, kind = BUG_FIX),
InstantiationOfAnnotationClasses(KOTLIN_1_6),
@@ -230,6 +229,7 @@ enum class LanguageFeature(
DefinitelyNotNullTypeParameters(KOTLIN_1_7),
DefinitelyNonNullableTypes(KOTLIN_1_7),
ProhibitSimplificationOfNonTrivialConstBooleanExpressions(KOTLIN_1_7),
SafeCallsAreAlwaysNullable(KOTLIN_1_7),
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),
@@ -3997,12 +3997,6 @@ public class FirIdeSpecTest extends AbstractDiagnosisCompilerTestDataSpecTest {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.1.kt");
}
@Test
@TestMetadata("2.10.kt")
public void test2_10() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt");
}
@Test
@TestMetadata("2.2.kt")
public void test2_2() throws Exception {
@@ -4067,6 +4061,12 @@ public class FirIdeSpecTest extends AbstractDiagnosisCompilerTestDataSpecTest {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.2.kt");
}
@Test
@TestMetadata("2.3.kt")
public void test2_3() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt");
}
@Test
public void testAllFilesPresentInPos() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);