diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestSpecGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestSpecGenerated.java index 2abfe090381..ca5d14810a7 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestSpecGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestSpecGenerated.java @@ -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); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 89d33b6d296..00cd59b24e5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -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"); diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt index 1d7c468771f..5a1fe4989db 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt @@ -3,4 +3,4 @@ fun test(s: String) = s?.length // 0 IFNULL // 0 IFNONNULL // 0 intValue -// 1 valueOf +// 0 valueOf diff --git a/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.kt b/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.kt index e4d456dc634..3e2a82a1317 100644 --- a/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.kt +++ b/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.kt @@ -12,5 +12,5 @@ val s: String = "test" fun ff() { val a = Test?.FOO val b = foo?.s - System?.out.println(a + b) + System?.out.println(a + b) } diff --git a/compiler/testData/diagnostics/tests/generics/kt9985.fir.kt b/compiler/testData/diagnostics/tests/generics/kt9985.fir.kt new file mode 100644 index 00000000000..fbf95708cd5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/kt9985.fir.kt @@ -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 { + public String gav() { + return ""; + } + + public static B create() { + return new B(); + } +} + +// FILE: A.kt + +class A { + fun gav() = "" +} +fun foo(x: R) = x +fun A.bar() = "" +fun B.bar() = "" + +fun foo(l: A?) { + // No errors should be here + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } + if (l != null) { + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } + } +} + +fun fooNotNull(l: A) { + // No errors should be here + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } +} + +fun bar() { + val l = B.create() + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } +} + diff --git a/compiler/testData/diagnostics/tests/generics/kt9985.kt b/compiler/testData/diagnostics/tests/generics/kt9985.kt index 1401dad777f..73f71d9693d 100644 --- a/compiler/testData/diagnostics/tests/generics/kt9985.kt +++ b/compiler/testData/diagnostics/tests/generics/kt9985.kt @@ -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?) { foo(l?.bar()) checkType { _() } foo(l?.gav()) checkType { _() } if (l != null) { - foo(l?.bar()) checkType { _() } - foo(l?.gav()) checkType { _() } + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } } } fun fooNotNull(l: A) { // No errors should be here - foo(l?.bar()) checkType { _() } - foo(l?.gav()) checkType { _() } + foo(l?.bar()) checkType { _() } + foo(l?.gav()) checkType { _() } } fun bar() { diff --git a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt b/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt index b8ad510b483..1926dacd36d 100644 --- a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt @@ -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?.foo()!! + u!!.b?.foo()!! x?.b!!.foo()!! // x?.b is not null x!!.b!!.foo()!! @@ -22,7 +22,7 @@ fun test(u: A?, x: A?, y: A?, z: A?, w: A, v: A?) { // z?.nb is not null z!!.nb!!.foo()!! - w.b?.foo()!! + w.b?.foo()!! w.b!!.foo()!! w.nb?.foo()!! w.nb!!.foo()!! diff --git a/compiler/testData/diagnostics/tests/nullableTypes/safeCallOperators.kt b/compiler/testData/diagnostics/tests/nullableTypes/safeCallOperators.kt index 03c2bfb7453..2de57b312cc 100644 --- a/compiler/testData/diagnostics/tests/nullableTypes/safeCallOperators.kt +++ b/compiler/testData/diagnostics/tests/nullableTypes/safeCallOperators.kt @@ -52,19 +52,19 @@ fun foo(a: A?) { a?.w.inc() if (a != null) { - a?.l += 1 - a?.l[0] - a?.l[0]++ - a?.l[0] = 1 + a?.l += 1 + a?.l[0] + a?.l[0]++ + a?.l[0] = 1 - a?.ll[0][0] - a?.ll[0][0]++ - a?.ll[0][0] = 1 + a?.ll[0][0] + a?.ll[0][0]++ + a?.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?.q() - a?.w++ + a?.w++ (a?.l) += 1 (a?.l)[0] diff --git a/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.kt b/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.kt index cd512b70b79..f1532277731 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.kt @@ -8,5 +8,5 @@ fun ff() { val a = Test.FOO val b = Test?.FOO System.out.println(a + b) - System?.out.println(a + b) + System?.out.println(a + b) } diff --git a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.fir.kt similarity index 79% rename from compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.fir.kt rename to compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.fir.kt index 181f150f6eb..55a95cd8b2b 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.fir.kt @@ -9,17 +9,17 @@ package testPackCase1 fun case1(a: A, c: C) { - a?.b += c - a?.b .plusAssign(c) + a?.b += c + a?.b .plusAssign(c) val x = { - a?.b += c - a?.b.plusAssign(c) + a?.b += c + a?.b.plusAssign(c) }() - a?.b += { c }() + a?.b += { c }() - a?.b.plusAssign({ c }()) + a?.b.plusAssign({ c }()) } class A(val b: B) diff --git a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt similarity index 62% rename from compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt rename to compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt index 6d0b98f5d40..7219627e8db 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/neg/2.10.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/building-the-overload-candidate-set-ocs/operator-call/p-1/pos/2.3.kt @@ -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) { - a?.b += c - a?.b .plusAssign(c) + a?.b += c + a?.b .plusAssign(c) val x = { - a?.b += c - a?.b.plusAssign(c) + a?.b += c + a?.b.plusAssign(c) }() - a?.b += { c }() + a?.b += { c }() - a?.b.plusAssign({ c }()) + a?.b.plusAssign({ c }()) } class A(val b: B) diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java index 4b38a1457bc..b215416052f 100644 --- a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java @@ -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); } diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 9f751ff4cc8..9f823c95ff3 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -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), diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/FirIdeSpecTest.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/FirIdeSpecTest.java index e2213febd62..62b1fbb34cb 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/FirIdeSpecTest.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/FirIdeSpecTest.java @@ -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);