Have "in type" for java fields to be able to check that type in assignment positions (against rhs' type)

^KT-46727 Fixed
This commit is contained in:
Victor Petukhov
2021-09-23 18:11:50 +03:00
parent 3530840da3
commit 0cb56be14f
17 changed files with 368 additions and 2 deletions
@@ -13758,6 +13758,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt2872.kt");
}
@Test
@TestMetadata("kt46727.kt")
public void testKt46727() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt");
}
@Test
@TestMetadata("memberScopeOfCaptured.kt")
public void testMemberScopeOfCaptured() throws Exception {
@@ -13758,6 +13758,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt2872.kt");
}
@Test
@TestMetadata("kt46727.kt")
public void testKt46727() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt");
}
@Test
@TestMetadata("memberScopeOfCaptured.kt")
public void testMemberScopeOfCaptured() throws Exception {
@@ -684,6 +684,12 @@ 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 {
@@ -684,6 +684,12 @@ 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 {
@@ -684,6 +684,12 @@ 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 {
@@ -371,8 +371,8 @@ 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();
KotlinType inType = ((PropertyDescriptor) descriptor).getInType();
if (inType != null) return inType;
}
return leftOperandType;
@@ -884,6 +884,8 @@ open class IrBasedPropertyDescriptor(owner: IrProperty) :
TODO("not implemented")
}
override fun getInType(): KotlinType? = setter?.valueParameters?.get(0)?.type
override fun <V : Any?> getUserData(key: CallableDescriptor.UserDataKey<V>?): V? = null
}
@@ -1062,6 +1064,8 @@ open class IrBasedFieldDescriptor(owner: IrField) : PropertyDescriptor, IrBasedD
override fun getBackingField(): FieldDescriptor? = null // TODO
override fun getDelegateField(): FieldDescriptor? = null // TODO
override fun getInType(): KotlinType? = setter?.valueParameters?.get(0)?.type
override fun <V : Any?> getUserData(key: CallableDescriptor.UserDataKey<V>?): V? = null
}
@@ -0,0 +1,111 @@
// WITH_RUNTIME
// FILE: Foo.java
public class Foo<T> {
public T value;
}
// FILE: Foo2.java
import org.jetbrains.annotations.Nullable;
public class Foo2<T> {
public @Nullable T value;
}
// FILE: Foo3.java
import org.jetbrains.annotations.NotNull;
public class Foo3<T> {
public @NotNull T value;
}
// FILE: main.kt
// --- from Java --- //
fun takeStarFoo(x: Foo<*>) {
x.value = <!ASSIGNMENT_TYPE_MISMATCH!>"test"<!>
}
fun main1() {
val foo = Foo<Int>()
foo.value = 1
takeStarFoo(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin --- //
public class Bar<T> {
var value: T = null as T
}
fun takeStarBar(x: Bar<*>) {
<!SETTER_PROJECTED_OUT!>x.value<!> = "test"
}
fun main2() {
val bar = Bar<Int>()
bar.value = 1
takeStarBar(bar)
println(bar.value) // CCE: String cannot be cast to Number
}
// --- from Java (nullable) --- //
fun takeStarFoo2(x: Foo2<*>) {
x.value = <!ASSIGNMENT_TYPE_MISMATCH!>"test"<!>
}
fun main3() {
val foo = Foo2<Int>()
foo.value = 1
takeStarFoo2(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin (nullable) --- //
public class Bar2<T> {
var value: T? = null
}
fun takeStarBar2(x: Bar2<*>) {
<!SETTER_PROJECTED_OUT!>x.value<!> = "test"
}
fun main4() {
val bar = Bar2<Int>()
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 = <!ASSIGNMENT_TYPE_MISMATCH!>"test"<!>
}
fun main5() {
val foo = Foo3<Int>()
foo.value = 1
takeStarFoo3(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin (field) --- //
class Bar3<T> {
@JvmField
var value: T? = null
}
fun takeStarBar3(x: Bar3<*>) {
<!SETTER_PROJECTED_OUT!>x.value<!> = "test"
}
fun main6() {
val bar = Bar3<Int>()
bar.value = 1
takeStarBar3(bar)
println(bar.value) // CCE: String cannot be cast to Number
}
@@ -0,0 +1,111 @@
// WITH_RUNTIME
// FILE: Foo.java
public class Foo<T> {
public T value;
}
// FILE: Foo2.java
import org.jetbrains.annotations.Nullable;
public class Foo2<T> {
public @Nullable T value;
}
// FILE: Foo3.java
import org.jetbrains.annotations.NotNull;
public class Foo3<T> {
public @NotNull T value;
}
// FILE: main.kt
// --- from Java --- //
fun takeStarFoo(x: Foo<*>) {
x.value = <!TYPE_MISMATCH("Nothing!; String")!>"test"<!>
}
fun main1() {
val foo = Foo<Int>()
foo.value = 1
takeStarFoo(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin --- //
public class Bar<T> {
var value: T = null <!UNCHECKED_CAST!>as T<!>
}
fun takeStarBar(x: Bar<*>) {
<!SETTER_PROJECTED_OUT!>x.value<!> = "test"
}
fun main2() {
val bar = Bar<Int>()
bar.value = 1
takeStarBar(bar)
println(bar.value) // CCE: String cannot be cast to Number
}
// --- from Java (nullable) --- //
fun takeStarFoo2(x: Foo2<*>) {
x.value = <!TYPE_MISMATCH("Nothing?; String")!>"test"<!>
}
fun main3() {
val foo = Foo2<Int>()
foo.value = 1
takeStarFoo2(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin (nullable) --- //
public class Bar2<T> {
var value: T? = null
}
fun takeStarBar2(x: Bar2<*>) {
x.value = <!TYPE_MISMATCH_DUE_TO_TYPE_PROJECTIONS("Nothing?; String; Bar2<CapturedType(*)>; public final var value: T? defined in Bar2")!>"test"<!>
}
fun main4() {
val bar = Bar2<Int>()
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 = <!TYPE_MISMATCH("Nothing; String")!>"test"<!>
}
fun main5() {
val foo = Foo3<Int>()
foo.value = 1
takeStarFoo3(foo)
println(foo.value) // CCE: String cannot be cast to Number
}
// --- from Kotlin (field) --- //
class Bar3<T> {
@JvmField
var value: T? = null
}
fun takeStarBar3(x: Bar3<*>) {
x.value = <!TYPE_MISMATCH_DUE_TO_TYPE_PROJECTIONS("Nothing?; String; Bar3<CapturedType(*)>; public final var value: T? defined in Bar3")!>"test"<!>
}
fun main6() {
val bar = Bar3<Int>()
bar.value = 1
takeStarBar3(bar)
println(bar.value) // CCE: String cannot be cast to Number
}
@@ -0,0 +1,53 @@
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 takeStarBar(/*0*/ x: Bar<*>): kotlin.Unit
public fun takeStarBar2(/*0*/ x: Bar2<*>): 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</*0*/ T> {
public constructor Bar</*0*/ T>()
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</*0*/ T> {
public constructor Bar2</*0*/ T>()
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</*0*/ T : kotlin.Any!> {
public constructor Foo</*0*/ T : kotlin.Any!>()
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</*0*/ T : kotlin.Any!> {
public constructor Foo2</*0*/ T : kotlin.Any!>()
@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</*0*/ T : kotlin.Any!> {
public constructor Foo3</*0*/ T : kotlin.Any!>()
@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
}
@@ -13764,6 +13764,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt2872.kt");
}
@Test
@TestMetadata("kt46727.kt")
public void testKt46727() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypes/kt46727.kt");
}
@Test
@TestMetadata("memberScopeOfCaptured.kt")
public void testMemberScopeOfCaptured() throws Exception {
@@ -684,6 +684,12 @@ 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 {
@@ -684,6 +684,12 @@ 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 {
@@ -684,6 +684,12 @@ 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 {
@@ -37,6 +37,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
@Nullable
private final Pair<UserDataKey<?>, ?> singleUserData;
private KotlinType inType = null;
protected JavaPropertyDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@@ -172,6 +174,18 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
(!TypeEnhancementKt.hasEnhancedNullability(type) || KotlinBuiltIns.isString(type));
}
@Override
public void setInType(@NotNull KotlinType inType) {
this.inType = inType;
}
@Override
public @Nullable KotlinType getInType() {
PropertySetterDescriptor setter = getSetter();
KotlinType setterType = setter != null ? setter.getValueParameters().get(0).getType() : null;
return setterType != null ? setterType : inType;
}
@Nullable
@Override
@SuppressWarnings("unchecked")
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import java.util.Collection;
@@ -65,4 +66,7 @@ public interface PropertyDescriptor extends VariableDescriptorWithAccessors, Cal
@NotNull
@Override
CopyBuilder<? extends PropertyDescriptor> newCopyBuilder();
@Nullable
KotlinType getInType();
}
@@ -108,6 +108,15 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
isExpect, isActual, isExternal, isDelegated);
}
public void setInType(@NotNull KotlinType inType) {
/* Do nothing as the corresponding setter is generated by default */
}
@Override
public KotlinType getInType() {
return setter != null ? setter.getValueParameters().get(0).getType() : null;
}
public void setType(
@NotNull KotlinType outType,
@ReadOnly @NotNull List<? extends TypeParameterDescriptor> typeParameters,
@@ -400,6 +409,12 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
return null; // TODO : tell the user that the property was projected out
}
KotlinType inType = substitutor.substitute(originalOutType, Variance.IN_VARIANCE);
if (inType != null) {
substitutedDescriptor.setInType(inType);
}
ReceiverParameterDescriptor substitutedDispatchReceiver;
ReceiverParameterDescriptor dispatchReceiver = copyConfiguration.dispatchReceiverParameter;
if (dispatchReceiver != null) {