From e30d467304aa206eb7b7c6be163df30a9f521b30 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Fri, 24 Sep 2021 12:51:25 +0300 Subject: [PATCH] Implement deprecation cycle for proper refinement rhs type in assignments for java fields ^KT-46727 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 + ...DiagnosticsWithLightTreeTestGenerated.java | 6 + ...nAnnotationsCompiledJavaTestGenerated.java | 6 - ...dJavaWithPsiClassReadingTestGenerated.java | 6 - ...ignAnnotationsSourceJavaTestGenerated.java | 6 - .../ExpressionTypingVisitorForStatements.java | 80 +++++++++++- .../inference/capturedTypes/kt46727.fir.kt | 7 ++ .../tests/inference/capturedTypes/kt46727.kt | 7 ++ .../tests/inference/capturedTypes/kt46727.txt | 12 +- .../capturedTypes/kt46727Warnings.fir.kt | 118 ++++++++++++++++++ .../capturedTypes/kt46727Warnings.kt | 118 ++++++++++++++++++ .../capturedTypes/kt46727Warnings.txt | 62 +++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 + ...nAnnotationsCompiledJavaTestGenerated.java | 6 - ...dJavaWithPsiClassReadingTestGenerated.java | 6 - ...ignAnnotationsSourceJavaTestGenerated.java | 6 - .../kotlin/config/LanguageVersionSettings.kt | 1 + 17 files changed, 420 insertions(+), 39 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt create mode 100644 compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.txt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index ef7bd6f145f..c37c3a0a383 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -13764,6 +13764,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt"); } + @Test + @TestMetadata("kt46727Warnings.kt") + public void testKt46727Warnings() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt"); + } + @Test @TestMetadata("memberScopeOfCaptured.kt") public void testMemberScopeOfCaptured() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 1cc117bfef7..7e745a00569 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -13764,6 +13764,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt"); } + @Test + @TestMetadata("kt46727Warnings.kt") + public void testKt46727Warnings() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt"); + } + @Test @TestMetadata("memberScopeOfCaptured.kt") public void testMemberScopeOfCaptured() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java index c3515bfa702..cb097a18770 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java @@ -684,12 +684,6 @@ public class FirOldFrontendForeignAnnotationsCompiledJavaTestGenerated extends A runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java index 3e29e5162aa..b576a256a30 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java @@ -684,12 +684,6 @@ public class FirOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTest runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsSourceJavaTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsSourceJavaTestGenerated.java index 02b12d83adf..b15f02e0757 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsSourceJavaTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendForeignAnnotationsSourceJavaTestGenerated.java @@ -684,12 +684,6 @@ public class FirOldFrontendForeignAnnotationsSourceJavaTestGenerated extends Abs runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java index 015f15a4bc6..73279fffc9f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver; import org.jetbrains.kotlin.resolve.calls.checkers.NewSchemeOfIntegerOperatorResolutionChecker; import org.jetbrains.kotlin.resolve.calls.context.CallPosition; import org.jetbrains.kotlin.resolve.calls.context.ContextDependency; +import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext; import org.jetbrains.kotlin.resolve.calls.context.TemporaryTraceAndCache; import org.jetbrains.kotlin.resolve.calls.inference.BuilderInferenceSession; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; @@ -335,7 +336,15 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito } KotlinTypeInfo rightInfo = facade.getTypeInfo(rightOperand, context.replaceDataFlowInfo(leftInfo.getDataFlowInfo())); - KotlinType expectedType = refineTypeFromPropertySetterIfPossible(context.trace.getBindingContext(), leftOperand, leftInfo.getType()); + boolean refineJavaFieldInTypeProperly = + components.languageVersionSettings.supportsFeature(LanguageFeature.RefineTypeCheckingOnAssignmentsToJavaFields); + + BindingContext bindingContext = context.trace.getBindingContext(); + KotlinType leftType = leftInfo.getType(); + + KotlinType expectedType = refineJavaFieldInTypeProperly + ? refineTypeByPropertyInType(bindingContext, leftOperand, leftType) + : refineTypeFromPropertySetterIfPossible(bindingContext, leftOperand, leftType); Ref hasErrorsOnTypeChecking = Ref.create(false); components.dataFlowAnalyzer.checkType( @@ -349,6 +358,12 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito ); basic.checkLValue(context.trace, context, leftOperand, rightOperand, expression, false); + if (!refineJavaFieldInTypeProperly) { + checkPropertyInTypeWithWarnings( + context, expression, binaryOperationType, rightInfo.getDataFlowInfo(), leftOperand, leftType, expectedType + ); + } + return !hasErrorsOnTypeChecking.get() ? rightInfo : null; } @@ -370,6 +385,22 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito ) { VariableDescriptor descriptor = BindingContextUtils.extractVariableFromResolvedCall(bindingContext, leftOperand); + if (descriptor instanceof PropertyDescriptor) { + PropertySetterDescriptor setter = ((PropertyDescriptor) descriptor).getSetter(); + if (setter != null) return setter.getValueParameters().get(0).getType(); + } + + return leftOperandType; + } + + @Nullable + private static KotlinType refineTypeByPropertyInType( + @NotNull BindingContext bindingContext, + @Nullable KtElement leftOperand, + @Nullable KotlinType leftOperandType + ) { + VariableDescriptor descriptor = BindingContextUtils.extractVariableFromResolvedCall(bindingContext, leftOperand); + if (descriptor instanceof PropertyDescriptor) { KotlinType inType = ((PropertyDescriptor) descriptor).getInType(); if (inType != null) return inType; @@ -400,7 +431,16 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito context.replaceCallPosition(new CallPosition.PropertyAssignment(left, true)), facade ); - KotlinType expectedType = refineTypeFromPropertySetterIfPossible(context.trace.getBindingContext(), leftOperand, leftInfo.getType()); + + BindingContext bindingContext = context.trace.getBindingContext(); + KotlinType leftType = leftInfo.getType(); + + boolean refineJavaFieldInTypeProperly = + components.languageVersionSettings.supportsFeature(LanguageFeature.RefineTypeCheckingOnAssignmentsToJavaFields); + KotlinType expectedType = refineJavaFieldInTypeProperly + ? refineTypeByPropertyInType(bindingContext, leftOperand, leftType) + : refineTypeFromPropertySetterIfPossible(bindingContext, leftOperand, leftType); + DataFlowInfo dataFlowInfo = leftInfo.getDataFlowInfo(); KotlinTypeInfo resultInfo; if (right != null) { @@ -427,9 +467,45 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito if (expectedType != null && leftOperand != null) { //if expectedType == null, some other error has been generated basic.checkLValue(context.trace, context, leftOperand, right, expression, false); } + + if (!refineJavaFieldInTypeProperly) { + checkPropertyInTypeWithWarnings( + context, expression, resultInfo.getType(), resultInfo.getDataFlowInfo(), leftOperand, leftType, expectedType + ); + } + return resultInfo.replaceType(components.dataFlowAnalyzer.checkStatementType(expression, contextWithExpectedType)); } + private void checkPropertyInTypeWithWarnings( + @NotNull ResolutionContext context, + @NotNull KtBinaryExpression expression, + @Nullable KotlinType rhsType, + @NotNull DataFlowInfo rhsDataFlowInfo, + @Nullable KtExpression lhsOperand, + @Nullable KotlinType lhsType, + @Nullable KotlinType expectedType + ) { + if (rhsType == null || expectedType == null) return; + + KotlinType expectedTypeByInType = refineTypeByPropertyInType(context.trace.getBindingContext(), lhsOperand, lhsType); + + if (expectedTypeByInType != null && expectedType != expectedTypeByInType && !TypeUtils.equalTypes(expectedType, expectedTypeByInType)) { + Ref hasErrorsOnTypeChecking = Ref.create(false); + components.dataFlowAnalyzer.checkType( + rhsType, + expression, + context.replaceExpectedType(expectedTypeByInType) + .replaceDataFlowInfo(rhsDataFlowInfo) + .replaceCallPosition(new CallPosition.PropertyAssignment(lhsOperand, false)), + hasErrorsOnTypeChecking, + false + ); + if (hasErrorsOnTypeChecking.get()) { + context.trace.report(TYPE_MISMATCH_WARNING.on(expression, expectedTypeByInType, rhsType)); + } + } + } @Override public KotlinTypeInfo visitExpression(@NotNull KtExpression expression, ExpressionTypingContext context) { diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.fir.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.fir.kt index d56997beed0..57b06f2180e 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.fir.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: +RefineTypeCheckingOnAssignmentsToJavaFields // WITH_RUNTIME // FILE: Foo.java @@ -25,6 +26,7 @@ public class Foo3 { fun takeStarFoo(x: Foo<*>) { x.value = "test" + x.value += "test" } fun main1() { @@ -42,6 +44,7 @@ public class Bar { fun takeStarBar(x: Bar<*>) { x.value = "test" + x.value += "test" } fun main2() { @@ -55,6 +58,7 @@ fun main2() { fun takeStarFoo2(x: Foo2<*>) { x.value = "test" + x.value += "test" } fun main3() { @@ -71,6 +75,7 @@ public class Bar2 { fun takeStarBar2(x: Bar2<*>) { x.value = "test" + x.value += "test" } fun main4() { @@ -84,6 +89,7 @@ fun main4() { fun takeStarFoo3(x: Foo3<*>) { x.value = "test" + x.value += "test" } fun main5() { @@ -101,6 +107,7 @@ class Bar3 { fun takeStarBar3(x: Bar3<*>) { x.value = "test" + x.value += "test" } fun main6() { diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt index 757c449183c..86bd7cd8311 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: +RefineTypeCheckingOnAssignmentsToJavaFields // WITH_RUNTIME // FILE: Foo.java @@ -25,6 +26,7 @@ public class Foo3 { fun takeStarFoo(x: Foo<*>) { x.value = "test" + x.value += "test" } fun main1() { @@ -42,6 +44,7 @@ public class Bar { fun takeStarBar(x: Bar<*>) { x.value = "test" + x.value += "test" } fun main2() { @@ -55,6 +58,7 @@ fun main2() { fun takeStarFoo2(x: Foo2<*>) { x.value = "test" + x.value += "test" } fun main3() { @@ -71,6 +75,7 @@ public class Bar2 { fun takeStarBar2(x: Bar2<*>) { x.value = ; public final var value: T? defined in Bar2")!>"test" + x.value += "test" } fun main4() { @@ -84,6 +89,7 @@ fun main4() { fun takeStarFoo3(x: Foo3<*>) { x.value = "test" + x.value += "test" } fun main5() { @@ -101,6 +107,7 @@ class Bar3 { fun takeStarBar3(x: Bar3<*>) { x.value = ; public final var value: T? defined in Bar3")!>"test" + x.value += "test" } fun main6() { diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.txt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.txt index 7b8ac704e39..ae01fe093f1 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.txt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.txt @@ -5,8 +5,10 @@ public fun main2(): kotlin.Unit public fun main3(): kotlin.Unit public fun main4(): kotlin.Unit public fun main5(): kotlin.Unit +public fun main6(): kotlin.Unit public fun takeStarBar(/*0*/ x: Bar<*>): kotlin.Unit public fun takeStarBar2(/*0*/ x: Bar2<*>): kotlin.Unit +public fun takeStarBar3(/*0*/ x: Bar3<*>): kotlin.Unit public fun takeStarFoo(/*0*/ x: Foo<*>): kotlin.Unit public fun takeStarFoo2(/*0*/ x: Foo2<*>): kotlin.Unit public fun takeStarFoo3(/*0*/ x: Foo3<*>): kotlin.Unit @@ -27,6 +29,14 @@ public final class Bar2 { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } +public final class Bar3 { + public constructor Bar3() + @field:kotlin.jvm.JvmField public final var value: T? + 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 +} + public open class Foo { public constructor Foo() public final var value: T! @@ -45,7 +55,7 @@ public open class Foo2 { public open class Foo3 { public constructor Foo3() - @org.jetbrains.annotations.Nullable public final var value: T? + @org.jetbrains.annotations.NotNull public final var value: T 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 diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.fir.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.fir.kt new file mode 100644 index 00000000000..37d88a85620 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.fir.kt @@ -0,0 +1,118 @@ +// !LANGUAGE: -RefineTypeCheckingOnAssignmentsToJavaFields +// WITH_RUNTIME + +// FILE: Foo.java +public class Foo { + public T value; +} + +// FILE: Foo2.java +import org.jetbrains.annotations.Nullable; + +public class Foo2 { + public @Nullable T value; +} + +// FILE: Foo3.java +import org.jetbrains.annotations.NotNull; + +public class Foo3 { + public @NotNull T value; +} + +// FILE: main.kt + +// --- from Java --- // + +fun takeStarFoo(x: Foo<*>) { + x.value = "test" + x.value += "test" +} + +fun main1() { + val foo = Foo() + foo.value = 1 + takeStarFoo(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin --- // + +public class Bar { + var value: T = null as T +} + +fun takeStarBar(x: Bar<*>) { + x.value = "test" + x.value += "test" +} + +fun main2() { + val bar = Bar() + bar.value = 1 + takeStarBar(bar) + println(bar.value) // CCE: String cannot be cast to Number +} + +// --- from Java (nullable) --- // + +fun takeStarFoo2(x: Foo2<*>) { + x.value = "test" + x.value += "test" +} + +fun main3() { + val foo = Foo2() + foo.value = 1 + takeStarFoo2(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin (nullable) --- // +public class Bar2 { + var value: T? = null +} + +fun takeStarBar2(x: Bar2<*>) { + x.value = "test" + x.value += "test" +} + +fun main4() { + val bar = Bar2() + bar.value = 1 + takeStarBar2(bar) + println(bar.value) // CCE: String cannot be cast to Number +} + +// --- from Java (not-null) --- // + +fun takeStarFoo3(x: Foo3<*>) { + x.value = "test" + x.value += "test" +} + +fun main5() { + val foo = Foo3() + foo.value = 1 + takeStarFoo3(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin (field) --- // +class Bar3 { + @JvmField + var value: T? = null +} + +fun takeStarBar3(x: Bar3<*>) { + x.value = "test" + x.value += "test" +} + +fun main6() { + val bar = Bar3() + bar.value = 1 + takeStarBar3(bar) + println(bar.value) // CCE: String cannot be cast to Number +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt new file mode 100644 index 00000000000..4a1e9b6d567 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt @@ -0,0 +1,118 @@ +// !LANGUAGE: -RefineTypeCheckingOnAssignmentsToJavaFields +// WITH_RUNTIME + +// FILE: Foo.java +public class Foo { + public T value; +} + +// FILE: Foo2.java +import org.jetbrains.annotations.Nullable; + +public class Foo2 { + public @Nullable T value; +} + +// FILE: Foo3.java +import org.jetbrains.annotations.NotNull; + +public class Foo3 { + public @NotNull T value; +} + +// FILE: main.kt + +// --- from Java --- // + +fun takeStarFoo(x: Foo<*>) { + x.value = "test" + x.value += "test" +} + +fun main1() { + val foo = Foo() + foo.value = 1 + takeStarFoo(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin --- // + +public class Bar { + var value: T = null as T +} + +fun takeStarBar(x: Bar<*>) { + x.value = "test" + x.value += "test" +} + +fun main2() { + val bar = Bar() + bar.value = 1 + takeStarBar(bar) + println(bar.value) // CCE: String cannot be cast to Number +} + +// --- from Java (nullable) --- // + +fun takeStarFoo2(x: Foo2<*>) { + x.value = "test" + x.value += "test" +} + +fun main3() { + val foo = Foo2() + foo.value = 1 + takeStarFoo2(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin (nullable) --- // +public class Bar2 { + var value: T? = null +} + +fun takeStarBar2(x: Bar2<*>) { + x.value = ; public final var value: T? defined in Bar2")!>"test" + x.value += "test" +} + +fun main4() { + val bar = Bar2() + bar.value = 1 + takeStarBar2(bar) + println(bar.value) // CCE: String cannot be cast to Number +} + +// --- from Java (not-null) --- // + +fun takeStarFoo3(x: Foo3<*>) { + x.value = "test" + x.value += "test" +} + +fun main5() { + val foo = Foo3() + foo.value = 1 + takeStarFoo3(foo) + println(foo.value) // CCE: String cannot be cast to Number +} + +// --- from Kotlin (field) --- // +class Bar3 { + @JvmField + var value: T? = null +} + +fun takeStarBar3(x: Bar3<*>) { + x.value = ; public final var value: T? defined in Bar3")!>"test" + x.value += "test" +} + +fun main6() { + val bar = Bar3() + bar.value = 1 + takeStarBar3(bar) + println(bar.value) // CCE: String cannot be cast to Number +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.txt b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.txt new file mode 100644 index 00000000000..80a677bd0a8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.txt @@ -0,0 +1,62 @@ +package + +public fun main1(): kotlin.Unit +public fun main2(): kotlin.Unit +public fun main3(): kotlin.Unit +public fun main4(): kotlin.Unit +public fun main5(): kotlin.Unit +public fun main6(): kotlin.Unit +public fun takeStarBar(/*0*/ x: Bar<*>): kotlin.Unit +public fun takeStarBar2(/*0*/ x: Bar2<*>): kotlin.Unit +public fun takeStarBar3(/*0*/ x: Bar3<*>): kotlin.Unit +public fun takeStarFoo(/*0*/ x: Foo<*>): kotlin.Unit +public fun takeStarFoo2(/*0*/ x: Foo2<*>): kotlin.Unit +public fun takeStarFoo3(/*0*/ x: Foo3<*>): kotlin.Unit + +public final class Bar { + public constructor Bar() + public final var value: T + 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 +} + +public final class Bar2 { + public constructor Bar2() + public final var value: T? + 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 +} + +public final class Bar3 { + public constructor Bar3() + @field:kotlin.jvm.JvmField public final var value: T? + 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 +} + +public open class Foo { + public constructor Foo() + public final var value: T! + 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 +} + +public open class Foo2 { + public constructor Foo2() + @org.jetbrains.annotations.Nullable public final var value: T? + 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 +} + +public open class Foo3 { + public constructor Foo3() + @org.jetbrains.annotations.NotNull public final var value: T + 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 +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 8073dd7f5e5..0ee82249fc0 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -13770,6 +13770,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt"); } + @Test + @TestMetadata("kt46727Warnings.kt") + public void testKt46727Warnings() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727Warnings.kt"); + } + @Test @TestMetadata("memberScopeOfCaptured.kt") public void testMemberScopeOfCaptured() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java index f47c439d389..685fa024920 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java @@ -684,12 +684,6 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java index 70efc6e89a7..232a42da463 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java @@ -684,12 +684,6 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java index 60c78371bdb..8f007f7291a 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java @@ -684,12 +684,6 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt"); } - @Test - @TestMetadata("kt48261.kt") - public void testKt48261() throws Exception { - runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt48261.kt"); - } - @Test @TestMetadata("NonPlatformTypeParameter.kt") public void testNonPlatformTypeParameter() throws Exception { diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 8f761ef0336..a328978e02c 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -231,6 +231,7 @@ enum class LanguageFeature( SafeCallsAreAlwaysNullable(KOTLIN_1_7), StopPropagatingDeprecationThroughOverrides(KOTLIN_1_7), AbstractClassMemberNotImplementedWithIntermediateAbstractClass(KOTLIN_1_7, kind = BUG_FIX), + RefineTypeCheckingOnAssignmentsToJavaFields(KOTLIN_1_7), // Temporarily disabled, see KT-27084/KT-22379 SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),